Test -if rule behavior in presence of class retention annotations
Bug: b/394275411
Change-Id: I1d1b99d78111daff0b29aaf5337d9148323c8db8
diff --git a/src/test/java/com/android/tools/r8/partial/PartialCompilationIfClassRetentionAnnotationTest.java b/src/test/java/com/android/tools/r8/partial/PartialCompilationIfClassRetentionAnnotationTest.java
new file mode 100644
index 0000000..ccad0ac
--- /dev/null
+++ b/src/test/java/com/android/tools/r8/partial/PartialCompilationIfClassRetentionAnnotationTest.java
@@ -0,0 +1,60 @@
+// Copyright (c) 2025, 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.partial;
+
+import static com.android.tools.r8.utils.codeinspector.Matchers.isAbsent;
+import static org.hamcrest.MatcherAssert.assertThat;
+
+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 java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+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 PartialCompilationIfClassRetentionAnnotationTest extends TestBase {
+
+ @Parameter(0)
+ public TestParameters parameters;
+
+ @Parameters(name = "{0}")
+ public static TestParametersCollection data() {
+ return getTestParameters().withAllRuntimesAndApiLevels().build();
+ }
+
+ @Test
+ public void test() throws Exception {
+ parameters.assumeCanUseR8Partial();
+ testForR8Partial(parameters)
+ .addR8ExcludedClasses(ClassRetentionAnnotation.class, Main.class)
+ .addR8IncludedClasses(Unused.class)
+ .addKeepRules(
+ "-if @" + ClassRetentionAnnotation.class.getTypeName() + " class *",
+ "-keep class " + Unused.class.getTypeName())
+ .compile()
+ .inspect(
+ inspector -> {
+ // TODO(b/394275411): Should be present.
+ ClassSubject unusedClassSubject = inspector.clazz(Unused.class);
+ assertThat(unusedClassSubject, isAbsent());
+ });
+ }
+
+ @Retention(RetentionPolicy.CLASS)
+ @interface ClassRetentionAnnotation {}
+
+ @ClassRetentionAnnotation
+ static class Main {
+
+ public static void main(String[] args) {}
+ }
+
+ static class Unused {}
+}
diff --git a/src/test/testbase/java/com/android/tools/r8/TestBase.java b/src/test/testbase/java/com/android/tools/r8/TestBase.java
index 7e7b4b6..a1b39e9 100644
--- a/src/test/testbase/java/com/android/tools/r8/TestBase.java
+++ b/src/test/testbase/java/com/android/tools/r8/TestBase.java
@@ -255,6 +255,10 @@
.applyIf(parameters.isRandom(), R8PartialTestBuilder::allowUnusedDontWarnPatterns);
}
+ public R8PartialTestBuilder testForR8Partial(TestParameters parameters) {
+ return testForR8Partial(parameters.getBackend()).setMinApi(parameters);
+ }
+
public R8PartialTestBuilder testForR8Partial(Backend backend) {
return testForR8Partial(temp, backend);
}