blob: 1e37300cf61524d2e2676fc6fbab5107cb42e338 [file] [log] [blame]
// Copyright (c) 2020, the R8 project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
package com.android.tools.r8.shaking.b169045091;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import com.android.tools.r8.NeverClassInline;
import com.android.tools.r8.NeverInline;
import com.android.tools.r8.NeverMerge;
import com.android.tools.r8.TestBase;
import com.android.tools.r8.TestParameters;
import com.android.tools.r8.TestParametersCollection;
import com.android.tools.r8.graph.AppView;
import com.android.tools.r8.graph.DexItemFactory;
import com.android.tools.r8.graph.DexMethod;
import com.android.tools.r8.graph.DexProgramClass;
import com.android.tools.r8.shaking.AppInfoWithLiveness;
import com.android.tools.r8.shaking.b169045091.testclasses.HelloGreeter;
import com.android.tools.r8.shaking.b169045091.testclasses.WorldGreeterBase;
import com.google.common.collect.ImmutableList;
import java.util.List;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
@RunWith(Parameterized.class)
public class B169045091 extends TestBase {
private final TestParameters parameters;
@Parameterized.Parameters(name = "{0}")
public static TestParametersCollection data() {
return getTestParameters().withAllRuntimesAndApiLevels().build();
}
public B169045091(TestParameters parameters) {
this.parameters = parameters;
}
@Test
public void testRuntime() throws Exception {
testForRuntime(parameters)
.addProgramClasses(getProgramClasses())
.addProgramClassFileData(getWorldGreeterClassFileData())
.run(parameters.getRuntime(), TestClass.class)
.assertSuccessWithOutputLines("Hello world!");
}
@Test
public void testR8() throws Exception {
testForR8(parameters.getBackend())
.addProgramClasses(getProgramClasses())
.addProgramClassFileData(getWorldGreeterClassFileData())
.addKeepMainRule(TestClass.class)
.enableNeverClassInliningAnnotations()
.enableInliningAnnotations()
.enableMergeAnnotations()
.setMinApi(parameters.getApiLevel())
.noMinification()
.compile()
.run(parameters.getRuntime(), TestClass.class)
.assertSuccessWithOutputLines("Hello world!");
}
private List<Class<?>> getProgramClasses() {
return ImmutableList.of(
TestClass.class, HelloGreeter.class, HelloGreeterBase.class, WorldGreeterBase.class);
}
private byte[] getWorldGreeterClassFileData() throws Exception {
return transformer(WorldGreeter.class)
.removeMethods(
(int access, String name, String descriptor, String signature, String[] exceptions) ->
name.equals("world"))
.transform();
}
@Test
public void testAccessibility() throws Exception {
AppView<AppInfoWithLiveness> appView =
computeAppViewWithLiveness(
buildClasses(getProgramClasses())
.addClassProgramData(getWorldGreeterClassFileData())
.build(),
TestClass.class);
AppInfoWithLiveness appInfo = appView.appInfo();
DexItemFactory dexItemFactory = appView.dexItemFactory();
DexProgramClass context =
appView.definitionFor(buildType(TestClass.class, dexItemFactory)).asProgramClass();
// Test that HelloGreeter.greet() is accessible to TestClass.
DexMethod helloReference = buildNullaryVoidMethod(HelloGreeter.class, "hello", dexItemFactory);
assertTrue(
appInfo
.resolveMethodOnClass(helloReference.holder, helloReference)
.isAccessibleFrom(context, appInfo));
// Test that WorldGreeter.greet() is inaccessible to TestClass.
DexMethod worldReference = buildNullaryVoidMethod(WorldGreeter.class, "world", dexItemFactory);
assertFalse(
appInfo
.resolveMethodOnClass(worldReference.holder, worldReference)
.isAccessibleFrom(context, appInfo));
}
public static class TestClass {
public static void main(String[] args) {
// HelloGreeterBase.greet() is accessible to TestClass because they are in the same package.
new HelloGreeter().hello();
try {
// WorldGreeterBase.world() is inaccessible to TestClass.
new WorldGreeter().world();
throw new RuntimeException();
} catch (IllegalAccessError e) {
System.out.println(" world!");
}
}
}
@NeverMerge
public static class HelloGreeterBase {
@NeverInline
protected void hello() {
System.out.print("Hello");
}
}
@NeverClassInline
public static class WorldGreeter extends WorldGreeterBase {
// Removed by a transformer.
@Override
public void world() {
super.world();
}
}
}