Reproduce b/123068484: no field rebinding due to visibility restriction.
Bug: 123068484
Change-Id: Idb54ee4f9b1366335e3670aceb51e506d656874e
diff --git a/src/test/java/com/android/tools/r8/TestBase.java b/src/test/java/com/android/tools/r8/TestBase.java
index 7493eba..2b66df4 100644
--- a/src/test/java/com/android/tools/r8/TestBase.java
+++ b/src/test/java/com/android/tools/r8/TestBase.java
@@ -50,10 +50,13 @@
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
+import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.ArrayList;
import java.util.Arrays;
+import java.util.Collection;
import java.util.Collections;
+import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.function.Consumer;
@@ -916,6 +919,15 @@
}
}
+ protected static void writeToJar(Path output, Collection<Path> classes) throws IOException {
+ List<byte[]> bytes = new LinkedList<>();
+ for (Path classPath : classes) {
+ byte[] classBytes = Files.readAllBytes(Paths.get(classPath.toString()));
+ bytes.add(classBytes);
+ }
+ writeToJar(output, bytes);
+ }
+
protected Path writeToJar(List<byte[]> classes) throws IOException {
Path result = File.createTempFile("junit", ".jar", temp.getRoot()).toPath();
writeToJar(result, classes);
diff --git a/src/test/java/com/android/tools/r8/naming/b123068484/FieldRenamingTest.java b/src/test/java/com/android/tools/r8/naming/b123068484/FieldRenamingTest.java
new file mode 100644
index 0000000..d0aef41
--- /dev/null
+++ b/src/test/java/com/android/tools/r8/naming/b123068484/FieldRenamingTest.java
@@ -0,0 +1,92 @@
+// Copyright (c) 2019, 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.naming.b123068484;
+
+import static com.android.tools.r8.utils.codeinspector.Matchers.isPresent;
+import static org.junit.Assert.assertNotEquals;
+import static org.junit.Assert.assertThat;
+
+import com.android.tools.r8.TestBase;
+import com.android.tools.r8.ToolHelper;
+import com.android.tools.r8.naming.b123068484.data.Concrete1;
+import com.android.tools.r8.naming.b123068484.runner.Runner;
+import com.android.tools.r8.utils.StringUtils;
+import com.android.tools.r8.utils.codeinspector.ClassSubject;
+import com.android.tools.r8.utils.codeinspector.CodeInspector;
+import com.android.tools.r8.utils.codeinspector.InstructionSubject;
+import com.android.tools.r8.utils.codeinspector.MethodSubject;
+import java.nio.file.Path;
+import java.util.LinkedList;
+import java.util.List;
+import org.junit.BeforeClass;
+import org.junit.Ignore;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.Parameterized;
+
+@RunWith(Parameterized.class)
+public class FieldRenamingTest extends TestBase {
+ private static final Class<?> MAIN = Runner.class;
+ private static final Class<?> CONCRETE1 = Concrete1.class;
+ private static List<Path> CLASSES;
+ private static final String EXPECTED_OUTPUT = StringUtils.lines("Runner");
+
+ private final Backend backend;
+
+ @Parameterized.Parameters(name = "Backend: {0}")
+ public static Object[] data() {
+ return Backend.values();
+ }
+
+ public FieldRenamingTest(Backend backend) {
+ this.backend = backend;
+ }
+
+ @BeforeClass
+ public static void setUpClass() throws Exception {
+ CLASSES = new LinkedList<>();
+ CLASSES.addAll(ToolHelper.getClassFilesForTestPackage(MAIN.getPackage()));
+ CLASSES.addAll(ToolHelper.getClassFilesForTestPackage(CONCRETE1.getPackage()));
+ }
+
+ @Test
+ public void testProguard() throws Exception {
+ Path inJar = temp.newFile("input.jar").toPath().toAbsolutePath();
+ writeToJar(inJar, CLASSES);
+ testForProguard()
+ .addProgramFiles(inJar)
+ .addKeepMainRule(MAIN)
+ .compile()
+ .inspect(this::inspect)
+ .run(MAIN)
+ .assertSuccessWithOutput(EXPECTED_OUTPUT);
+ }
+
+ @Ignore("b/123068484")
+ @Test
+ public void testR8() throws Exception {
+ testForR8(backend)
+ .addProgramFiles(CLASSES)
+ .addKeepMainRule(MAIN)
+ .compile()
+ .inspect(this::inspect)
+ .run(MAIN)
+ .assertSuccessWithOutput(EXPECTED_OUTPUT);
+ }
+
+ private void inspect(CodeInspector inspector) {
+ ClassSubject main = inspector.clazz(MAIN);
+ assertThat(main, isPresent());
+ MethodSubject methodSubject = main.mainMethod();
+ assertThat(methodSubject, isPresent());
+
+ methodSubject
+ .iterateInstructions(InstructionSubject::isInstanceGet)
+ .forEachRemaining(instructionSubject -> {
+ String fieldName = instructionSubject.getField().name.toString();
+ assertNotEquals("strField", fieldName);
+ });
+ }
+
+}
diff --git a/src/test/java/com/android/tools/r8/naming/b123068484/data/Abs.java b/src/test/java/com/android/tools/r8/naming/b123068484/data/Abs.java
new file mode 100644
index 0000000..d40899a
--- /dev/null
+++ b/src/test/java/com/android/tools/r8/naming/b123068484/data/Abs.java
@@ -0,0 +1,13 @@
+// Copyright (c) 2019, 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.naming.b123068484.data;
+
+// Hide the strField via class-level access modifier.
+abstract class Abs extends PublicAbs {
+ public final String strField;
+
+ Abs(String x) {
+ strField = x;
+ }
+}
diff --git a/src/test/java/com/android/tools/r8/naming/b123068484/data/Concrete1.java b/src/test/java/com/android/tools/r8/naming/b123068484/data/Concrete1.java
new file mode 100644
index 0000000..02ce3b6
--- /dev/null
+++ b/src/test/java/com/android/tools/r8/naming/b123068484/data/Concrete1.java
@@ -0,0 +1,12 @@
+// Copyright (c) 2019, 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.naming.b123068484.data;
+
+public class Concrete1 extends Abs {
+ Object otherField;
+ public Concrete1(String x) {
+ super(x);
+ otherField = new Throwable("Concrete1::<init>");
+ }
+}
diff --git a/src/test/java/com/android/tools/r8/naming/b123068484/data/Concrete2.java b/src/test/java/com/android/tools/r8/naming/b123068484/data/Concrete2.java
new file mode 100644
index 0000000..55e8dce
--- /dev/null
+++ b/src/test/java/com/android/tools/r8/naming/b123068484/data/Concrete2.java
@@ -0,0 +1,12 @@
+// Copyright (c) 2019, 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.naming.b123068484.data;
+
+public class Concrete2 extends Abs {
+ Object otherField;
+ public Concrete2(String x) {
+ super(x);
+ otherField = new Exception("Concrete2::<init>");
+ }
+}
diff --git a/src/test/java/com/android/tools/r8/naming/b123068484/data/PublicAbs.java b/src/test/java/com/android/tools/r8/naming/b123068484/data/PublicAbs.java
new file mode 100644
index 0000000..d7d0e45
--- /dev/null
+++ b/src/test/java/com/android/tools/r8/naming/b123068484/data/PublicAbs.java
@@ -0,0 +1,9 @@
+// Copyright (c) 2019, 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.naming.b123068484.data;
+
+// Just to abstract Concrete(1|2) and make them visible from different packages.
+public abstract class PublicAbs {
+ static Object foo;
+}
diff --git a/src/test/java/com/android/tools/r8/naming/b123068484/runner/Runner.java b/src/test/java/com/android/tools/r8/naming/b123068484/runner/Runner.java
new file mode 100644
index 0000000..84062c9
--- /dev/null
+++ b/src/test/java/com/android/tools/r8/naming/b123068484/runner/Runner.java
@@ -0,0 +1,28 @@
+// Copyright (c) 2019, 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.naming.b123068484.runner;
+
+import com.android.tools.r8.naming.b123068484.data.PublicAbs;
+import com.android.tools.r8.naming.b123068484.data.Concrete1;
+import com.android.tools.r8.naming.b123068484.data.Concrete2;
+
+public class Runner {
+ private static int counter = 0;
+ static PublicAbs create(String x) {
+ if (counter++ % 2 == 0) {
+ return new Concrete1(x);
+ } else {
+ return new Concrete2(x);
+ }
+ }
+
+ public static void main(String[] args) {
+ PublicAbs instance = create("Runner");
+ if (instance instanceof Concrete1) {
+ System.out.println(((Concrete1) instance).strField);
+ } else {
+ System.out.println(((Concrete2) instance).strField);
+ }
+ }
+}