Add test and fix for abstract class implementing interface
Bug: 136180822
Change-Id: I94e8bc7ba8384c35d9a930bb82252219ed327b32
diff --git a/src/main/java/com/android/tools/r8/naming/ProguardMapMinifier.java b/src/main/java/com/android/tools/r8/naming/ProguardMapMinifier.java
index bca6f1f..e25d81e 100644
--- a/src/main/java/com/android/tools/r8/naming/ProguardMapMinifier.java
+++ b/src/main/java/com/android/tools/r8/naming/ProguardMapMinifier.java
@@ -208,18 +208,23 @@
.dexItemFactory()
.createMethod(type, parentReference.proto, parentReference.name);
addMemberNaming(
- key, parentReferenceOnCurrentType, parentMembers, additionalMethodNamings);
+ parentReferenceOnCurrentType, parentMembers.get(key), additionalMethodNamings);
} else {
DexField parentReference = key.asDexField();
DexField parentReferenceOnCurrentType =
appView
.dexItemFactory()
.createField(type, parentReference.type, parentReference.name);
- addMemberNaming(key, parentReferenceOnCurrentType, parentMembers, additionalFieldNamings);
+ addMemberNaming(
+ parentReferenceOnCurrentType, parentMembers.get(key), additionalFieldNamings);
}
}
}
+ // We do not visit interfaces, but their members can also be targeted on implementing abstract
+ // classes, so we have to build their names up as well.
+ addNonPrivateInterfaceMappings(dexClass, nonPrivateMembers);
+
if (nonPrivateMembers.size() > 0) {
buildUpNames.addLast(nonPrivateMembers);
appView
@@ -233,16 +238,40 @@
}
}
+ private void addNonPrivateInterfaceMappings(
+ DexClass dexClass, Map<DexReference, MemberNaming> nonPrivateMembers) {
+ if (dexClass != null && dexClass.isAbstract()) {
+ for (DexType value : dexClass.interfaces.values) {
+ ClassNamingForMapApplier interfaceNaming = seedMapper.getClassNaming(value);
+ if (interfaceNaming == null) {
+ continue;
+ }
+ interfaceNaming.forAllMemberNaming(
+ memberNaming -> {
+ Signature signature = memberNaming.getOriginalSignature();
+ assert !signature.isQualified();
+ if (signature instanceof MethodSignature) {
+ DexMethod member =
+ ((MethodSignature) signature)
+ .toDexMethod(appView.dexItemFactory(), dexClass.type);
+ addMemberNaming(member, memberNaming, additionalMethodNamings);
+ } else {
+ DexField member =
+ ((FieldSignature) signature)
+ .toDexField(appView.dexItemFactory(), dexClass.type);
+ addMemberNaming(member, memberNaming, additionalFieldNamings);
+ }
+ });
+ }
+ }
+ }
+
private <T extends DexReference> void addMemberNaming(
- DexReference key,
- T member,
- Map<DexReference, MemberNaming> parentMembers,
- Map<T, DexString> additionalMemberNamings) {
+ T member, MemberNaming memberNaming, Map<T, DexString> additionalMemberNamings) {
// We might have overridden a naming in the direct class namings above.
if (!memberNames.containsKey(member)) {
- DexString renamedName =
- appView.dexItemFactory().createString(parentMembers.get(key).getRenamedName());
- memberNames.put(member, parentMembers.get(key));
+ DexString renamedName = appView.dexItemFactory().createString(memberNaming.getRenamedName());
+ memberNames.put(member, memberNaming);
additionalMemberNamings.put(member, renamedName);
}
}
diff --git a/src/test/java/com/android/tools/r8/naming/applymapping/ApplyMappingInterfaceInvokeTest.java b/src/test/java/com/android/tools/r8/naming/applymapping/ApplyMappingInterfaceInvokeTest.java
new file mode 100644
index 0000000..a1ef87f
--- /dev/null
+++ b/src/test/java/com/android/tools/r8/naming/applymapping/ApplyMappingInterfaceInvokeTest.java
@@ -0,0 +1,89 @@
+// 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.applymapping;
+
+import com.android.tools.r8.CompilationFailedException;
+import com.android.tools.r8.R8TestCompileResult;
+import com.android.tools.r8.TestBase;
+import com.android.tools.r8.TestParameters;
+import com.android.tools.r8.TestParametersCollection;
+import java.io.IOException;
+import java.util.concurrent.ExecutionException;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.Parameterized;
+import org.junit.runners.Parameterized.Parameters;
+
+@RunWith(Parameterized.class)
+public class ApplyMappingInterfaceInvokeTest extends TestBase {
+
+ public interface I {
+ void foo();
+ }
+
+ // Keep this class to test A.foo();
+ public abstract static class A implements I {}
+
+ public static class B extends A {
+
+ @Override
+ public void foo() {
+ System.out.println("Hello World!");
+ }
+ }
+
+ // Keep this class to test C.foo();
+ public static class C extends B {}
+
+ public static class TestApp {
+
+ public static void main(String[] args) {
+ testA(new B());
+ testC(new C());
+ }
+
+ public static void testA(A a) {
+ a.foo();
+ }
+
+ public static void testC(C c) {
+ c.foo();
+ }
+ }
+
+ private final TestParameters parameters;
+
+ @Parameters(name = "{0}")
+ public static TestParametersCollection data() {
+ return getTestParameters().withAllRuntimes().build();
+ }
+
+ public ApplyMappingInterfaceInvokeTest(TestParameters parameters) {
+ this.parameters = parameters;
+ }
+
+ @Test
+ public void testInvokeVirtual()
+ throws IOException, CompilationFailedException, ExecutionException {
+ Class<?>[] classPathClasses = {I.class, A.class, B.class, C.class};
+ R8TestCompileResult libraryResult =
+ testForR8(parameters.getBackend())
+ .addProgramClasses(classPathClasses)
+ .addKeepAllClassesRuleWithAllowObfuscation()
+ .setMinApi(parameters.getRuntime())
+ .compile();
+ testForR8(parameters.getBackend())
+ .addClasspathClasses(classPathClasses)
+ .addProgramClasses(TestApp.class)
+ .noMinification()
+ .noTreeShaking()
+ .addApplyMapping(libraryResult.getProguardMap())
+ .setMinApi(parameters.getRuntime())
+ .compile()
+ .addRunClasspathFiles(libraryResult.writeToZip())
+ .run(parameters.getRuntime(), TestApp.class)
+ .assertSuccessWithOutputLines("Hello World!", "Hello World!");
+ }
+}