Add tests about rewriting metadata on renamed class.
Bug: 147447503, 70169921
Change-Id: I3090994c6aeddd02c441f8129ddba30a51c7632a
diff --git a/src/test/java/com/android/tools/r8/kotlin/metadata/MetadataRewriteInRenamedTypeTest.java b/src/test/java/com/android/tools/r8/kotlin/metadata/MetadataRewriteInRenamedTypeTest.java
new file mode 100644
index 0000000..cd74f9b
--- /dev/null
+++ b/src/test/java/com/android/tools/r8/kotlin/metadata/MetadataRewriteInRenamedTypeTest.java
@@ -0,0 +1,135 @@
+// 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.kotlin.metadata;
+
+import static com.android.tools.r8.KotlinCompilerTool.KOTLINC;
+import static com.android.tools.r8.utils.codeinspector.Matchers.isPresent;
+import static com.android.tools.r8.utils.codeinspector.Matchers.isRenamed;
+import static org.hamcrest.CoreMatchers.equalTo;
+import static org.hamcrest.CoreMatchers.not;
+import static org.hamcrest.MatcherAssert.assertThat;
+
+import com.android.tools.r8.TestParameters;
+import com.android.tools.r8.ToolHelper;
+import com.android.tools.r8.ToolHelper.KotlinTargetVersion;
+import com.android.tools.r8.shaking.ProguardKeepAttributes;
+import com.android.tools.r8.utils.codeinspector.AnnotationSubject;
+import com.android.tools.r8.utils.codeinspector.ClassSubject;
+import com.android.tools.r8.utils.codeinspector.CodeInspector;
+import com.android.tools.r8.utils.codeinspector.KmClassSubject;
+import java.nio.file.Path;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.Map;
+import org.junit.BeforeClass;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.Parameterized;
+
+@RunWith(Parameterized.class)
+public class MetadataRewriteInRenamedTypeTest extends KotlinMetadataTestBase {
+ private static final String OBFUSCATE_RENAMED = "-keep,allowobfuscation class **.Renamed { *; }";
+ private static final String KEEP_KEPT = "-keep class **.Kept { *; }";
+
+ private final TestParameters parameters;
+
+ @Parameterized.Parameters(name = "{0} target: {1}")
+ public static Collection<Object[]> data() {
+ return buildParameters(
+ getTestParameters().withAllRuntimesAndApiLevels().build(), KotlinTargetVersion.values());
+ }
+
+ public MetadataRewriteInRenamedTypeTest(
+ TestParameters parameters, KotlinTargetVersion targetVersion) {
+ super(targetVersion);
+ this.parameters = parameters;
+ }
+
+ private static final Map<KotlinTargetVersion, Path> annoJarMap = new HashMap<>();
+ private static final Map<KotlinTargetVersion, Path> inputJarMap = new HashMap<>();
+
+ @BeforeClass
+ public static void createInputJar() throws Exception {
+ String inputFolder = PKG_PREFIX + "/anno";
+ for (KotlinTargetVersion targetVersion : KotlinTargetVersion.values()) {
+ Path annoJar =
+ kotlinc(KOTLINC, targetVersion)
+ .addSourceFiles(getKotlinFileInTest(inputFolder, "Anno"))
+ .compile();
+ Path inputJar =
+ kotlinc(KOTLINC, targetVersion)
+ .addClasspathFiles(annoJar)
+ .addSourceFiles(getKotlinFileInTest(inputFolder, "main"))
+ .compile();
+ annoJarMap.put(targetVersion, annoJar);
+ inputJarMap.put(targetVersion, inputJar);
+ }
+ }
+
+ @Test
+ public void testR8_kotlinStdlibAsLib() throws Exception {
+ testForR8(parameters.getBackend())
+ .addLibraryFiles(
+ annoJarMap.get(targetVersion),
+ ToolHelper.getJava8RuntimeJar(),
+ ToolHelper.getKotlinStdlibJar())
+ .addProgramFiles(inputJarMap.get(targetVersion))
+ .addKeepRules(OBFUSCATE_RENAMED, KEEP_KEPT)
+ .addKeepAttributes(ProguardKeepAttributes.RUNTIME_VISIBLE_ANNOTATIONS)
+ .compile()
+ .inspect(this::inspect);
+ }
+
+ @Test
+ public void testR8_kotlinStdlibAsClassPath() throws Exception {
+ testForR8(parameters.getBackend())
+ .addClasspathFiles(annoJarMap.get(targetVersion), ToolHelper.getKotlinStdlibJar())
+ .addProgramFiles(inputJarMap.get(targetVersion))
+ .addKeepRules(OBFUSCATE_RENAMED, KEEP_KEPT)
+ .addKeepAttributes(ProguardKeepAttributes.RUNTIME_VISIBLE_ANNOTATIONS)
+ .compile()
+ .inspect(this::inspect);
+ }
+
+ @Test
+ public void testR8_kotlinStdlibAsProgramFile() throws Exception {
+ testForR8(parameters.getBackend())
+ .addProgramFiles(annoJarMap.get(targetVersion), ToolHelper.getKotlinStdlibJar())
+ .addProgramFiles(inputJarMap.get(targetVersion))
+ .addKeepRules(OBFUSCATE_RENAMED, KEEP_KEPT)
+ .addKeepRules("-keep class **.Anno")
+ .addKeepRules("-keep class kotlin.Metadata")
+ .addKeepAttributes(ProguardKeepAttributes.RUNTIME_VISIBLE_ANNOTATIONS)
+ .allowDiagnosticWarningMessages()
+ .compile()
+ .assertWarningMessageThatMatches(
+ equalTo("Resource 'META-INF/MANIFEST.MF' already exists."))
+ .inspect(this::inspect);
+ }
+
+ private void inspect(CodeInspector inspector) {
+ String pkg = getClass().getPackage().getName();
+ ClassSubject kept = inspector.clazz(pkg + ".anno.Kept");
+ assertThat(kept, isPresent());
+ assertThat(kept, not(isRenamed()));
+ // API entry is kept, hence @Metadata exists.
+ KmClassSubject kmClass = kept.getKmClass();
+ assertThat(kmClass, isPresent());
+ // TODO(b/70169921): check if `name` in @Metadata is equal to the (kept) class name.
+ // @Anno is kept.
+ String annoName = pkg + ".anno.Anno";
+ AnnotationSubject anno = kept.annotation(annoName);
+ assertThat(anno, isPresent());
+
+ ClassSubject renamed = inspector.clazz(pkg + ".anno.Renamed");
+ assertThat(renamed, isRenamed());
+ // @Anno is kept.
+ anno = renamed.annotation(annoName);
+ assertThat(anno, isPresent());
+ // TODO(b/147447503): But, R8 treats @Metadata specially.
+ kmClass = renamed.getKmClass();
+ assertThat(kmClass, not(isPresent()));
+ // TODO(b/70169921): check if `name` in @Metadata is equal to the (renamed) class name.
+ }
+}
diff --git a/src/test/java/com/android/tools/r8/kotlin/metadata/anno/Anno.kt b/src/test/java/com/android/tools/r8/kotlin/metadata/anno/Anno.kt
new file mode 100644
index 0000000..3e37cc4
--- /dev/null
+++ b/src/test/java/com/android/tools/r8/kotlin/metadata/anno/Anno.kt
@@ -0,0 +1,7 @@
+// 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.kotlin.metadata.anno
+
+@Retention(AnnotationRetention.RUNTIME)
+annotation class Anno
diff --git a/src/test/java/com/android/tools/r8/kotlin/metadata/anno/main.kt b/src/test/java/com/android/tools/r8/kotlin/metadata/anno/main.kt
new file mode 100644
index 0000000..c2443e5
--- /dev/null
+++ b/src/test/java/com/android/tools/r8/kotlin/metadata/anno/main.kt
@@ -0,0 +1,14 @@
+// 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.kotlin.metadata.anno
+
+@Anno
+class Renamed {
+ var num: Int = 8
+}
+
+@Anno
+class Kept {
+ val answer: Int = 42
+}