Add test for not repackaging if call to Array.clone
Bug: 197482897
Change-Id: Id31c023186e0265e69f4c4ffc85d149a3d7d8acc
diff --git a/src/test/java/com/android/tools/r8/repackage/RepackageCloneTest.java b/src/test/java/com/android/tools/r8/repackage/RepackageCloneTest.java
new file mode 100644
index 0000000..7c138ee
--- /dev/null
+++ b/src/test/java/com/android/tools/r8/repackage/RepackageCloneTest.java
@@ -0,0 +1,86 @@
+// Copyright (c) 2021, 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.repackage;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+
+import com.android.tools.r8.NeverInline;
+import com.android.tools.r8.NoHorizontalClassMerging;
+import com.android.tools.r8.TestParameters;
+import com.android.tools.r8.utils.codeinspector.HorizontallyMergedClassesInspector;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.Parameterized;
+
+@RunWith(Parameterized.class)
+public class RepackageCloneTest extends RepackageTestBase {
+
+ public RepackageCloneTest(
+ String flattenPackageHierarchyOrRepackageClasses, TestParameters parameters) {
+ super(flattenPackageHierarchyOrRepackageClasses, parameters);
+ }
+
+ @Test
+ public void testClone() throws Exception {
+ testForR8Compat(parameters.getBackend())
+ .addProgramClasses(A.class, B.class, C.class, Main.class)
+ .addKeepMainRule(Main.class)
+ .addKeepClassRulesWithAllowObfuscation(A.class)
+ // Ensure we keep values() which has a call to clone.
+ .addKeepRules("-keepclassmembers class " + typeName(A.class) + " { *; }")
+ .apply(this::configureRepackaging)
+ .setMinApi(parameters.getApiLevel())
+ .addHorizontallyMergedClassesInspector(
+ HorizontallyMergedClassesInspector::assertNoClassesMerged)
+ .enableInliningAnnotations()
+ .enableNoHorizontalClassMergingAnnotations()
+ .compile()
+ .inspect(
+ inspector -> {
+ // TODO(b/197482897) This should be repackaged.
+ assertThat(A.class, isNotRepackaged(inspector));
+ assertThat(B.class, isRepackaged(inspector));
+ assertThat(C.class, isNotRepackaged(inspector));
+ })
+ .run(parameters.getRuntime(), Main.class)
+ .assertSuccessWithOutputLines("foo", "null");
+ }
+
+ public enum A {
+ foo;
+ }
+
+ @NoHorizontalClassMerging
+ public static class B {
+
+ @NeverInline
+ public static void foo() {
+ try {
+ new B().clone();
+ } catch (CloneNotSupportedException e) {
+ e.printStackTrace();
+ }
+ }
+ }
+
+ @NoHorizontalClassMerging
+ public static class C {
+
+ @NeverInline
+ public static void foo() {
+ System.out.println(new A[10].clone()[1]);
+ ;
+ }
+ }
+
+ public static class Main {
+
+ public static void main(String[] args) {
+ System.out.println(A.foo);
+ B.foo();
+ C.foo();
+ }
+ }
+}