RuntimeParameterAnnotationsForLambdaTest.java revision 3294:9adfb22ff08f
1141296Sdas/*
2141296Sdas * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
32116Sjkh * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
42116Sjkh *
52116Sjkh * This code is free software; you can redistribute it and/or modify it
62116Sjkh * under the terms of the GNU General Public License version 2 only, as
7141296Sdas * published by the Free Software Foundation.
82116Sjkh *
9141296Sdas * This code is distributed in the hope that it will be useful, but WITHOUT
102116Sjkh * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
112116Sjkh * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
122116Sjkh * version 2 for more details (a copy is included in the LICENSE file that
132116Sjkh * accompanied this code).
14176451Sdas *
15176451Sdas * You should have received a copy of the GNU General Public License version
162116Sjkh * 2 along with this work; if not, write to the Free Software Foundation,
17219360Sdas * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18219360Sdas *
192116Sjkh * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202116Sjkh * or visit www.oracle.com if you need additional information or have any
212116Sjkh * questions.
222116Sjkh */
23219360Sdas
242116Sjkh/*
252116Sjkh * @test
262116Sjkh * @bug 8044411 8079060 8138612
27219360Sdas * @summary Tests the RuntimeParameterVisibleAnnotations/RuntimeParameterInvisibleAnnotations attribute.
28219360Sdas * @modules jdk.jdeps/com.sun.tools.classfile
292116Sjkh *          jdk.compiler/com.sun.tools.javac.api
302116Sjkh *          jdk.compiler/com.sun.tools.javac.main
312116Sjkh *          jdk.jdeps/com.sun.tools.javap
322116Sjkh * @library /tools/lib /tools/javac/lib ../lib
332116Sjkh * @build WorkAnnotations TestBase TestResult InMemoryFileManager ToolBox
3497413Salfred * @build TestCase ClassType TestAnnotationInfo
35117912Speter * @build RuntimeParameterAnnotationsForLambdaTest AnnotationsTestBase RuntimeParameterAnnotationsTestBase
362116Sjkh * @run main RuntimeParameterAnnotationsForLambdaTest
37219360Sdas */
382116Sjkh
392116Sjkhimport java.util.List;
402116Sjkhimport java.util.stream.Collectors;
412116Sjkh
422116Sjkhimport com.sun.tools.classfile.*;
432116Sjkh
442116Sjkh/**
452116Sjkh * RuntimeParameterAnnotationsForLambdaTest is a test which checks that RuntimeVisibleParameterAnnotationsAttribute
462116Sjkh * and RuntimeInvisibleParameterAnnotationsAttribute are not generated at all for lambda expressions.
472116Sjkh * The test checks both single and repeatable annotations.
482116Sjkh * All possible combinations of retention policies are tested.
492116Sjkh *
502116Sjkh * The test generates source code, compiles it and checks the byte code.
512116Sjkh *
522116Sjkh * See README.txt for more information.
53219360Sdas */
54219360Sdaspublic class RuntimeParameterAnnotationsForLambdaTest extends RuntimeParameterAnnotationsTestBase {
55219360Sdas
56219360Sdas    private static final String CLASS_NAME = "Test";
57219360Sdas    private static final String SOURCE_TEMPLATE =
58219360Sdas            "public class " + CLASS_NAME + " {\n" +
59219360Sdas            "   interface I { void method(int a, double b, String c); }\n" +
60219360Sdas            "   %SOURCE%\n" +
61219360Sdas            "}";
62219360Sdas
632116Sjkh    public static void main(String[] args) throws TestFailedException {
642116Sjkh        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