Test Record and annotations
Bug: 199359249
Change-Id: I2d86a773cf22a2b4e1680d2c962331a5d5e7a461
diff --git a/src/test/examplesJava16/records/EmptyRecordAnnotation.java b/src/test/examplesJava16/records/EmptyRecordAnnotation.java
new file mode 100644
index 0000000..7236a66
--- /dev/null
+++ b/src/test/examplesJava16/records/EmptyRecordAnnotation.java
@@ -0,0 +1,39 @@
+// 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 records;
+
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+
+public class EmptyRecordAnnotation {
+
+ record Empty() {}
+
+ @Retention(RetentionPolicy.RUNTIME)
+ @interface ClassAnnotation {
+ Class<? extends Record> theClass();
+ }
+
+ @ClassAnnotation(theClass = Record.class)
+ public static void annotatedMethod1() {}
+
+ @ClassAnnotation(theClass = Empty.class)
+ public static void annotatedMethod2() {}
+
+ public static void main(String[] args) throws Exception {
+ Class<?> annotatedMethod1Content =
+ EmptyRecordAnnotation.class
+ .getDeclaredMethod("annotatedMethod1")
+ .getAnnotation(ClassAnnotation.class)
+ .theClass();
+ System.out.println(annotatedMethod1Content);
+ Class<?> annotatedMethod2Content =
+ EmptyRecordAnnotation.class
+ .getDeclaredMethod("annotatedMethod2")
+ .getAnnotation(ClassAnnotation.class)
+ .theClass();
+ System.out.println(annotatedMethod2Content);
+ }
+}
diff --git a/src/test/java/com/android/tools/r8/desugar/records/EmptyRecordAnnotationTest.java b/src/test/java/com/android/tools/r8/desugar/records/EmptyRecordAnnotationTest.java
new file mode 100644
index 0000000..a9ed3bd
--- /dev/null
+++ b/src/test/java/com/android/tools/r8/desugar/records/EmptyRecordAnnotationTest.java
@@ -0,0 +1,98 @@
+// 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.desugar.records;
+
+import static com.android.tools.r8.desugar.records.RecordTestUtils.RECORD_KEEP_RULE;
+import static com.android.tools.r8.utils.InternalOptions.TestingOptions;
+
+import com.android.tools.r8.TestBase;
+import com.android.tools.r8.TestParameters;
+import com.android.tools.r8.TestRuntime.CfRuntime;
+import com.android.tools.r8.utils.StringUtils;
+import java.util.List;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.Parameterized;
+
+@RunWith(Parameterized.class)
+public class EmptyRecordAnnotationTest extends TestBase {
+
+ private static final String RECORD_NAME = "EmptyRecordAnnotation";
+ private static final byte[][] PROGRAM_DATA = RecordTestUtils.getProgramData(RECORD_NAME);
+ private static final String MAIN_TYPE = RecordTestUtils.getMainType(RECORD_NAME);
+ private static final String EXPECTED_RESULT_CF =
+ StringUtils.lines("class java.lang.Record", "class records.EmptyRecordAnnotation$Empty");
+ private static final String EXPECTED_RESULT_DEX =
+ StringUtils.lines(
+ "class com.android.tools.r8.RecordTag", "class records.EmptyRecordAnnotation$Empty");
+
+ private final TestParameters parameters;
+
+ public EmptyRecordAnnotationTest(TestParameters parameters) {
+ this.parameters = parameters;
+ }
+
+ @Parameterized.Parameters(name = "{0}")
+ public static List<Object[]> data() {
+ // TODO(b/174431251): This should be replaced with .withCfRuntimes(start = jdk16).
+ return buildParameters(
+ getTestParameters()
+ .withCustomRuntime(CfRuntime.getCheckedInJdk16())
+ .withDexRuntimes()
+ .withAllApiLevelsAlsoForCf()
+ .build());
+ }
+
+ @Test
+ public void testD8AndJvm() throws Exception {
+ if (parameters.isCfRuntime()) {
+ testForJvm()
+ .addProgramClassFileData(PROGRAM_DATA)
+ .enablePreview()
+ .run(parameters.getRuntime(), MAIN_TYPE)
+ .assertSuccessWithOutput(EXPECTED_RESULT_CF);
+ }
+ testForD8(parameters.getBackend())
+ .addProgramClassFileData(PROGRAM_DATA)
+ .setMinApi(parameters.getApiLevel())
+ .addOptionsModification(TestingOptions::allowExperimentClassFileVersion)
+ .compile()
+ .run(parameters.getRuntime(), MAIN_TYPE)
+ .assertSuccessWithOutput(EXPECTED_RESULT_DEX);
+ }
+
+ @Test
+ public void testR8() throws Exception {
+ if (parameters.isCfRuntime()) {
+ testForR8(parameters.getBackend())
+ .addProgramClassFileData(PROGRAM_DATA)
+ .setMinApi(parameters.getApiLevel())
+ .addKeepRules("-keepattributes *Annotation*")
+ .addKeepRules("-keep class records.EmptyRecordAnnotation { *; }")
+ .addKeepRules(RECORD_KEEP_RULE)
+ .addKeepMainRule(MAIN_TYPE)
+ .addLibraryFiles(RecordTestUtils.getJdk15LibraryFiles(temp))
+ .addOptionsModification(TestingOptions::allowExperimentClassFileVersion)
+ .compile()
+ .inspect(RecordTestUtils::assertRecordsAreRecords)
+ .enableJVMPreview()
+ .run(parameters.getRuntime(), MAIN_TYPE)
+ .assertSuccessWithOutput(EXPECTED_RESULT_CF);
+ return;
+ }
+ testForR8(parameters.getBackend())
+ .addProgramClassFileData(PROGRAM_DATA)
+ .setMinApi(parameters.getApiLevel())
+ .addKeepRules("-keepattributes *Annotation*")
+ .addKeepRules("-keep class records.EmptyRecordAnnotation { *; }")
+ .addKeepRules("-keep class java.lang.Record")
+ .addKeepRules(RECORD_KEEP_RULE)
+ .addKeepMainRule(MAIN_TYPE)
+ .addOptionsModification(TestingOptions::allowExperimentClassFileVersion)
+ .compile()
+ .run(parameters.getRuntime(), MAIN_TYPE)
+ .assertSuccessWithOutput(EXPECTED_RESULT_DEX);
+ }
+}