blob: 90ae5bfba6af8f01e2a1c786767061fda4104cb1 [file] [log] [blame]
// Copyright (c) 2024, 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.annotations;
import static com.android.tools.r8.utils.codeinspector.Matchers.isPresentAndRenamed;
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.StringUtils;
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/359385828.
@RunWith(Parameterized.class)
public class NestedAnnotationsUsedTest extends TestBase {
@Parameter(0)
public TestParameters parameters;
@Parameters(name = "{0}")
public static TestParametersCollection data() {
return getTestParameters().withAllRuntimesAndApiLevels().build();
}
private static final String EXPECTED_OUTPUT = StringUtils.lines("1", "2", "3");
@Test
public void testR8() throws Exception {
testForR8(parameters.getBackend())
.addInnerClasses(getClass())
.addKeepClassRules(A.class)
.addKeepRuntimeVisibleAnnotations()
.addKeepMainRule(TestClass.class)
.setMinApi(parameters)
.run(parameters.getRuntime(), TestClass.class)
.inspect(
inspector -> {
assertThat(inspector.clazz(B.class), isPresentAndRenamed());
})
.assertSuccessWithOutput(EXPECTED_OUTPUT);
}
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE})
public @interface A {
B b();
B[] bs() default {};
}
@Retention(RetentionPolicy.RUNTIME)
public @interface B {
String x();
}
@A(
b = @B(x = "1"),
bs = {@B(x = "2"), @B(x = "3")})
static class TestClass {
public static void main(String[] args) throws Exception {
System.out.println(TestClass.class.getAnnotation(A.class).b().x());
for (B b : TestClass.class.getAnnotation(A.class).bs()) {
System.out.println(b.x());
}
}
}
}