RuntimeParameterAnnotationsForLambdaTest.java revision 3086:16d09290bb2a
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 8079060 8138612 27 * @summary Tests the RuntimeParameterVisibleAnnotations/RuntimeParameterInvisibleAnnotations attribute. 28 * @library /tools/lib /tools/javac/lib ../lib 29 * @build WorkAnnotations TestBase TestResult InMemoryFileManager ToolBox 30 * @build TestCase ClassType TestAnnotationInfo 31 * @build RuntimeParameterAnnotationsForLambdaTest AnnotationsTestBase RuntimeParameterAnnotationsTestBase 32 * @run main RuntimeParameterAnnotationsForLambdaTest 33 */ 34 35import java.util.List; 36import java.util.stream.Collectors; 37 38import com.sun.tools.classfile.*; 39 40/** 41 * RuntimeParameterAnnotationsForLambdaTest is a test which checks that RuntimeVisibleParameterAnnotationsAttribute 42 * and RuntimeInvisibleParameterAnnotationsAttribute are not generated at all for lambda expressions. 43 * The test checks both single and repeatable annotations. 44 * All possible combinations of retention policies are tested. 45 * 46 * The test generates source code, compiles it and checks the byte code. 47 * 48 * See README.txt for more information. 49 */ 50public class RuntimeParameterAnnotationsForLambdaTest extends RuntimeParameterAnnotationsTestBase { 51 52 private static final String CLASS_NAME = "Test"; 53 private static final String SOURCE_TEMPLATE = 54 "public class " + CLASS_NAME + " {\n" + 55 " interface I { void method(int a, double b, String c); }\n" + 56 " %SOURCE%\n" + 57 "}"; 58 59 public static void main(String[] args) throws TestFailedException { 60 new RuntimeParameterAnnotationsForLambdaTest().test(); 61 } 62 63 @Override 64 public void test() throws TestFailedException { 65 try { 66 for (TestAnnotationInfos annotations : getAllCombinationsOfAnnotations()) { 67 try { 68 TestCase.TestMethodInfo testMethodInfo = new TestCase.TestMethodInfo(0, null, "lambda", false, false); 69 TestCase.TestParameterInfo p1 = testMethodInfo.addParameter("int", "a"); 70 annotations.annotate(p1); 71 testMethodInfo.addParameter("double", "b"); 72 TestCase.TestParameterInfo p3 = testMethodInfo.addParameter("String", "c"); 73 annotations.annotate(p3); 74 String source = SOURCE_TEMPLATE.replace("%SOURCE%", generateLambdaSource(testMethodInfo)); 75 addTestCase(source); 76 echo("Testing:\n" + source); 77 ClassFile classFile = readClassFile(compile(source).getClasses().get(CLASS_NAME)); 78 boolean isFoundLambda = false; 79 for (Method method : classFile.methods) { 80 if (method.getName(classFile.constant_pool).startsWith("lambda$")) { 81 isFoundLambda = true; 82 testAttributes(testMethodInfo, classFile, method); 83 } 84 } 85 checkTrue(isFoundLambda, "The tested lambda method was not found."); 86 } catch (Exception e) { 87 addFailure(e); 88 } 89 } 90 } finally { 91 checkStatus(); 92 } 93 } 94 95 protected void testAttributes( 96 TestCase.TestMethodInfo testMethod, 97 ClassFile classFile, 98 Method method) throws ConstantPoolException { 99 Attributes attributes = method.attributes; 100 RuntimeParameterAnnotations_attribute attr = (RuntimeParameterAnnotations_attribute) attributes.get(Attribute.RuntimeInvisibleParameterAnnotations); 101 checkNull(attr, String.format("%s should be null", Attribute.RuntimeInvisibleParameterAnnotations)); 102 attr = (RuntimeParameterAnnotations_attribute) attributes.get(Attribute.RuntimeVisibleParameterAnnotations); 103 checkNull(attr, String.format("%s should be null", Attribute.RuntimeVisibleParameterAnnotations)); 104 } 105 106 public String generateLambdaSource(TestCase.TestMethodInfo method) { 107 return method.parameters.stream() 108 .map(TestCase.TestParameterInfo::generateSource) 109 .collect(Collectors.joining(", ", "I i = (", ") -> {};")); 110 } 111 112 @Override 113 public List<TestCase> generateTestCases() { 114 throw new UnsupportedOperationException(); 115 } 116} 117