Add test for rewriting method parameters with missing annotations
Bug: b/235733922
Change-Id: Iafe5643ae12cc18154df89345d7dd05b4a84d96d
diff --git a/src/test/java/com/android/tools/r8/optimize/proto/ProtoNormalizationEnumParameterTest.java b/src/test/java/com/android/tools/r8/optimize/proto/ProtoNormalizationEnumParameterTest.java
new file mode 100644
index 0000000..05bc075
--- /dev/null
+++ b/src/test/java/com/android/tools/r8/optimize/proto/ProtoNormalizationEnumParameterTest.java
@@ -0,0 +1,90 @@
+// Copyright (c) 2022, 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.optimize.proto;
+
+import static org.junit.Assert.assertThrows;
+
+import com.android.tools.r8.CompilationFailedException;
+import com.android.tools.r8.DiagnosticsMatcher;
+import com.android.tools.r8.NeverInline;
+import com.android.tools.r8.TestBase;
+import com.android.tools.r8.TestParameters;
+import com.android.tools.r8.TestParametersCollection;
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+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;
+
+/** This is a regression test for b/235733922 */
+@RunWith(Parameterized.class)
+public class ProtoNormalizationEnumParameterTest extends TestBase {
+
+ @Parameter() public TestParameters parameters;
+
+ @Parameters(name = "{0}")
+ public static TestParametersCollection data() {
+ return getTestParameters().withAllRuntimesAndApiLevels().build();
+ }
+
+ @Test
+ public void testR8() throws Exception {
+ assertThrows(
+ CompilationFailedException.class,
+ () ->
+ testForR8Compat(parameters.getBackend())
+ .addInnerClasses(getClass())
+ .setMinApi(parameters.getApiLevel())
+ .addKeepClassAndMembersRules(Main.class)
+ .addKeepClassRules(CustomAnnotation.class)
+ .addKeepRuntimeVisibleParameterAnnotations()
+ .enableInliningAnnotations()
+ // TODO(b/b/235733922): Should not throw an NPE.
+ .compileWithExpectedDiagnostics(
+ diagnostics ->
+ diagnostics.assertErrorThatMatches(
+ DiagnosticsMatcher.diagnosticException(NullPointerException.class))));
+ }
+
+ @Retention(RetentionPolicy.RUNTIME)
+ @Target(ElementType.PARAMETER)
+ public @interface CustomAnnotation {}
+
+ public enum MyEnum {
+ TEST_1("Foo"),
+ TEST_2("Bar");
+
+ private final String str;
+
+ MyEnum(@CustomAnnotation String str) {
+ this.str = str;
+ }
+
+ public String getStr() {
+ return TEST_1 + str;
+ }
+ }
+
+ public static class Main {
+
+ @NeverInline
+ // The test(int foo, String bar, String baz) is needed to have
+ // MyEnum.<init>(String name, int ordinal, String str) written into this proto.
+ public static void test(int foo, String bar, String baz) {
+ System.out.println(foo + bar + baz);
+ }
+
+ @NeverInline
+ public static void main(String[] args) {
+ test(args.length, args[0], args[1]);
+ MyEnum val = System.currentTimeMillis() > 0 ? MyEnum.TEST_1 : MyEnum.TEST_2;
+ System.out.println(val);
+ }
+ }
+}