author | bobv |
Tue, 07 Nov 2017 10:30:53 -0500 (2017-11-07) | |
changeset 47801 | c7b50c23ea71 |
parent 47318 | 423f5e46016e |
child 49518 | d0ff431a596e |
permissions | -rw-r--r-- |
47318
423f5e46016e
8188225: AST could be improved in presence of var types.
jlahoda
parents:
diff
changeset
|
1 |
/* |
423f5e46016e
8188225: AST could be improved in presence of var types.
jlahoda
parents:
diff
changeset
|
2 |
* Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved. |
423f5e46016e
8188225: AST could be improved in presence of var types.
jlahoda
parents:
diff
changeset
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
423f5e46016e
8188225: AST could be improved in presence of var types.
jlahoda
parents:
diff
changeset
|
4 |
* |
423f5e46016e
8188225: AST could be improved in presence of var types.
jlahoda
parents:
diff
changeset
|
5 |
* This code is free software; you can redistribute it and/or modify it |
423f5e46016e
8188225: AST could be improved in presence of var types.
jlahoda
parents:
diff
changeset
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
423f5e46016e
8188225: AST could be improved in presence of var types.
jlahoda
parents:
diff
changeset
|
7 |
* published by the Free Software Foundation. |
423f5e46016e
8188225: AST could be improved in presence of var types.
jlahoda
parents:
diff
changeset
|
8 |
* |
423f5e46016e
8188225: AST could be improved in presence of var types.
jlahoda
parents:
diff
changeset
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
423f5e46016e
8188225: AST could be improved in presence of var types.
jlahoda
parents:
diff
changeset
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
423f5e46016e
8188225: AST could be improved in presence of var types.
jlahoda
parents:
diff
changeset
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
423f5e46016e
8188225: AST could be improved in presence of var types.
jlahoda
parents:
diff
changeset
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that |
423f5e46016e
8188225: AST could be improved in presence of var types.
jlahoda
parents:
diff
changeset
|
13 |
* accompanied this code). |
423f5e46016e
8188225: AST could be improved in presence of var types.
jlahoda
parents:
diff
changeset
|
14 |
* |
423f5e46016e
8188225: AST could be improved in presence of var types.
jlahoda
parents:
diff
changeset
|
15 |
* You should have received a copy of the GNU General Public License version |
423f5e46016e
8188225: AST could be improved in presence of var types.
jlahoda
parents:
diff
changeset
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation, |
423f5e46016e
8188225: AST could be improved in presence of var types.
jlahoda
parents:
diff
changeset
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
423f5e46016e
8188225: AST could be improved in presence of var types.
jlahoda
parents:
diff
changeset
|
18 |
* |
423f5e46016e
8188225: AST could be improved in presence of var types.
jlahoda
parents:
diff
changeset
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
423f5e46016e
8188225: AST could be improved in presence of var types.
jlahoda
parents:
diff
changeset
|
20 |
* or visit www.oracle.com if you need additional information or have any |
423f5e46016e
8188225: AST could be improved in presence of var types.
jlahoda
parents:
diff
changeset
|
21 |
* questions. |
423f5e46016e
8188225: AST could be improved in presence of var types.
jlahoda
parents:
diff
changeset
|
22 |
*/ |
423f5e46016e
8188225: AST could be improved in presence of var types.
jlahoda
parents:
diff
changeset
|
23 |
|
423f5e46016e
8188225: AST could be improved in presence of var types.
jlahoda
parents:
diff
changeset
|
24 |
/* |
423f5e46016e
8188225: AST could be improved in presence of var types.
jlahoda
parents:
diff
changeset
|
25 |
* @test |
423f5e46016e
8188225: AST could be improved in presence of var types.
jlahoda
parents:
diff
changeset
|
26 |
* @bug 8188225 |
423f5e46016e
8188225: AST could be improved in presence of var types.
jlahoda
parents:
diff
changeset
|
27 |
* @summary Check that variables of type var have a consistent model |
423f5e46016e
8188225: AST could be improved in presence of var types.
jlahoda
parents:
diff
changeset
|
28 |
* @modules jdk.compiler/com.sun.tools.javac.api |
423f5e46016e
8188225: AST could be improved in presence of var types.
jlahoda
parents:
diff
changeset
|
29 |
*/ |
423f5e46016e
8188225: AST could be improved in presence of var types.
jlahoda
parents:
diff
changeset
|
30 |
|
423f5e46016e
8188225: AST could be improved in presence of var types.
jlahoda
parents:
diff
changeset
|
31 |
import com.sun.source.tree.CompilationUnitTree; |
423f5e46016e
8188225: AST could be improved in presence of var types.
jlahoda
parents:
diff
changeset
|
32 |
import com.sun.tools.javac.api.JavacTaskImpl; |
423f5e46016e
8188225: AST could be improved in presence of var types.
jlahoda
parents:
diff
changeset
|
33 |
|
423f5e46016e
8188225: AST could be improved in presence of var types.
jlahoda
parents:
diff
changeset
|
34 |
import java.io.IOException; |
423f5e46016e
8188225: AST could be improved in presence of var types.
jlahoda
parents:
diff
changeset
|
35 |
import java.net.URI; |
423f5e46016e
8188225: AST could be improved in presence of var types.
jlahoda
parents:
diff
changeset
|
36 |
import java.util.Arrays; |
423f5e46016e
8188225: AST could be improved in presence of var types.
jlahoda
parents:
diff
changeset
|
37 |
import java.util.List; |
423f5e46016e
8188225: AST could be improved in presence of var types.
jlahoda
parents:
diff
changeset
|
38 |
|
423f5e46016e
8188225: AST could be improved in presence of var types.
jlahoda
parents:
diff
changeset
|
39 |
import javax.tools.JavaCompiler; |
423f5e46016e
8188225: AST could be improved in presence of var types.
jlahoda
parents:
diff
changeset
|
40 |
import javax.tools.JavaFileObject; |
423f5e46016e
8188225: AST could be improved in presence of var types.
jlahoda
parents:
diff
changeset
|
41 |
import javax.tools.SimpleJavaFileObject; |
423f5e46016e
8188225: AST could be improved in presence of var types.
jlahoda
parents:
diff
changeset
|
42 |
import javax.tools.ToolProvider; |
423f5e46016e
8188225: AST could be improved in presence of var types.
jlahoda
parents:
diff
changeset
|
43 |
|
423f5e46016e
8188225: AST could be improved in presence of var types.
jlahoda
parents:
diff
changeset
|
44 |
import com.sun.source.tree.VariableTree; |
423f5e46016e
8188225: AST could be improved in presence of var types.
jlahoda
parents:
diff
changeset
|
45 |
import com.sun.source.util.TreeScanner; |
423f5e46016e
8188225: AST could be improved in presence of var types.
jlahoda
parents:
diff
changeset
|
46 |
import com.sun.source.util.Trees; |
423f5e46016e
8188225: AST could be improved in presence of var types.
jlahoda
parents:
diff
changeset
|
47 |
|
423f5e46016e
8188225: AST could be improved in presence of var types.
jlahoda
parents:
diff
changeset
|
48 |
public class VarTree { |
423f5e46016e
8188225: AST could be improved in presence of var types.
jlahoda
parents:
diff
changeset
|
49 |
private final JavaCompiler tool = ToolProvider.getSystemJavaCompiler(); |
423f5e46016e
8188225: AST could be improved in presence of var types.
jlahoda
parents:
diff
changeset
|
50 |
|
423f5e46016e
8188225: AST could be improved in presence of var types.
jlahoda
parents:
diff
changeset
|
51 |
public static void main(String... args) throws Exception { |
423f5e46016e
8188225: AST could be improved in presence of var types.
jlahoda
parents:
diff
changeset
|
52 |
VarTree test = new VarTree(); |
423f5e46016e
8188225: AST could be improved in presence of var types.
jlahoda
parents:
diff
changeset
|
53 |
test.run("|var testVar = 0;| ", |
423f5e46016e
8188225: AST could be improved in presence of var types.
jlahoda
parents:
diff
changeset
|
54 |
"int testVar = 0"); |
423f5e46016e
8188225: AST could be improved in presence of var types.
jlahoda
parents:
diff
changeset
|
55 |
test.run("|var testVar = 0;| undef undef;", |
423f5e46016e
8188225: AST could be improved in presence of var types.
jlahoda
parents:
diff
changeset
|
56 |
"int testVar = 0"); |
423f5e46016e
8188225: AST could be improved in presence of var types.
jlahoda
parents:
diff
changeset
|
57 |
test.run("|final var testVar = 0;| ", |
423f5e46016e
8188225: AST could be improved in presence of var types.
jlahoda
parents:
diff
changeset
|
58 |
"final int testVar = 0"); |
423f5e46016e
8188225: AST could be improved in presence of var types.
jlahoda
parents:
diff
changeset
|
59 |
test.run("for (|var testVar| : java.util.Arrays.asList(0, 1)) {}", |
423f5e46016e
8188225: AST could be improved in presence of var types.
jlahoda
parents:
diff
changeset
|
60 |
"java.lang.Integer testVar"); |
423f5e46016e
8188225: AST could be improved in presence of var types.
jlahoda
parents:
diff
changeset
|
61 |
test.run("for (|final var testVar| : java.util.Arrays.asList(0, 1)) {}", |
423f5e46016e
8188225: AST could be improved in presence of var types.
jlahoda
parents:
diff
changeset
|
62 |
"final java.lang.Integer testVar"); |
423f5e46016e
8188225: AST could be improved in presence of var types.
jlahoda
parents:
diff
changeset
|
63 |
test.run("java.util.function.Consumer<String> c = |testVar| -> {};", |
423f5e46016e
8188225: AST could be improved in presence of var types.
jlahoda
parents:
diff
changeset
|
64 |
"java.lang.String testVar"); |
423f5e46016e
8188225: AST could be improved in presence of var types.
jlahoda
parents:
diff
changeset
|
65 |
test.run("java.util.function.Consumer<String> c = (|testVar|) -> {};", |
423f5e46016e
8188225: AST could be improved in presence of var types.
jlahoda
parents:
diff
changeset
|
66 |
"java.lang.String testVar"); |
423f5e46016e
8188225: AST could be improved in presence of var types.
jlahoda
parents:
diff
changeset
|
67 |
} |
423f5e46016e
8188225: AST could be improved in presence of var types.
jlahoda
parents:
diff
changeset
|
68 |
|
423f5e46016e
8188225: AST could be improved in presence of var types.
jlahoda
parents:
diff
changeset
|
69 |
void run(String code, String expected) throws IOException { |
423f5e46016e
8188225: AST could be improved in presence of var types.
jlahoda
parents:
diff
changeset
|
70 |
String[] parts = code.split("\\|"); |
423f5e46016e
8188225: AST could be improved in presence of var types.
jlahoda
parents:
diff
changeset
|
71 |
|
423f5e46016e
8188225: AST could be improved in presence of var types.
jlahoda
parents:
diff
changeset
|
72 |
if (parts.length != 3) { |
423f5e46016e
8188225: AST could be improved in presence of var types.
jlahoda
parents:
diff
changeset
|
73 |
throw new IllegalStateException("Incorrect number of markers."); |
423f5e46016e
8188225: AST could be improved in presence of var types.
jlahoda
parents:
diff
changeset
|
74 |
} |
423f5e46016e
8188225: AST could be improved in presence of var types.
jlahoda
parents:
diff
changeset
|
75 |
|
423f5e46016e
8188225: AST could be improved in presence of var types.
jlahoda
parents:
diff
changeset
|
76 |
String prefix = "public class Test { void test() { "; |
423f5e46016e
8188225: AST could be improved in presence of var types.
jlahoda
parents:
diff
changeset
|
77 |
String src = prefix + parts[0] + parts[1] + parts[2] + " } }"; |
423f5e46016e
8188225: AST could be improved in presence of var types.
jlahoda
parents:
diff
changeset
|
78 |
|
423f5e46016e
8188225: AST could be improved in presence of var types.
jlahoda
parents:
diff
changeset
|
79 |
JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, d -> {}, |
423f5e46016e
8188225: AST could be improved in presence of var types.
jlahoda
parents:
diff
changeset
|
80 |
List.of("--should-stop:at=FLOW"), |
423f5e46016e
8188225: AST could be improved in presence of var types.
jlahoda
parents:
diff
changeset
|
81 |
null, Arrays.asList(new MyFileObject(src))); |
423f5e46016e
8188225: AST could be improved in presence of var types.
jlahoda
parents:
diff
changeset
|
82 |
|
423f5e46016e
8188225: AST could be improved in presence of var types.
jlahoda
parents:
diff
changeset
|
83 |
Iterable<? extends CompilationUnitTree> units = ct.parse(); |
423f5e46016e
8188225: AST could be improved in presence of var types.
jlahoda
parents:
diff
changeset
|
84 |
ct.analyze(); |
423f5e46016e
8188225: AST could be improved in presence of var types.
jlahoda
parents:
diff
changeset
|
85 |
|
423f5e46016e
8188225: AST could be improved in presence of var types.
jlahoda
parents:
diff
changeset
|
86 |
Trees trees = Trees.instance(ct); |
423f5e46016e
8188225: AST could be improved in presence of var types.
jlahoda
parents:
diff
changeset
|
87 |
|
423f5e46016e
8188225: AST could be improved in presence of var types.
jlahoda
parents:
diff
changeset
|
88 |
for (CompilationUnitTree cut : units) { |
423f5e46016e
8188225: AST could be improved in presence of var types.
jlahoda
parents:
diff
changeset
|
89 |
new TreeScanner<Void, Void>() { |
423f5e46016e
8188225: AST could be improved in presence of var types.
jlahoda
parents:
diff
changeset
|
90 |
@Override |
423f5e46016e
8188225: AST could be improved in presence of var types.
jlahoda
parents:
diff
changeset
|
91 |
public Void visitVariable(VariableTree node, Void p) { |
423f5e46016e
8188225: AST could be improved in presence of var types.
jlahoda
parents:
diff
changeset
|
92 |
if (node.getName().contentEquals("testVar")) { |
423f5e46016e
8188225: AST could be improved in presence of var types.
jlahoda
parents:
diff
changeset
|
93 |
if (!expected.equals(node.toString())) { |
423f5e46016e
8188225: AST could be improved in presence of var types.
jlahoda
parents:
diff
changeset
|
94 |
throw new AssertionError("Unexpected tree: " + node.toString()); |
423f5e46016e
8188225: AST could be improved in presence of var types.
jlahoda
parents:
diff
changeset
|
95 |
} |
423f5e46016e
8188225: AST could be improved in presence of var types.
jlahoda
parents:
diff
changeset
|
96 |
|
423f5e46016e
8188225: AST could be improved in presence of var types.
jlahoda
parents:
diff
changeset
|
97 |
int start = (int) trees.getSourcePositions().getStartPosition(cut, node); |
423f5e46016e
8188225: AST could be improved in presence of var types.
jlahoda
parents:
diff
changeset
|
98 |
int end = (int) trees.getSourcePositions().getEndPosition(cut, node); |
423f5e46016e
8188225: AST could be improved in presence of var types.
jlahoda
parents:
diff
changeset
|
99 |
|
423f5e46016e
8188225: AST could be improved in presence of var types.
jlahoda
parents:
diff
changeset
|
100 |
String snip = src.substring(start, end); |
423f5e46016e
8188225: AST could be improved in presence of var types.
jlahoda
parents:
diff
changeset
|
101 |
|
423f5e46016e
8188225: AST could be improved in presence of var types.
jlahoda
parents:
diff
changeset
|
102 |
if (start != prefix.length() + parts[0].length() || end != prefix.length() + parts[0].length() + parts[1].length()) { |
423f5e46016e
8188225: AST could be improved in presence of var types.
jlahoda
parents:
diff
changeset
|
103 |
throw new AssertionError("Unexpected span: " + snip); |
423f5e46016e
8188225: AST could be improved in presence of var types.
jlahoda
parents:
diff
changeset
|
104 |
} |
423f5e46016e
8188225: AST could be improved in presence of var types.
jlahoda
parents:
diff
changeset
|
105 |
|
423f5e46016e
8188225: AST could be improved in presence of var types.
jlahoda
parents:
diff
changeset
|
106 |
int typeStart = (int) trees.getSourcePositions().getStartPosition(cut, node.getType()); |
423f5e46016e
8188225: AST could be improved in presence of var types.
jlahoda
parents:
diff
changeset
|
107 |
int typeEnd = (int) trees.getSourcePositions().getEndPosition(cut, node.getType()); |
423f5e46016e
8188225: AST could be improved in presence of var types.
jlahoda
parents:
diff
changeset
|
108 |
|
423f5e46016e
8188225: AST could be improved in presence of var types.
jlahoda
parents:
diff
changeset
|
109 |
if (typeStart != (-1) && typeEnd != (-1)) { |
423f5e46016e
8188225: AST could be improved in presence of var types.
jlahoda
parents:
diff
changeset
|
110 |
throw new AssertionError("Unexpected type position: " + typeStart + ", " + typeEnd); |
423f5e46016e
8188225: AST could be improved in presence of var types.
jlahoda
parents:
diff
changeset
|
111 |
} |
423f5e46016e
8188225: AST could be improved in presence of var types.
jlahoda
parents:
diff
changeset
|
112 |
} |
423f5e46016e
8188225: AST could be improved in presence of var types.
jlahoda
parents:
diff
changeset
|
113 |
return super.visitVariable(node, p); |
423f5e46016e
8188225: AST could be improved in presence of var types.
jlahoda
parents:
diff
changeset
|
114 |
} |
423f5e46016e
8188225: AST could be improved in presence of var types.
jlahoda
parents:
diff
changeset
|
115 |
|
423f5e46016e
8188225: AST could be improved in presence of var types.
jlahoda
parents:
diff
changeset
|
116 |
}.scan(cut, null); |
423f5e46016e
8188225: AST could be improved in presence of var types.
jlahoda
parents:
diff
changeset
|
117 |
} |
423f5e46016e
8188225: AST could be improved in presence of var types.
jlahoda
parents:
diff
changeset
|
118 |
} |
423f5e46016e
8188225: AST could be improved in presence of var types.
jlahoda
parents:
diff
changeset
|
119 |
class MyFileObject extends SimpleJavaFileObject { |
423f5e46016e
8188225: AST could be improved in presence of var types.
jlahoda
parents:
diff
changeset
|
120 |
|
423f5e46016e
8188225: AST could be improved in presence of var types.
jlahoda
parents:
diff
changeset
|
121 |
private String text; |
423f5e46016e
8188225: AST could be improved in presence of var types.
jlahoda
parents:
diff
changeset
|
122 |
|
423f5e46016e
8188225: AST could be improved in presence of var types.
jlahoda
parents:
diff
changeset
|
123 |
public MyFileObject(String text) { |
423f5e46016e
8188225: AST could be improved in presence of var types.
jlahoda
parents:
diff
changeset
|
124 |
super(URI.create("myfo:/Test.java"), JavaFileObject.Kind.SOURCE); |
423f5e46016e
8188225: AST could be improved in presence of var types.
jlahoda
parents:
diff
changeset
|
125 |
this.text = text; |
423f5e46016e
8188225: AST could be improved in presence of var types.
jlahoda
parents:
diff
changeset
|
126 |
} |
423f5e46016e
8188225: AST could be improved in presence of var types.
jlahoda
parents:
diff
changeset
|
127 |
|
423f5e46016e
8188225: AST could be improved in presence of var types.
jlahoda
parents:
diff
changeset
|
128 |
@Override |
423f5e46016e
8188225: AST could be improved in presence of var types.
jlahoda
parents:
diff
changeset
|
129 |
public CharSequence getCharContent(boolean ignoreEncodingErrors) { |
423f5e46016e
8188225: AST could be improved in presence of var types.
jlahoda
parents:
diff
changeset
|
130 |
return text; |
423f5e46016e
8188225: AST could be improved in presence of var types.
jlahoda
parents:
diff
changeset
|
131 |
} |
423f5e46016e
8188225: AST could be improved in presence of var types.
jlahoda
parents:
diff
changeset
|
132 |
} |
423f5e46016e
8188225: AST could be improved in presence of var types.
jlahoda
parents:
diff
changeset
|
133 |
} |