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