|
1 /* |
|
2 * Copyright (c) 2013, 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 8023835 |
|
27 * @summary Verify that implicit type of lambda parameter is correctly attributed |
|
28 * in Scope |
|
29 * @run main ScopeTest |
|
30 */ |
|
31 |
|
32 import com.sun.source.tree.CompilationUnitTree; |
|
33 import com.sun.source.tree.MemberSelectTree; |
|
34 import com.sun.source.tree.Scope; |
|
35 import com.sun.source.util.JavacTask; |
|
36 import com.sun.source.util.TreePath; |
|
37 import com.sun.source.util.TreePathScanner; |
|
38 import com.sun.source.util.Trees; |
|
39 import com.sun.tools.javac.api.JavacTaskImpl; |
|
40 import com.sun.tools.javac.api.JavacTool; |
|
41 import com.sun.tools.javac.model.JavacTypes; |
|
42 import java.io.IOException; |
|
43 import java.net.URI; |
|
44 import java.util.ArrayList; |
|
45 import java.util.Collections; |
|
46 import javax.lang.model.element.Element; |
|
47 import javax.lang.model.type.TypeMirror; |
|
48 import javax.lang.model.util.Types; |
|
49 import javax.tools.JavaFileObject; |
|
50 import javax.tools.JavaFileObject.Kind; |
|
51 import javax.tools.SimpleJavaFileObject; |
|
52 |
|
53 public class ScopeTest { |
|
54 |
|
55 private static final String SOURCE_CODE = |
|
56 "public class Test {\n" + |
|
57 " private static void test() {\n" + |
|
58 " InvokeOn f = null;\n" + |
|
59 " f.run(x -> { x.correct(); });\n" + |
|
60 " }\n" + |
|
61 " public static final class FooBar {\n" + |
|
62 " public void dontRun() { }\n" + |
|
63 " }\n" + |
|
64 "}\n" + |
|
65 "class InvokeOn {\n" + |
|
66 " public void run(I i) { }\n" + |
|
67 "}\n" + |
|
68 "class FooBar {\n" + |
|
69 " public void correct() { }\n" + |
|
70 "}\n" + |
|
71 "interface I {\n" + |
|
72 " public void run(FooBar f);\n" + |
|
73 "}"; |
|
74 |
|
75 public static void main(String... args) throws Exception { |
|
76 verifyLambdaScopeCorrect(""); |
|
77 verifyLambdaScopeCorrect("package test;"); |
|
78 } |
|
79 |
|
80 private static void verifyLambdaScopeCorrect(final String packageClause) throws Exception { |
|
81 JavacTool tool = JavacTool.create(); |
|
82 JavaFileObject source = new SimpleJavaFileObject(URI.create("mem://Test.java"), Kind.SOURCE) { |
|
83 @Override public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException { |
|
84 return packageClause + SOURCE_CODE; |
|
85 } |
|
86 @Override public boolean isNameCompatible(String simpleName, Kind kind) { |
|
87 return true; |
|
88 } |
|
89 }; |
|
90 Iterable<? extends JavaFileObject> fos = Collections.singletonList(source); |
|
91 JavacTask task = tool.getTask(null, null, null, new ArrayList<String>(), null, fos); |
|
92 final Types types = JavacTypes.instance(((JavacTaskImpl) task).getContext()); |
|
93 final Trees trees = Trees.instance(task); |
|
94 CompilationUnitTree cu = task.parse().iterator().next(); |
|
95 |
|
96 task.analyze(); |
|
97 |
|
98 new TreePathScanner<Void, Void>() { |
|
99 @Override public Void visitMemberSelect(MemberSelectTree node, Void p) { |
|
100 if (node.getIdentifier().contentEquals("correct")) { |
|
101 TypeMirror xType = trees.getTypeMirror(new TreePath(getCurrentPath(), node.getExpression())); |
|
102 Scope scope = trees.getScope(getCurrentPath()); |
|
103 for (Element l : scope.getLocalElements()) { |
|
104 if (!l.getSimpleName().contentEquals("x")) continue; |
|
105 if (!types.isSameType(xType, l.asType())) { |
|
106 throw new IllegalStateException("Incorrect variable type in scope: " + l.asType() + "; should be: " + xType); |
|
107 } |
|
108 } |
|
109 } |
|
110 return super.visitMemberSelect(node, p); |
|
111 } |
|
112 }.scan(cu, null); |
|
113 } |
|
114 } |