Misc. tests for access modification
Change-Id: I4e16e4d3dc64111fec09ef0394f7351c5a64bfe2
diff --git a/src/test/java/com/android/tools/r8/accessrelaxation/InterfaceMethodAndSiblingConsistentRenamingPublicizerTest.java b/src/test/java/com/android/tools/r8/accessrelaxation/InterfaceMethodAndSiblingConsistentRenamingPublicizerTest.java
new file mode 100644
index 0000000..0cf8434
--- /dev/null
+++ b/src/test/java/com/android/tools/r8/accessrelaxation/InterfaceMethodAndSiblingConsistentRenamingPublicizerTest.java
@@ -0,0 +1,129 @@
+// Copyright (c) 2023, 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.accessrelaxation;
+
+import static com.android.tools.r8.utils.codeinspector.Matchers.isPresent;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotEquals;
+
+import com.android.tools.r8.NeverClassInline;
+import com.android.tools.r8.NeverInline;
+import com.android.tools.r8.NoHorizontalClassMerging;
+import com.android.tools.r8.TestBase;
+import com.android.tools.r8.TestParameters;
+import com.android.tools.r8.TestParametersCollection;
+import com.android.tools.r8.utils.codeinspector.ClassSubject;
+import com.android.tools.r8.utils.codeinspector.MethodSubject;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.Parameterized;
+import org.junit.runners.Parameterized.Parameter;
+import org.junit.runners.Parameterized.Parameters;
+
+@RunWith(Parameterized.class)
+public class InterfaceMethodAndSiblingConsistentRenamingPublicizerTest extends TestBase {
+
+ @Parameter(0)
+ public TestParameters parameters;
+
+ @Parameters(name = "{0}")
+ public static TestParametersCollection data() {
+ return getTestParameters().withAllRuntimesAndApiLevels().build();
+ }
+
+ @Test
+ public void test() throws Exception {
+ testForR8(parameters.getBackend())
+ .addInnerClasses(getClass())
+ .addKeepMainRule(Main.class)
+ .enableInliningAnnotations()
+ .enableNeverClassInliningAnnotations()
+ .enableNoHorizontalClassMergingAnnotations()
+ .setMinApi(parameters)
+ .compile()
+ .inspect(
+ inspector -> {
+ // Check that B.foo(), C.foo() and I.foo() are all present in the output.
+ ClassSubject bClassSubject = inspector.clazz(B.class);
+ assertThat(bClassSubject, isPresent());
+
+ MethodSubject bMethodSubject = bClassSubject.uniqueMethodWithOriginalName("foo");
+ assertThat(bMethodSubject, isPresent());
+
+ ClassSubject cClassSubject = inspector.clazz(C.class);
+ assertThat(cClassSubject, isPresent());
+
+ MethodSubject cMethodSubject = cClassSubject.uniqueMethodWithOriginalName("foo");
+ assertThat(cMethodSubject, isPresent());
+
+ ClassSubject iClassSubject = inspector.clazz(I.class);
+ assertThat(iClassSubject, isPresent());
+
+ MethodSubject iMethodSubject = iClassSubject.uniqueMethodWithOriginalName("foo");
+ assertThat(iMethodSubject, isPresent());
+
+ // Check that B.foo() is renamed to one name, and C.foo() and I.foo() is renamed to
+ // another name. The reason for B.foo() being given another name is that we use a
+ // single naming state for the classes, which means that we must give B.foo() another
+ // name to ensure we do not introduce new method overriding relationships from
+ // publicizing.
+ assertNotEquals(bMethodSubject.getFinalName(), cMethodSubject.getFinalName());
+ assertEquals(cMethodSubject.getFinalName(), iMethodSubject.getFinalName());
+ })
+ .run(parameters.getRuntime(), Main.class)
+ .assertSuccessWithOutputLines("B", "C", "C");
+ }
+
+ static class Main {
+
+ public static void main(String[] args) {
+ new B().foo();
+ new C().foo();
+ I i = System.currentTimeMillis() > 0 ? new D() : new E();
+ i.foo();
+ }
+ }
+
+ interface I {
+
+ void foo();
+ }
+
+ static class A {}
+
+ @NeverClassInline
+ @NoHorizontalClassMerging
+ static class B extends A {
+
+ @NeverInline
+ private void foo() {
+ System.out.println("B");
+ }
+ }
+
+ @NeverClassInline
+ @NoHorizontalClassMerging
+ static class C extends A {
+
+ // Implements I.foo().
+ @NeverInline
+ public void foo() {
+ System.out.println("C");
+ }
+ }
+
+ @NoHorizontalClassMerging
+ static class D extends C implements I {}
+
+ @NoHorizontalClassMerging
+ static class E extends C implements I {
+
+ @Override
+ public void foo() {
+ System.out.println("E");
+ }
+ }
+}
diff --git a/src/test/java/com/android/tools/r8/accessrelaxation/PrivateOverrideOfPublicMethodTest.java b/src/test/java/com/android/tools/r8/accessrelaxation/PrivateOverrideOfPublicMethodTest.java
new file mode 100644
index 0000000..ea6a6a0
--- /dev/null
+++ b/src/test/java/com/android/tools/r8/accessrelaxation/PrivateOverrideOfPublicMethodTest.java
@@ -0,0 +1,123 @@
+// Copyright (c) 2023, 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.accessrelaxation;
+
+import static com.android.tools.r8.transformers.ClassFileTransformer.MethodPredicate.onName;
+
+import com.android.tools.r8.NeverInline;
+import com.android.tools.r8.NoVerticalClassMerging;
+import com.android.tools.r8.TestBase;
+import com.android.tools.r8.TestParameters;
+import com.android.tools.r8.TestParametersCollection;
+import com.android.tools.r8.ToolHelper.DexVm.Version;
+import com.android.tools.r8.utils.StringUtils;
+import java.io.IOException;
+import org.junit.BeforeClass;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.Parameterized;
+import org.junit.runners.Parameterized.Parameter;
+import org.junit.runners.Parameterized.Parameters;
+
+@RunWith(Parameterized.class)
+public class PrivateOverrideOfPublicMethodTest extends TestBase {
+
+ private static final String EXPECTED_OUTPUT = StringUtils.lines("A", "B", "IAE", "A", "B", "A");
+ private static final String EXPECTED_OUTPUT_5_TO_6 =
+ StringUtils.lines("A", "A", "A", "A", "A", "A");
+ private static final String EXPECTED_OUTPUT_7 = StringUtils.lines("A", "B", "A", "A", "B", "A");
+
+ private static byte[] programClassFileData;
+
+ @Parameter(0)
+ public TestParameters parameters;
+
+ @Parameters(name = "{0}")
+ public static TestParametersCollection data() {
+ return getTestParameters().withAllRuntimesAndApiLevels().build();
+ }
+
+ @BeforeClass
+ public static void setup() throws IOException {
+ programClassFileData =
+ transformer(B.class)
+ .renameMethod(onName("bar"), "foo")
+ .transformMethodInsnInMethod(
+ "<init>",
+ (opcode, owner, name, descriptor, isInterface, continuation) ->
+ continuation.visitMethodInsn(
+ opcode, owner, name.equals("bar") ? "foo" : name, descriptor, isInterface))
+ .transform();
+ }
+
+ @Test
+ public void testRuntime() throws Exception {
+ testForRuntime(parameters)
+ .addProgramClasses(Main.class, A.class)
+ .addProgramClassFileData(programClassFileData)
+ .run(parameters.getRuntime(), Main.class)
+ .applyIf(
+ parameters.isDexRuntimeVersionNewerThanOrEqual(Version.V5_1_1)
+ && parameters.isDexRuntimeVersionOlderThanOrEqual(Version.V6_0_1),
+ runResult -> runResult.assertSuccessWithOutput(EXPECTED_OUTPUT_5_TO_6),
+ parameters.isDexRuntimeVersion(Version.V7_0_0),
+ runResult -> runResult.assertSuccessWithOutput(EXPECTED_OUTPUT_7),
+ runResult -> runResult.assertSuccessWithOutput(EXPECTED_OUTPUT));
+ }
+
+ @Test
+ public void testR8() throws Exception {
+ testForR8(parameters.getBackend())
+ .addProgramClasses(Main.class, A.class)
+ .addProgramClassFileData(programClassFileData)
+ .addKeepMainRule(Main.class)
+ .allowAccessModification()
+ .enableInliningAnnotations()
+ .enableNoVerticalClassMergingAnnotations()
+ .setMinApi(parameters)
+ .run(parameters.getRuntime(), Main.class)
+ .assertSuccessWithOutput(EXPECTED_OUTPUT);
+ }
+
+ static class Main {
+
+ public static void main(String[] args) {
+ new A().foo();
+ try {
+ new B().foo();
+ } catch (IllegalAccessError e) {
+ System.out.println("IAE");
+ }
+ A a = System.currentTimeMillis() > 0 ? new A() : new B();
+ a.foo();
+ A b = System.currentTimeMillis() > 0 ? new B() : new A();
+ b.foo();
+ }
+ }
+
+ @NoVerticalClassMerging
+ static class A {
+
+ @NeverInline
+ public void foo() {
+ System.out.println("A");
+ }
+ }
+
+ static class B extends A {
+
+ B() {
+ if (System.currentTimeMillis() > 0) {
+ bar();
+ }
+ }
+
+ // Renamed to foo.
+ @NeverInline
+ private void bar() {
+ System.out.println("B");
+ }
+ }
+}
diff --git a/src/test/java/com/android/tools/r8/accessrelaxation/PrivateShadowOfPrivateMethodTest.java b/src/test/java/com/android/tools/r8/accessrelaxation/PrivateShadowOfPrivateMethodTest.java
new file mode 100644
index 0000000..4698b94
--- /dev/null
+++ b/src/test/java/com/android/tools/r8/accessrelaxation/PrivateShadowOfPrivateMethodTest.java
@@ -0,0 +1,104 @@
+// Copyright (c) 2023, 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.accessrelaxation;
+
+import static com.android.tools.r8.utils.codeinspector.Matchers.isPresent;
+import static com.android.tools.r8.utils.codeinspector.Matchers.isPrivate;
+import static com.android.tools.r8.utils.codeinspector.Matchers.isPublic;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.junit.Assert.assertEquals;
+
+import com.android.tools.r8.NeverClassInline;
+import com.android.tools.r8.NeverInline;
+import com.android.tools.r8.NoVerticalClassMerging;
+import com.android.tools.r8.TestBase;
+import com.android.tools.r8.TestParameters;
+import com.android.tools.r8.TestParametersCollection;
+import com.android.tools.r8.utils.codeinspector.ClassSubject;
+import com.android.tools.r8.utils.codeinspector.MethodSubject;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.Parameterized;
+import org.junit.runners.Parameterized.Parameter;
+import org.junit.runners.Parameterized.Parameters;
+
+@RunWith(Parameterized.class)
+public class PrivateShadowOfPrivateMethodTest extends TestBase {
+
+ @Parameter(0)
+ public TestParameters parameters;
+
+ @Parameters(name = "{0}")
+ public static TestParametersCollection data() {
+ return getTestParameters().withAllRuntimesAndApiLevels().build();
+ }
+
+ @Test
+ public void test() throws Exception {
+ testForR8(parameters.getBackend())
+ .addInnerClasses(getClass())
+ .addKeepMainRule(Main.class)
+ .allowAccessModification()
+ .enableInliningAnnotations()
+ .enableNeverClassInliningAnnotations()
+ .enableNoVerticalClassMergingAnnotations()
+ .setMinApi(parameters)
+ .compile()
+ .inspect(
+ inspector -> {
+ // Check that A.foo() is publicized.
+ ClassSubject aClassSubject = inspector.clazz(A.class);
+ assertThat(aClassSubject, isPresent());
+
+ MethodSubject aMethodSubject = aClassSubject.uniqueMethodWithOriginalName("foo");
+ assertThat(aMethodSubject, isPresent());
+ assertThat(aMethodSubject, isPublic());
+
+ // Check that B.foo is still private.
+ ClassSubject bClassSubject = inspector.clazz(B.class);
+ assertThat(bClassSubject, isPresent());
+
+ MethodSubject bMethodSubject = bClassSubject.uniqueMethodWithOriginalName("foo");
+ assertThat(bMethodSubject, isPresent());
+ assertThat(bMethodSubject, isPrivate());
+
+ // Verify that the two methods still have the same name.
+ assertEquals(aMethodSubject.getFinalName(), bMethodSubject.getFinalName());
+ })
+ .run(parameters.getRuntime(), Main.class)
+ .assertSuccessWithOutputLines("A", "B", "A", "A");
+ }
+
+ static class Main {
+
+ public static void main(String[] args) {
+ new A().foo();
+ new B().foo();
+ A a = System.currentTimeMillis() > 0 ? new A() : new B();
+ a.foo();
+ A b = System.currentTimeMillis() > 0 ? new B() : new A();
+ b.foo();
+ }
+ }
+
+ @NeverClassInline
+ @NoVerticalClassMerging
+ static class A {
+
+ @NeverInline
+ private void foo() {
+ System.out.println("A");
+ }
+ }
+
+ @NeverClassInline
+ static class B extends A {
+
+ @NeverInline
+ private void foo() {
+ System.out.println("B");
+ }
+ }
+}
diff --git a/src/test/java/com/android/tools/r8/accessrelaxation/PublicOverrideOfPrivateMethodTest.java b/src/test/java/com/android/tools/r8/accessrelaxation/PublicOverrideOfPrivateMethodTest.java
new file mode 100644
index 0000000..09d3456
--- /dev/null
+++ b/src/test/java/com/android/tools/r8/accessrelaxation/PublicOverrideOfPrivateMethodTest.java
@@ -0,0 +1,91 @@
+// Copyright (c) 2023, 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.accessrelaxation;
+
+import com.android.tools.r8.NeverClassInline;
+import com.android.tools.r8.NeverInline;
+import com.android.tools.r8.NoVerticalClassMerging;
+import com.android.tools.r8.TestBase;
+import com.android.tools.r8.TestParameters;
+import com.android.tools.r8.TestParametersCollection;
+import com.android.tools.r8.utils.StringUtils;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.Parameterized;
+import org.junit.runners.Parameterized.Parameter;
+import org.junit.runners.Parameterized.Parameters;
+
+@RunWith(Parameterized.class)
+public class PublicOverrideOfPrivateMethodTest extends TestBase {
+
+ private static final String EXPECTED_OUTPUT = StringUtils.lines("A", "B", "B", "A", "B", "A");
+
+ @Parameter(0)
+ public TestParameters parameters;
+
+ @Parameters(name = "{0}")
+ public static TestParametersCollection data() {
+ return getTestParameters().withAllRuntimesAndApiLevels().build();
+ }
+
+ @Test
+ public void testRuntime() throws Exception {
+ testForRuntime(parameters)
+ .addInnerClasses(getClass())
+ .run(parameters.getRuntime(), Main.class)
+ .assertSuccessWithOutput(EXPECTED_OUTPUT);
+ }
+
+ @Test
+ public void testR8() throws Exception {
+ testForR8(parameters.getBackend())
+ .addInnerClasses(getClass())
+ .addKeepMainRule(Main.class)
+ .allowAccessModification()
+ .enableInliningAnnotations()
+ .enableNeverClassInliningAnnotations()
+ .enableNoVerticalClassMergingAnnotations()
+ .setMinApi(parameters)
+ .run(parameters.getRuntime(), Main.class)
+ .assertSuccessWithOutput(EXPECTED_OUTPUT);
+ }
+
+ static class Main {
+
+ public static void main(String[] args) {
+ new A().foo();
+ new B().foo();
+ A a = System.currentTimeMillis() > 0 ? new A() : new B();
+ a.foo();
+ A b = System.currentTimeMillis() > 0 ? new B() : new A();
+ b.foo();
+ }
+ }
+
+ @NeverClassInline
+ @NoVerticalClassMerging
+ static class A {
+
+ @NeverInline
+ private void foo() {
+ System.out.println("A");
+ }
+ }
+
+ @NeverClassInline
+ static class B extends A {
+
+ B() {
+ if (System.currentTimeMillis() > 0) {
+ foo();
+ }
+ }
+
+ @NeverInline
+ public void foo() {
+ System.out.println("B");
+ }
+ }
+}