author | rfield |
Sat, 09 Apr 2016 11:03:48 -0700 | |
changeset 37007 | 6023a9a9d58a |
parent 34750 | 36d62753f5da |
child 37644 | 33cf53901cac |
permissions | -rw-r--r-- |
33362 | 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 |
|
34750
36d62753f5da
8144903: JShell: determine incorrectly the type of the expression which is array type of captured type
rfield
parents:
33362
diff
changeset
|
26 |
* @bug 8144903 |
33362 | 27 |
* @summary Tests for EvaluationState.variables |
28 |
* @build KullaTesting TestingInputStream ExpectedDiagnostic |
|
29 |
* @run testng VariablesTest |
|
30 |
*/ |
|
31 |
||
32 |
import java.util.List; |
|
33 |
import javax.tools.Diagnostic; |
|
34 |
||
35 |
import jdk.jshell.Snippet; |
|
36 |
import jdk.jshell.TypeDeclSnippet; |
|
37 |
import jdk.jshell.VarSnippet; |
|
38 |
import jdk.jshell.Snippet.SubKind; |
|
39 |
import jdk.jshell.SnippetEvent; |
|
40 |
import org.testng.annotations.Test; |
|
41 |
||
42 |
import static jdk.jshell.Snippet.Status.*; |
|
43 |
import static jdk.jshell.Snippet.SubKind.VAR_DECLARATION_SUBKIND; |
|
44 |
import static org.testng.Assert.assertEquals; |
|
45 |
import static org.testng.Assert.fail; |
|
46 |
||
47 |
@Test |
|
48 |
public class VariablesTest extends KullaTesting { |
|
49 |
||
50 |
public void noVariables() { |
|
51 |
assertNumberOfActiveVariables(0); |
|
52 |
} |
|
53 |
||
54 |
private void badVarValue(VarSnippet key) { |
|
55 |
try { |
|
56 |
getState().varValue(key); |
|
57 |
fail("Expected exception for: " + key.source()); |
|
58 |
} catch (IllegalArgumentException e) { |
|
59 |
// ok |
|
60 |
} |
|
61 |
} |
|
62 |
||
63 |
public void testVarValue1() { |
|
64 |
VarSnippet v1 = varKey(assertEval("und1 a;", added(RECOVERABLE_NOT_DEFINED))); |
|
65 |
badVarValue(v1); |
|
66 |
VarSnippet v2 = varKey(assertEval("und2 a;", |
|
67 |
ste(MAIN_SNIPPET, RECOVERABLE_NOT_DEFINED, RECOVERABLE_NOT_DEFINED, false, null), |
|
68 |
ste(v1, RECOVERABLE_NOT_DEFINED, OVERWRITTEN, false, MAIN_SNIPPET))); |
|
69 |
badVarValue(v2); |
|
70 |
TypeDeclSnippet und = classKey(assertEval("class und2 {}", |
|
71 |
added(VALID), |
|
72 |
ste(v2, RECOVERABLE_NOT_DEFINED, VALID, true, MAIN_SNIPPET))); |
|
73 |
assertVarValue(v2, "null"); |
|
74 |
assertDrop(und, |
|
75 |
DiagCheck.DIAG_OK, |
|
76 |
DiagCheck.DIAG_ERROR, |
|
77 |
ste(und, VALID, DROPPED, true, null), |
|
78 |
ste(v2, VALID, RECOVERABLE_NOT_DEFINED, true, und)); |
|
79 |
badVarValue(v1); |
|
80 |
badVarValue(v2); |
|
81 |
} |
|
82 |
||
83 |
public void testVarValue2() { |
|
84 |
VarSnippet v1 = (VarSnippet) assertDeclareFail("int a = 0.0;", "compiler.err.prob.found.req"); |
|
85 |
badVarValue(v1); |
|
86 |
VarSnippet v2 = varKey(assertEval("int a = 0;", ste(v1, REJECTED, VALID, true, null))); |
|
87 |
assertDrop(v2, ste(MAIN_SNIPPET, VALID, DROPPED, true, null)); |
|
88 |
badVarValue(v2); |
|
89 |
} |
|
90 |
||
91 |
public void testSignature1() { |
|
92 |
VarSnippet v1 = varKey(assertEval("und1 a;", added(RECOVERABLE_NOT_DEFINED))); |
|
93 |
assertVariableDeclSnippet(v1, "a", "und1", RECOVERABLE_NOT_DEFINED, VAR_DECLARATION_SUBKIND, 1, 0); |
|
94 |
VarSnippet v2 = varKey(assertEval("und2 a;", |
|
95 |
ste(MAIN_SNIPPET, RECOVERABLE_NOT_DEFINED, RECOVERABLE_NOT_DEFINED, false, null), |
|
96 |
ste(v1, RECOVERABLE_NOT_DEFINED, OVERWRITTEN, false, MAIN_SNIPPET))); |
|
97 |
assertVariableDeclSnippet(v2, "a", "und2", RECOVERABLE_NOT_DEFINED, VAR_DECLARATION_SUBKIND, 1, 0); |
|
98 |
TypeDeclSnippet und = classKey(assertEval("class und2 {}", |
|
99 |
added(VALID), |
|
100 |
ste(v2, RECOVERABLE_NOT_DEFINED, VALID, true, MAIN_SNIPPET))); |
|
101 |
assertVariableDeclSnippet(v2, "a", "und2", VALID, VAR_DECLARATION_SUBKIND, 0, 0); |
|
102 |
assertDrop(und, |
|
103 |
DiagCheck.DIAG_OK, |
|
104 |
DiagCheck.DIAG_ERROR, |
|
105 |
ste(und, VALID, DROPPED, true, null), |
|
106 |
ste(v2, VALID, RECOVERABLE_NOT_DEFINED, true, und)); |
|
107 |
assertVariableDeclSnippet(v2, "a", "und2", RECOVERABLE_NOT_DEFINED, VAR_DECLARATION_SUBKIND, 1, 0); |
|
108 |
} |
|
109 |
||
110 |
public void testSignature2() { |
|
111 |
VarSnippet v1 = (VarSnippet) assertDeclareFail("int a = 0.0;", "compiler.err.prob.found.req"); |
|
112 |
assertVariableDeclSnippet(v1, "a", "int", REJECTED, SubKind.VAR_DECLARATION_WITH_INITIALIZER_SUBKIND, 0, 1); |
|
113 |
VarSnippet v2 = varKey(assertEval("int a = 0;", |
|
114 |
ste(v1, REJECTED, VALID, true, null))); |
|
115 |
assertVariableDeclSnippet(v2, "a", "int", VALID, SubKind.VAR_DECLARATION_WITH_INITIALIZER_SUBKIND, 0, 0); |
|
116 |
assertDrop(v2, ste(MAIN_SNIPPET, VALID, DROPPED, true, null)); |
|
117 |
assertVariableDeclSnippet(v2, "a", "int", DROPPED, SubKind.VAR_DECLARATION_WITH_INITIALIZER_SUBKIND, 0, 0); |
|
118 |
} |
|
119 |
||
120 |
public void variables() { |
|
121 |
VarSnippet snx = varKey(assertEval("int x = 10;")); |
|
122 |
VarSnippet sny = varKey(assertEval("String y = \"hi\";")); |
|
123 |
VarSnippet snz = varKey(assertEval("long z;")); |
|
124 |
assertVariables(variable("int", "x"), variable("String", "y"), variable("long", "z")); |
|
125 |
assertVarValue(snx, "10"); |
|
126 |
assertVarValue(sny, "\"hi\""); |
|
127 |
assertVarValue(snz, "0"); |
|
128 |
assertActiveKeys(); |
|
129 |
} |
|
130 |
||
131 |
public void variablesArray() { |
|
132 |
VarSnippet sn = varKey(assertEval("int[] a = new int[12];")); |
|
133 |
assertEquals(sn.typeName(), "int[]"); |
|
134 |
assertEval("int len = a.length;", "12"); |
|
135 |
assertVariables(variable("int[]", "a"), variable("int", "len")); |
|
136 |
assertActiveKeys(); |
|
137 |
} |
|
138 |
||
139 |
public void variablesArrayOld() { |
|
140 |
VarSnippet sn = varKey(assertEval("int a[] = new int[12];")); |
|
141 |
assertEquals(sn.typeName(), "int[]"); |
|
142 |
assertEval("int len = a.length;", "12"); |
|
143 |
assertVariables(variable("int[]", "a"), variable("int", "len")); |
|
144 |
assertActiveKeys(); |
|
145 |
} |
|
146 |
||
147 |
public void variablesRedefinition() { |
|
148 |
Snippet x = varKey(assertEval("int x = 10;")); |
|
149 |
Snippet y = varKey(assertEval("String y = \"\";", added(VALID))); |
|
150 |
assertVariables(variable("int", "x"), variable("String", "y")); |
|
151 |
assertActiveKeys(); |
|
152 |
assertEval("long x;", |
|
153 |
ste(MAIN_SNIPPET, VALID, VALID, true, null), |
|
154 |
ste(x, VALID, OVERWRITTEN, false, MAIN_SNIPPET)); |
|
155 |
assertVariables(variable("long", "x"), variable("String", "y")); |
|
156 |
assertActiveKeys(); |
|
157 |
assertEval("String y;", |
|
158 |
ste(MAIN_SNIPPET, VALID, VALID, false, null), |
|
159 |
ste(y, VALID, OVERWRITTEN, false, MAIN_SNIPPET)); |
|
160 |
assertVariables(variable("long", "x"), variable("String", "y")); |
|
161 |
assertActiveKeys(); |
|
162 |
} |
|
163 |
||
164 |
public void variablesTemporary() { |
|
165 |
assertEval("int $1 = 10;", added(VALID)); |
|
166 |
assertEval("2 * $1;", added(VALID)); |
|
167 |
assertVariables(variable("int", "$1"), variable("int", "$2")); |
|
168 |
assertActiveKeys(); |
|
169 |
assertEval("String y;", added(VALID)); |
|
170 |
assertVariables(variable("int", "$1"), variable("int", "$2"), variable("String", "y")); |
|
171 |
assertActiveKeys(); |
|
172 |
} |
|
173 |
||
174 |
public void variablesTemporaryNull() { |
|
175 |
assertEval("null;", added(VALID)); |
|
176 |
assertVariables(variable("Object", "$1")); |
|
177 |
assertEval("(String) null;", added(VALID)); |
|
178 |
assertVariables(variable("Object", "$1"), variable("String", "$2")); |
|
179 |
assertActiveKeys(); |
|
180 |
assertEval("\"\";", added(VALID)); |
|
181 |
assertVariables( |
|
182 |
variable("Object", "$1"), |
|
183 |
variable("String", "$2"), |
|
184 |
variable("String", "$3")); |
|
185 |
assertActiveKeys(); |
|
186 |
} |
|
187 |
||
34750
36d62753f5da
8144903: JShell: determine incorrectly the type of the expression which is array type of captured type
rfield
parents:
33362
diff
changeset
|
188 |
public void variablesTemporaryArrayOfCapturedType() { |
36d62753f5da
8144903: JShell: determine incorrectly the type of the expression which is array type of captured type
rfield
parents:
33362
diff
changeset
|
189 |
assertEval("class Test<T> { T[][] get() { return null; } }", added(VALID)); |
36d62753f5da
8144903: JShell: determine incorrectly the type of the expression which is array type of captured type
rfield
parents:
33362
diff
changeset
|
190 |
assertEval("Test<? extends String> test() { return new Test<>(); }", added(VALID)); |
36d62753f5da
8144903: JShell: determine incorrectly the type of the expression which is array type of captured type
rfield
parents:
33362
diff
changeset
|
191 |
assertEval("test().get()", added(VALID)); |
36d62753f5da
8144903: JShell: determine incorrectly the type of the expression which is array type of captured type
rfield
parents:
33362
diff
changeset
|
192 |
assertVariables(variable("String[][]", "$1")); |
36d62753f5da
8144903: JShell: determine incorrectly the type of the expression which is array type of captured type
rfield
parents:
33362
diff
changeset
|
193 |
assertEval("\"\".getClass().getEnumConstants()", added(VALID)); |
36d62753f5da
8144903: JShell: determine incorrectly the type of the expression which is array type of captured type
rfield
parents:
33362
diff
changeset
|
194 |
assertVariables(variable("String[][]", "$1"), variable("String[]", "$2")); |
36d62753f5da
8144903: JShell: determine incorrectly the type of the expression which is array type of captured type
rfield
parents:
33362
diff
changeset
|
195 |
assertActiveKeys(); |
36d62753f5da
8144903: JShell: determine incorrectly the type of the expression which is array type of captured type
rfield
parents:
33362
diff
changeset
|
196 |
} |
36d62753f5da
8144903: JShell: determine incorrectly the type of the expression which is array type of captured type
rfield
parents:
33362
diff
changeset
|
197 |
|
33362 | 198 |
public void variablesClassReplace() { |
199 |
assertEval("import java.util.*;", added(VALID)); |
|
200 |
Snippet var = varKey(assertEval("List<Integer> list = new ArrayList<>();", "[]", |
|
201 |
added(VALID))); |
|
202 |
assertVariables(variable("List<Integer>", "list")); |
|
203 |
assertEval("class List {}", |
|
204 |
DiagCheck.DIAG_OK, |
|
205 |
DiagCheck.DIAG_ERROR, |
|
206 |
added(VALID), |
|
207 |
ste(var, VALID, RECOVERABLE_NOT_DEFINED, true, MAIN_SNIPPET)); |
|
208 |
assertVariables(); |
|
209 |
assertEval("List list = new List();", |
|
210 |
DiagCheck.DIAG_OK, DiagCheck.DIAG_IGNORE, |
|
211 |
ste(MAIN_SNIPPET, RECOVERABLE_NOT_DEFINED, VALID, true, null), |
|
212 |
ste(var, RECOVERABLE_NOT_DEFINED, OVERWRITTEN, false, MAIN_SNIPPET)); |
|
213 |
assertVariables(variable("List", "list")); |
|
214 |
assertActiveKeys(); |
|
215 |
} |
|
216 |
||
217 |
public void variablesErrors() { |
|
218 |
assertDeclareFail("String;", new ExpectedDiagnostic("compiler.err.cant.resolve.location", 0, 6, 0, -1, -1, Diagnostic.Kind.ERROR)); |
|
219 |
assertNumberOfActiveVariables(0); |
|
220 |
assertActiveKeys(); |
|
221 |
} |
|
222 |
||
223 |
public void variablesUnresolvedActiveFailed() { |
|
224 |
VarSnippet key = varKey(assertEval("und x;", added(RECOVERABLE_NOT_DEFINED))); |
|
225 |
assertVariableDeclSnippet(key, "x", "und", RECOVERABLE_NOT_DEFINED, VAR_DECLARATION_SUBKIND, 1, 0); |
|
226 |
assertUnresolvedDependencies1(key, RECOVERABLE_NOT_DEFINED, "class und"); |
|
227 |
assertNumberOfActiveVariables(1); |
|
228 |
assertActiveKeys(); |
|
229 |
} |
|
230 |
||
231 |
public void variablesUnresolvedError() { |
|
232 |
assertDeclareFail("und y = null;", new ExpectedDiagnostic("compiler.err.cant.resolve.location", 0, 3, 0, -1, -1, Diagnostic.Kind.ERROR)); |
|
233 |
assertNumberOfActiveVariables(0); |
|
234 |
assertActiveKeys(); |
|
235 |
} |
|
236 |
||
237 |
public void variablesMultiByteCharacterType() { |
|
238 |
assertEval("class \u3042 {}"); |
|
239 |
assertEval("\u3042 \u3042 = null;", added(VALID)); |
|
240 |
assertVariables(variable("\u3042", "\u3042")); |
|
241 |
assertEval("new \u3042()", added(VALID)); |
|
242 |
assertVariables(variable("\u3042", "\u3042"), variable("\u3042", "$1")); |
|
243 |
||
244 |
assertEval("class \u3042\u3044\u3046\u3048\u304a {}"); |
|
245 |
assertEval("\u3042\u3044\u3046\u3048\u304a \u3042\u3044\u3046\u3048\u304a = null;", added(VALID)); |
|
246 |
assertVariables(variable("\u3042", "\u3042"), variable("\u3042", "$1"), |
|
247 |
variable("\u3042\u3044\u3046\u3048\u304a", "\u3042\u3044\u3046\u3048\u304a")); |
|
248 |
assertEval("new \u3042\u3044\u3046\u3048\u304a();"); |
|
249 |
assertVariables(variable("\u3042", "\u3042"), variable("\u3042", "$1"), |
|
250 |
variable("\u3042\u3044\u3046\u3048\u304a", "\u3042\u3044\u3046\u3048\u304a"), |
|
251 |
variable("\u3042\u3044\u3046\u3048\u304a", "$2")); |
|
252 |
assertActiveKeys(); |
|
253 |
} |
|
254 |
||
255 |
@Test(enabled = false) // TODO 8081689 |
|
256 |
public void methodVariablesAreNotVisible() { |
|
257 |
Snippet foo = varKey(assertEval("int foo() {" + |
|
258 |
"int x = 10;" + |
|
259 |
"int y = 2 * x;" + |
|
260 |
"return x * y;" + |
|
261 |
"}", added(VALID))); |
|
262 |
assertNumberOfActiveVariables(0); |
|
263 |
assertActiveKeys(); |
|
264 |
assertEval("int x = 10;", "10"); |
|
265 |
assertEval("int foo() {" + |
|
266 |
"int y = 2 * x;" + |
|
267 |
"return x * y;" + |
|
268 |
"}", |
|
269 |
ste(foo, VALID, VALID, false, null)); |
|
270 |
assertVariables(variable("int", "x")); |
|
271 |
assertActiveKeys(); |
|
272 |
assertEval("foo();", "200"); |
|
273 |
assertVariables(variable("int", "x"), variable("int", "$1")); |
|
274 |
assertActiveKeys(); |
|
275 |
} |
|
276 |
||
277 |
@Test(enabled = false) // TODO 8081689 |
|
278 |
public void classFieldsAreNotVisible() { |
|
279 |
Snippet key = classKey(assertEval("class clazz {" + |
|
280 |
"int x = 10;" + |
|
281 |
"int y = 2 * x;" + |
|
282 |
"}")); |
|
283 |
assertNumberOfActiveVariables(0); |
|
284 |
assertEval("int x = 10;", "10"); |
|
285 |
assertActiveKeys(); |
|
286 |
assertEval( |
|
287 |
"class clazz {" + |
|
288 |
"int y = 2 * x;" + |
|
289 |
"}", |
|
290 |
ste(key, VALID, VALID, true, null)); |
|
291 |
assertVariables(variable("int", "x")); |
|
292 |
assertEval("new clazz().y;", "20"); |
|
293 |
assertVariables(variable("int", "x"), variable("int", "$1")); |
|
294 |
assertActiveKeys(); |
|
295 |
} |
|
296 |
||
297 |
public void multiVariables() { |
|
298 |
List<SnippetEvent> abc = assertEval("int a, b, c = 10;", |
|
299 |
DiagCheck.DIAG_OK, DiagCheck.DIAG_OK, |
|
300 |
chain(added(VALID)), |
|
301 |
chain(added(VALID)), |
|
302 |
chain(added(VALID))); |
|
303 |
Snippet a = abc.get(0).snippet(); |
|
304 |
Snippet b = abc.get(1).snippet(); |
|
305 |
Snippet c = abc.get(2).snippet(); |
|
306 |
assertVariables(variable("int", "a"), variable("int", "b"), variable("int", "c")); |
|
307 |
assertEval("double a = 1.4, b = 8.8;", DiagCheck.DIAG_OK, DiagCheck.DIAG_OK, |
|
308 |
chain(ste(MAIN_SNIPPET, VALID, VALID, true, null), ste(a, VALID, OVERWRITTEN, false, MAIN_SNIPPET)), |
|
309 |
chain(ste(MAIN_SNIPPET, VALID, VALID, true, null), ste(b, VALID, OVERWRITTEN, false, MAIN_SNIPPET))); |
|
310 |
assertVariables(variable("double", "a"), variable("double", "b"), variable("int", "c")); |
|
311 |
assertEval("double c = a + b;", |
|
312 |
ste(MAIN_SNIPPET, VALID, VALID, true, null), |
|
313 |
ste(c, VALID, OVERWRITTEN, false, MAIN_SNIPPET)); |
|
314 |
assertVariables(variable("double", "a"), variable("double", "b"), variable("double", "c")); |
|
315 |
assertActiveKeys(); |
|
316 |
} |
|
317 |
||
318 |
public void syntheticVariables() { |
|
319 |
assertEval("assert false;"); |
|
320 |
assertNumberOfActiveVariables(0); |
|
321 |
assertActiveKeys(); |
|
322 |
} |
|
323 |
||
324 |
public void undefinedReplaceVariable() { |
|
325 |
Snippet key = varKey(assertEval("int d = 234;", "234")); |
|
326 |
assertVariables(variable("int", "d")); |
|
327 |
String src = "undefined d;"; |
|
328 |
Snippet undefKey = varKey(assertEval(src, |
|
329 |
ste(MAIN_SNIPPET, VALID, RECOVERABLE_NOT_DEFINED, true, null), |
|
330 |
ste(key, VALID, OVERWRITTEN, false, MAIN_SNIPPET))); |
|
331 |
//assertEquals(getState().source(snippet), src); |
|
332 |
//assertEquals(snippet, undefKey); |
|
333 |
assertEquals(getState().status(undefKey), RECOVERABLE_NOT_DEFINED); |
|
334 |
List<String> unr = getState().unresolvedDependencies((VarSnippet) undefKey); |
|
335 |
assertEquals(unr.size(), 1); |
|
336 |
assertEquals(unr.get(0), "class undefined"); |
|
337 |
assertVariables(variable("undefined", "d")); |
|
338 |
} |
|
339 |
||
340 |
public void variableTypeName() { |
|
341 |
assertEquals(varKey(assertEval("\"x\"")).typeName(), "String"); |
|
342 |
||
343 |
assertEquals(varKey(assertEval("java.util.regex.Pattern.compile(\"x\")")).typeName(), "java.util.regex.Pattern"); |
|
344 |
assertEval("import java.util.regex.*;", added(VALID)); |
|
345 |
assertEquals(varKey(assertEval("java.util.regex.Pattern.compile(\"x\")")).typeName(), "Pattern"); |
|
346 |
||
347 |
assertEquals(varKey(assertEval("new java.util.ArrayList()")).typeName(), "java.util.ArrayList"); |
|
348 |
assertEval("import java.util.ArrayList;", added(VALID)); |
|
349 |
assertEquals(varKey(assertEval("new java.util.ArrayList()")).typeName(), "ArrayList"); |
|
350 |
||
351 |
assertEquals(varKey(assertEval("java.util.Locale.Category.FORMAT")).typeName(), "java.util.Locale.Category"); |
|
352 |
assertEval("import static java.util.Locale.Category;", added(VALID)); |
|
353 |
assertEquals(varKey(assertEval("java.util.Locale.Category.FORMAT")).typeName(), "Category"); |
|
354 |
} |
|
355 |
} |