RuntimeAnnotationsForInnerClassTest.java revision 3222:94cfc50c1b8a
1/* 2 * Copyright (c) 2015, 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 WorkAnnotations TestBase TestResult InMemoryFileManager ToolBox 33 * @build TestCase ClassType TestAnnotationInfo 34 * @build RuntimeAnnotationsForInnerClassTest AnnotationsTestBase RuntimeAnnotationsTestBase 35 * @run main RuntimeAnnotationsForInnerClassTest 36 */ 37 38import java.util.ArrayList; 39import java.util.List; 40 41/** 42 * The test checks that RuntimeVisibleAnnotationsAttribute and RuntimeInvisibleAnnotationsAttribute 43 * are generated properly for inner classes, for constructors, for methods, for fields. 44 * The test checks both single and repeatable annotations. In addition, all possible combinations 45 * of retention policies are tested. 46 * 47 * The test generates source code, compiles it and checks the byte code. 48 * 49 * See README.txt for more information. 50 */ 51public class RuntimeAnnotationsForInnerClassTest extends RuntimeAnnotationsTestBase { 52 @Override 53 public List<TestCase> generateTestCases() { 54 List<TestCase> testCases = new ArrayList<>(); 55 for (List<TestAnnotationInfos> groupedAnnotations : groupAnnotations(getAllCombinationsOfAnnotations())) { 56 for (ClassType outerClassType : ClassType.values()) { 57 TestCase test = new TestCase(); 58 TestCase.TestClassInfo outerClazz = test.addClassInfo(outerClassType, "Test"); 59 for (int i = 0; i < groupedAnnotations.size(); ++i) { 60 TestAnnotationInfos annotations = groupedAnnotations.get(i); 61 TestCase.TestClassInfo clazz = outerClazz.addInnerClassInfo(ClassType.CLASS, "InnerClass" + i, "static"); 62 annotations.annotate(clazz); 63 64 TestCase.TestMethodInfo constructor = clazz.addMethodInfo("<init>()"); 65 annotations.annotate(constructor); 66 67 TestCase.TestClassInfo localClassInConstructor = constructor.addLocalClassInfo("Local1" + i); 68 annotations.annotate(localClassInConstructor); 69 70 TestCase.TestMethodInfo innerClazzMethod = clazz.addMethodInfo("innerClassMethod" + i + "()"); 71 annotations.annotate(innerClazzMethod); 72 73 TestCase.TestClassInfo localClassInClassMethod = innerClazzMethod.addLocalClassInfo("Local2" + i); 74 annotations.annotate(localClassInClassMethod); 75 76 TestCase.TestMethodInfo innerStaticClazzMethod = clazz.addMethodInfo("innerStaticClassMethod" + i + "()", "static"); 77 annotations.annotate(innerStaticClazzMethod); 78 79 TestCase.TestClassInfo localClassInStaticMethod = innerStaticClazzMethod.addLocalClassInfo("Local3" + i); 80 annotations.annotate(localClassInStaticMethod); 81 } 82 testCases.add(test); 83 } 84 } 85 return testCases; 86 } 87 88 public static void main(String[] args) throws TestFailedException { 89 new RuntimeAnnotationsForInnerClassTest().test(); 90 } 91} 92