RuntimeParameterAnnotationsTest.java revision 3294:9adfb22ff08f
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 RuntimeParameterVisibleAnnotations/RuntimeParameterInvisibleAnnotations 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 * jdk.jdeps/com.sun.tools.javap 32 * @library /tools/lib /tools/javac/lib ../lib 33 * @build WorkAnnotations TestBase TestResult InMemoryFileManager ToolBox 34 * @build TestCase ClassType TestAnnotationInfo 35 * @build RuntimeParameterAnnotationsTest AnnotationsTestBase RuntimeParameterAnnotationsTestBase 36 * @run main RuntimeParameterAnnotationsTest 37 */ 38 39import java.util.ArrayList; 40import java.util.List; 41 42/** 43 * RuntimeParameterAnnotationsTest is a test which checks that RuntimeVisibleParameterAnnotationsAttribute 44 * and RuntimeInvisibleParameterAnnotationsAttribute are generated properly for constructors, 45 * for static and abstract methods of class, for abstract, default and static methods of interface. 46 * The test checks both single and repeatable annotations. 47 * All possible combinations of retention policies are tested. 48 * 49 * The test generates source code, compiles it and checks the byte code. 50 * 51 * See README.txt for more information. 52 */ 53public class RuntimeParameterAnnotationsTest extends RuntimeParameterAnnotationsTestBase { 54 55 @Override 56 public List<TestCase> generateTestCases() { 57 List<TestCase> testCases = new ArrayList<>(); 58 for (List<TestAnnotationInfos> groupedAnnotations : groupAnnotations(getAllCombinationsOfAnnotations())) { 59 for (ClassType classType : new ClassType[]{ClassType.CLASS, ClassType.INTERFACE}) { 60 TestCase test = new TestCase(); 61 for (int i = 0; i < groupedAnnotations.size(); i++) { 62 TestAnnotationInfos annotations = groupedAnnotations.get(i); 63 TestCase.TestClassInfo clazz = test.addClassInfo(classType, "Test" + i, "abstract"); 64 65 initMethod(annotations, clazz, "<init>"); 66 67 initMethod(annotations, clazz, "method1"); 68 69 initMethod(annotations, clazz, "method2", 70 classType == ClassType.CLASS ? "abstract" : "default"); 71 72 initMethod(annotations, clazz, "staticMethod", "static"); 73 } 74 testCases.add(test); 75 } 76 } 77 return testCases; 78 } 79 80 /** 81 * Adds a method to the {@code testCase} with {@code methodName}. 82 * 83 * @param annotations a list of annotations 84 * @param clazz a test class 85 * @param methodName a method name 86 * @param mods an array of modifiers 87 */ 88 private void initMethod(TestAnnotationInfos annotations, TestCase.TestClassInfo clazz, String methodName, String...mods) { 89 String methodDescriptor = methodName + "(int, double, java.lang.String)"; 90 TestCase.TestMethodInfo method = clazz.addMethodInfo(methodDescriptor, mods); 91 TestCase.TestParameterInfo p1 = method.addParameter("int", "a"); 92 annotations.annotate(p1); 93 method.addParameter("double", "b"); 94 TestCase.TestParameterInfo p3 = method.addParameter("String", "c"); 95 annotations.annotate(p3); 96 } 97 98 public static void main(String[] args) throws TestFailedException { 99 new RuntimeParameterAnnotationsTest().test(); 100 } 101} 102