1 /*
2  * Copyright (c) 2015, 2016, 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 8044411
27  * @summary Tests the RuntimeVisibleAnnotations/RuntimeInvisibleAnnotations attribute.
28  * @modules jdk.jdeps/com.sun.tools.classfile
29  *          jdk.compiler/com.sun.tools.javac.api
30  *          jdk.compiler/com.sun.tools.javac.main
31  * @library /tools/lib /tools/javac/lib ../lib
32  * @build toolbox.ToolBox InMemoryFileManager TestResult TestBase
33  * @build WorkAnnotations TestCase ClassType TestAnnotationInfo
34  * @build RuntimeAnnotationsForTopLevelClassTest AnnotationsTestBase RuntimeAnnotationsTestBase
35  * @run main RuntimeAnnotationsForTopLevelClassTest
36  */
37 
38 import java.util.ArrayList;
39 import java.util.List;
40 
41 /**
42  * The test checks that RuntimeVisibleAnnotationsAttribute and RuntimeInvisibleAnnotationsAttribute
43  * are generated properly for top-level class (class, enum, annotation, interface),
44  * for constructors (in enum and in class), for methods (abstract methods, static and default methods in interface),
45  * for fields. The test checks both single and repeatable annotations.
46  * In addition, all possible combinations of retention policies are tested.
47  *
48  * The test generates source code, compiles it and checks the byte code.
49  *
50  * See README.txt for more information.
51  */
52 public class RuntimeAnnotationsForTopLevelClassTest extends RuntimeAnnotationsTestBase {
53 
54     @Override
generateTestCases()55     public List<TestCase> generateTestCases() {
56         List<TestCase> testCases = new ArrayList<>();
57         for (List<TestAnnotationInfos> groupedAnnotations : groupAnnotations(getAllCombinationsOfAnnotations())) {
58             for (ClassType classType : ClassType.values()) {
59                 TestCase test = new TestCase();
60                 for (int i = 0; i < groupedAnnotations.size(); ++i) {
61                     TestAnnotationInfos annotations = groupedAnnotations.get(i);
62                     TestCase.TestClassInfo clazz = test.addClassInfo(classType, "Test" + i);
63                     annotations.annotate(clazz);
64 
65                     if (classType != ClassType.ENUM) {
66                         TestCase.TestMethodInfo constructor = clazz.addMethodInfo("<init>()");
67                         annotations.annotate(constructor);
68 
69                         TestCase.TestClassInfo localClass = constructor.addLocalClassInfo("Local1" + i);
70                         annotations.annotate(localClass);
71                     }
72                     if (classType != ClassType.ANNOTATION) {
73                         TestCase.TestMethodInfo staticClassMethod = clazz.addMethodInfo("staticClassMethod" + i + "()", "static");
74                         annotations.annotate(staticClassMethod);
75 
76                         TestCase.TestClassInfo localClassInStaticMethod = staticClassMethod.addLocalClassInfo("Local2" + i);
77                         annotations.annotate(localClassInStaticMethod);
78                     }
79                     TestCase.TestMethodInfo classMethod = clazz.addMethodInfo("classMethod" + i + "()");
80                     annotations.annotate(classMethod);
81 
82                     TestCase.TestClassInfo localClassInClassMethod = classMethod.addLocalClassInfo("Local3" + i);
83                     annotations.annotate(localClassInClassMethod);
84 
85                     TestCase.TestFieldInfo field = clazz.addFieldInfo("field" + i);
86                     annotations.annotate(field);
87 
88                     TestCase.TestFieldInfo staticField = clazz.addFieldInfo("staticField" + i, "static");
89                     annotations.annotate(staticField);
90                 }
91                 testCases.add(test);
92             }
93         }
94         return testCases;
95     }
96 
main(String[] args)97     public static void main(String[] args) throws TestFailedException {
98         new RuntimeAnnotationsForTopLevelClassTest().test();
99     }
100 }
101