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