|
| 1 | +/* |
| 2 | + * Copyright (c) 2024, 2025, Oracle and/or its affiliates. All rights reserved. |
| 3 | + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | + * |
| 5 | + * This code is free software; you can redistribute it and/or modify it |
| 6 | + * under the terms of the GNU General Public License version 2 only, as |
| 7 | + * published by the Free Software Foundation. |
| 8 | + * |
| 9 | + * This code is distributed in the hope that it will be useful, but WITHOUT |
| 10 | + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 11 | + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 12 | + * version 2 for more details (a copy is included in the LICENSE file that |
| 13 | + * accompanied this code). |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU General Public License version |
| 16 | + * 2 along with this work; if not, write to the Free Software Foundation, |
| 17 | + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 | + * |
| 19 | + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
| 20 | + * or visit www.oracle.com if you need additional information or have any |
| 21 | + * questions. |
| 22 | + */ |
| 23 | + |
| 24 | +/* |
| 25 | + * @test |
| 26 | + * @bug 8345614 8350704 |
| 27 | + * @summary Ensure behavior with duplicated annotations - class, method, or |
| 28 | + * field fails fast on duplicate annotations, but parameter allows them |
| 29 | + * @library /test/lib |
| 30 | + * @run junit DuplicateAnnotationsTest |
| 31 | + */ |
| 32 | + |
| 33 | +import java.io.IOException; |
| 34 | +import java.lang.annotation.AnnotationFormatError; |
| 35 | +import java.lang.classfile.*; |
| 36 | +import java.lang.classfile.attribute.RuntimeVisibleAnnotationsAttribute; |
| 37 | +import java.lang.classfile.attribute.RuntimeVisibleParameterAnnotationsAttribute; |
| 38 | +import java.lang.constant.ClassDesc; |
| 39 | +import java.lang.reflect.AnnotatedElement; |
| 40 | +import java.nio.file.Path; |
| 41 | +import java.util.Arrays; |
| 42 | +import java.util.List; |
| 43 | + |
| 44 | +import jdk.test.lib.ByteCodeLoader; |
| 45 | +import org.junit.jupiter.api.BeforeAll; |
| 46 | +import org.junit.jupiter.api.function.Executable; |
| 47 | +import org.junit.jupiter.params.ParameterizedTest; |
| 48 | +import org.junit.jupiter.params.provider.Arguments; |
| 49 | +import org.junit.jupiter.params.provider.MethodSource; |
| 50 | + |
| 51 | +import static org.junit.jupiter.api.Assertions.*; |
| 52 | + |
| 53 | +class DuplicateAnnotationsTest { |
| 54 | + static ClassModel cm; |
| 55 | + |
| 56 | + @BeforeAll |
| 57 | + static void setup() throws IOException { |
| 58 | + Path annoDuplicatedClass = Path.of(System.getProperty("test.classes")).resolve("AnnotationDuplicated.class"); |
| 59 | + cm = ClassFile.of().parse(annoDuplicatedClass); |
| 60 | + } |
| 61 | + |
| 62 | + interface Extractor { |
| 63 | + AnnotatedElement find(Class<?> cl) throws ReflectiveOperationException; |
| 64 | + } |
| 65 | + |
| 66 | + // Compiler hint |
| 67 | + static Extractor extract(Extractor e) { |
| 68 | + return e; |
| 69 | + } |
| 70 | + |
| 71 | + static Arguments[] arguments() { |
| 72 | + Annotation annotationOne = Annotation.of(ClassDesc.of("java.lang.Deprecated"), AnnotationElement.ofBoolean("forRemoval", true)); |
| 73 | + Annotation annotationTwo = Annotation.of(ClassDesc.of("java.lang.Deprecated"), AnnotationElement.ofString("since", "24")); |
| 74 | + RuntimeVisibleAnnotationsAttribute rvaa = RuntimeVisibleAnnotationsAttribute.of( |
| 75 | + List.of(annotationOne, annotationTwo) |
| 76 | + ); |
| 77 | + |
| 78 | + return new Arguments[]{ |
| 79 | + Arguments.of( |
| 80 | + "class", true, |
| 81 | + ClassTransform.endHandler(cob -> cob.with(rvaa)), |
| 82 | + extract(c -> c) |
| 83 | + ), |
| 84 | + Arguments.of( |
| 85 | + "field", true, |
| 86 | + ClassTransform.transformingFields(FieldTransform.endHandler(fb -> fb.with(rvaa))), |
| 87 | + extract(c -> c.getDeclaredField("field")) |
| 88 | + ), |
| 89 | + Arguments.of( |
| 90 | + "method", true, |
| 91 | + ClassTransform.transformingMethods(MethodTransform.endHandler(mb -> mb.with(rvaa))), |
| 92 | + extract(c -> c.getDeclaredConstructor(int.class)) |
| 93 | + ), |
| 94 | + Arguments.of( |
| 95 | + "parameter", false, // Surprisingly, parameters always allowed duplicate annotations |
| 96 | + ClassTransform.transformingMethods(MethodTransform.endHandler(mb -> mb.with( |
| 97 | + RuntimeVisibleParameterAnnotationsAttribute.of( |
| 98 | + List.of(List.of(annotationOne, annotationTwo)) |
| 99 | + ) |
| 100 | + ))), |
| 101 | + extract(c -> c.getDeclaredConstructor(int.class).getParameters()[0]) |
| 102 | + ), |
| 103 | + }; |
| 104 | + } |
| 105 | + |
| 106 | + /** |
| 107 | + * A test case represents a declaration that can be annotated. |
| 108 | + * Different declarations have different behaviors when multiple annotations |
| 109 | + * of the same interface are present (without a container annotation). |
| 110 | + * |
| 111 | + * @param caseName the type of declaration, for pretty printing in JUnit |
| 112 | + * @param fails whether this case should fail upon encountering duplicate annotations |
| 113 | + * @param ct transform to install duplicate annotations on the specific declaration |
| 114 | + * @param extractor function to access the AnnotatedElement representing that declaration |
| 115 | + */ |
| 116 | + @MethodSource("arguments") |
| 117 | + @ParameterizedTest |
| 118 | + void test(String caseName, boolean fails, ClassTransform ct, Extractor extractor) throws IOException, ReflectiveOperationException { |
| 119 | + var clazz = ByteCodeLoader.load("AnnotationDuplicated", ClassFile.of().transformClass(cm, ct)); |
| 120 | + var element = assertDoesNotThrow(() -> extractor.find(clazz)); |
| 121 | + Executable exec = () -> element.getAnnotation(Deprecated.class); |
| 122 | + if (fails) { |
| 123 | + var ex = assertThrows(AnnotationFormatError.class, exec, "no duplicate annotation access"); |
| 124 | + assertTrue(ex.getMessage().contains("Deprecated"), () -> "missing problematic annotation: " + ex.getMessage()); |
| 125 | + assertTrue(ex.getMessage().contains("AnnotationDuplicated"), () -> "missing container class: " + ex.getMessage()); |
| 126 | + } else { |
| 127 | + assertDoesNotThrow(exec, "obtaining duplicate annotations should be fine"); |
| 128 | + assertEquals(2, Arrays.stream(element.getAnnotations()) |
| 129 | + .filter(anno -> anno instanceof Deprecated) |
| 130 | + .count()); |
| 131 | + } |
| 132 | + } |
| 133 | +} |
| 134 | + |
| 135 | +// Duplicate annotations on class, field, method (constructor), method parameter |
| 136 | +class AnnotationDuplicated { |
| 137 | + int field; |
| 138 | + |
| 139 | + AnnotationDuplicated(int arg) { |
| 140 | + } |
| 141 | +} |
0 commit comments