author | bobv |
Tue, 07 Nov 2017 10:30:53 -0500 (2017-11-07) | |
changeset 47801 | c7b50c23ea71 |
parent 47216 | 71c04702a3d5 |
permissions | -rw-r--r-- |
20244 | 1 |
/* |
30730
d3ce7619db2c
8076543: Add @modules as needed to the langtools tests
akulyakh
parents:
20244
diff
changeset
|
2 |
* Copyright (c) 2013, 2015, Oracle and/or its affiliates. All rights reserved. |
20244 | 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 |
|
30730
d3ce7619db2c
8076543: Add @modules as needed to the langtools tests
akulyakh
parents:
20244
diff
changeset
|
29 |
* @modules jdk.compiler/com.sun.tools.javac.api |
d3ce7619db2c
8076543: Add @modules as needed to the langtools tests
akulyakh
parents:
20244
diff
changeset
|
30 |
* jdk.compiler/com.sun.tools.javac.model |
d3ce7619db2c
8076543: Add @modules as needed to the langtools tests
akulyakh
parents:
20244
diff
changeset
|
31 |
* jdk.compiler/com.sun.tools.javac.util |
20244 | 32 |
* @run main ScopeTest |
33 |
*/ |
|
34 |
||
35 |
import com.sun.source.tree.CompilationUnitTree; |
|
36 |
import com.sun.source.tree.MemberSelectTree; |
|
37 |
import com.sun.source.tree.Scope; |
|
38 |
import com.sun.source.util.JavacTask; |
|
39 |
import com.sun.source.util.TreePath; |
|
40 |
import com.sun.source.util.TreePathScanner; |
|
41 |
import com.sun.source.util.Trees; |
|
42 |
import com.sun.tools.javac.api.JavacTaskImpl; |
|
43 |
import com.sun.tools.javac.api.JavacTool; |
|
44 |
import com.sun.tools.javac.model.JavacTypes; |
|
45 |
import java.io.IOException; |
|
46 |
import java.net.URI; |
|
47 |
import java.util.ArrayList; |
|
48 |
import java.util.Collections; |
|
49 |
import javax.lang.model.element.Element; |
|
50 |
import javax.lang.model.type.TypeMirror; |
|
51 |
import javax.lang.model.util.Types; |
|
52 |
import javax.tools.JavaFileObject; |
|
53 |
import javax.tools.JavaFileObject.Kind; |
|
54 |
import javax.tools.SimpleJavaFileObject; |
|
55 |
||
56 |
public class ScopeTest { |
|
57 |
||
58 |
private static final String SOURCE_CODE = |
|
59 |
"public class Test {\n" + |
|
60 |
" private static void test() {\n" + |
|
61 |
" InvokeOn f = null;\n" + |
|
62 |
" f.run(x -> { x.correct(); });\n" + |
|
63 |
" }\n" + |
|
64 |
" public static final class FooBar {\n" + |
|
65 |
" public void dontRun() { }\n" + |
|
66 |
" }\n" + |
|
67 |
"}\n" + |
|
68 |
"class InvokeOn {\n" + |
|
69 |
" public void run(I i) { }\n" + |
|
70 |
"}\n" + |
|
71 |
"class FooBar {\n" + |
|
72 |
" public void correct() { }\n" + |
|
73 |
"}\n" + |
|
74 |
"interface I {\n" + |
|
75 |
" public void run(FooBar f);\n" + |
|
76 |
"}"; |
|
77 |
||
78 |
public static void main(String... args) throws Exception { |
|
79 |
verifyLambdaScopeCorrect(""); |
|
80 |
verifyLambdaScopeCorrect("package test;"); |
|
81 |
} |
|
82 |
||
83 |
private static void verifyLambdaScopeCorrect(final String packageClause) throws Exception { |
|
84 |
JavacTool tool = JavacTool.create(); |
|
85 |
JavaFileObject source = new SimpleJavaFileObject(URI.create("mem://Test.java"), Kind.SOURCE) { |
|
86 |
@Override public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException { |
|
87 |
return packageClause + SOURCE_CODE; |
|
88 |
} |
|
89 |
@Override public boolean isNameCompatible(String simpleName, Kind kind) { |
|
36526 | 90 |
return !"module-info".equals(simpleName); |
20244 | 91 |
} |
92 |
}; |
|
93 |
Iterable<? extends JavaFileObject> fos = Collections.singletonList(source); |
|
94 |
JavacTask task = tool.getTask(null, null, null, new ArrayList<String>(), null, fos); |
|
95 |
final Types types = JavacTypes.instance(((JavacTaskImpl) task).getContext()); |
|
96 |
final Trees trees = Trees.instance(task); |
|
97 |
CompilationUnitTree cu = task.parse().iterator().next(); |
|
98 |
||
99 |
task.analyze(); |
|
100 |
||
101 |
new TreePathScanner<Void, Void>() { |
|
102 |
@Override public Void visitMemberSelect(MemberSelectTree node, Void p) { |
|
103 |
if (node.getIdentifier().contentEquals("correct")) { |
|
104 |
TypeMirror xType = trees.getTypeMirror(new TreePath(getCurrentPath(), node.getExpression())); |
|
105 |
Scope scope = trees.getScope(getCurrentPath()); |
|
106 |
for (Element l : scope.getLocalElements()) { |
|
107 |
if (!l.getSimpleName().contentEquals("x")) continue; |
|
108 |
if (!types.isSameType(xType, l.asType())) { |
|
109 |
throw new IllegalStateException("Incorrect variable type in scope: " + l.asType() + "; should be: " + xType); |
|
110 |
} |
|
111 |
} |
|
112 |
} |
|
113 |
return super.visitMemberSelect(node, p); |
|
114 |
} |
|
115 |
}.scan(cu, null); |
|
116 |
} |
|
117 |
} |