1 /*
  2  * Copyright (c) 2015, 2018, 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 8144903 8177466 8191842 8211694 8213725 8239536 8257236 8252409
 27  * @summary Tests for EvaluationState.variables
 28  * @library /tools/lib
 29  * @modules jdk.compiler/com.sun.tools.javac.api
 30  *          jdk.compiler/com.sun.tools.javac.main
 31  *          jdk.jshell
 32  * @build Compiler KullaTesting TestingInputStream ExpectedDiagnostic
 33  * @run testng VariablesTest
 34  */
 35 
 36 import java.nio.file.Path;
 37 import java.nio.file.Paths;
 38 import java.util.List;
 39 import javax.tools.Diagnostic;
 40 
 41 import jdk.jshell.MethodSnippet;
 42 import jdk.jshell.Snippet;
 43 import jdk.jshell.TypeDeclSnippet;
 44 import jdk.jshell.VarSnippet;
 45 import jdk.jshell.Snippet.SubKind;
 46 import jdk.jshell.SnippetEvent;
 47 import org.testng.annotations.BeforeMethod;
 48 import org.testng.annotations.Test;
 49 
 50 import static java.util.stream.Collectors.toList;
 51 import static jdk.jshell.Snippet.Status.*;
 52 import static jdk.jshell.Snippet.SubKind.VAR_DECLARATION_SUBKIND;
 53 import static org.testng.Assert.assertEquals;
 54 import static org.testng.Assert.fail;
 55 
 56 @Test
 57 public class VariablesTest extends KullaTesting {
 58 
 59     public void noVariables() {
 60         assertNumberOfActiveVariables(0);
 61     }
 62 
 63     private void badVarValue(VarSnippet key) {
 64         try {
 65             getState().varValue(key);
 66             fail("Expected exception for: " + key.source());
 67         } catch (IllegalArgumentException e) {
 68             // ok
 69         }
 70     }
 71 
 72     public void testVarValue1() {
 73         VarSnippet v1 = varKey(assertEval("und1 a;", added(RECOVERABLE_NOT_DEFINED)));
 74         badVarValue(v1);
 75         VarSnippet v2 = varKey(assertEval("und2 a;",
 76                 ste(MAIN_SNIPPET, RECOVERABLE_NOT_DEFINED, RECOVERABLE_NOT_DEFINED, false, null),
 77                 ste(v1, RECOVERABLE_NOT_DEFINED, OVERWRITTEN, false, MAIN_SNIPPET)));
 78         badVarValue(v2);
 79         TypeDeclSnippet und = classKey(assertEval("class und2 {}",
 80                 added(VALID),
 81                 ste(v2, RECOVERABLE_NOT_DEFINED, VALID, true, MAIN_SNIPPET)));
 82         assertVarValue(v2, "null");
 83         assertDrop(und,
 84                 DiagCheck.DIAG_OK,
 85                 DiagCheck.DIAG_ERROR,
 86                 ste(und, VALID, DROPPED, true, null),
 87                 ste(v2, VALID, RECOVERABLE_NOT_DEFINED, true, und));
 88         badVarValue(v1);
 89         badVarValue(v2);
 90     }
 91 
 92     public void testVarValue2() {
 93         VarSnippet v1 = (VarSnippet) assertDeclareFail("int a = 0.0;", "compiler.err.prob.found.req");
 94         badVarValue(v1);
 95         VarSnippet v2 = varKey(assertEval("int a = 0;", added(VALID)));
 96         assertDrop(v2, ste(MAIN_SNIPPET, VALID, DROPPED, true, null));
 97         badVarValue(v2);
 98     }
 99 
100     public void testSignature1() {
101         VarSnippet v1 = varKey(assertEval("und1 a;", added(RECOVERABLE_NOT_DEFINED)));
102         assertVariableDeclSnippet(v1, "a", "und1", RECOVERABLE_NOT_DEFINED, VAR_DECLARATION_SUBKIND, 1, 0);
103         VarSnippet v2 = varKey(assertEval("und2 a;",
104                 ste(MAIN_SNIPPET, RECOVERABLE_NOT_DEFINED, RECOVERABLE_NOT_DEFINED, false, null),
105                 ste(v1, RECOVERABLE_NOT_DEFINED, OVERWRITTEN, false, MAIN_SNIPPET)));
106         assertVariableDeclSnippet(v2, "a", "und2", RECOVERABLE_NOT_DEFINED, VAR_DECLARATION_SUBKIND, 1, 0);
107         TypeDeclSnippet und = classKey(assertEval("class und2 {}",
108                 added(VALID),
109                 ste(v2, RECOVERABLE_NOT_DEFINED, VALID, true, MAIN_SNIPPET)));
110         assertVariableDeclSnippet(v2, "a", "und2", VALID, VAR_DECLARATION_SUBKIND, 0, 0);
111         assertDrop(und,
112                 DiagCheck.DIAG_OK,
113                 DiagCheck.DIAG_ERROR,
114                 ste(und, VALID, DROPPED, true, null),
115                 ste(v2, VALID, RECOVERABLE_NOT_DEFINED, true, und));
116         assertVariableDeclSnippet(v2, "a", "und2", RECOVERABLE_NOT_DEFINED, VAR_DECLARATION_SUBKIND, 1, 0);
117     }
118 
119     public void testSignature2() {
120         VarSnippet v1 = (VarSnippet) assertDeclareFail("int a = 0.0;", "compiler.err.prob.found.req");
121         assertVariableDeclSnippet(v1, "a", "int", REJECTED, SubKind.VAR_DECLARATION_WITH_INITIALIZER_SUBKIND, 0, 1);
122         VarSnippet v2 = varKey(assertEval("int a = 0;",
123                 added(VALID)));
124         assertVariableDeclSnippet(v2, "a", "int", VALID, SubKind.VAR_DECLARATION_WITH_INITIALIZER_SUBKIND, 0, 0);
125         assertDrop(v2, ste(MAIN_SNIPPET, VALID, DROPPED, true, null));
126         assertVariableDeclSnippet(v2, "a", "int", DROPPED, SubKind.VAR_DECLARATION_WITH_INITIALIZER_SUBKIND, 0, 0);
127     }
128 
129     public void variables() {
130         VarSnippet snx = varKey(assertEval("int x = 10;"));
131         VarSnippet sny = varKey(assertEval("String y = \"hi\";"));
132         VarSnippet snz = varKey(assertEval("long z;"));
133         assertVariables(variable("int", "x"), variable("String", "y"), variable("long", "z"));
134         assertVarValue(snx, "10");
135         assertVarValue(sny, "\"hi\"");
136         assertVarValue(snz, "0");
137         assertActiveKeys();
138     }
139 
140     public void variablesArray() {
141         VarSnippet sn = varKey(assertEval("int[] a = new int[12];"));
142         assertEquals(sn.typeName(), "int[]");
143         assertEval("int len = a.length;", "12");
144         assertVariables(variable("int[]", "a"), variable("int", "len"));
145         assertActiveKeys();
146     }
147 
148     public void variablesArrayOld() {
149         VarSnippet sn = varKey(assertEval("int a[] = new int[12];"));
150         assertEquals(sn.typeName(), "int[]");
151         assertEval("int len = a.length;", "12");
152         assertVariables(variable("int[]", "a"), variable("int", "len"));
153         assertActiveKeys();
154     }
155 
156     public void variablesRedefinition() {
157         Snippet x = varKey(assertEval("int x = 10;"));
158         Snippet y = varKey(assertEval("String y = \"\";", added(VALID)));
159         assertVariables(variable("int", "x"), variable("String", "y"));
160         assertActiveKeys();
161         assertEval("long x;",
162                 ste(MAIN_SNIPPET, VALID, VALID, true, null),
163                 ste(x, VALID, OVERWRITTEN, false, MAIN_SNIPPET));
164         assertVariables(variable("long", "x"), variable("String", "y"));
165         assertActiveKeys();
166         assertEval("String y;",
167                 ste(MAIN_SNIPPET, VALID, VALID, false, null),
168                 ste(y, VALID, OVERWRITTEN, false, MAIN_SNIPPET));
169         assertVariables(variable("long", "x"), variable("String", "y"));
170         assertActiveKeys();
171     }
172 
173     public void variablesTemporary() {
174         assertEval("int $1 = 10;", added(VALID));
175         assertEval("2 * $1;", added(VALID));
176         assertVariables(variable("int", "$1"), variable("int", "$2"));
177         assertActiveKeys();
178         assertEval("String y;", added(VALID));
179         assertVariables(variable("int", "$1"), variable("int", "$2"), variable("String", "y"));
180         assertActiveKeys();
181     }
182 
183     public void variablesTemporaryNull() {
184         assertEval("null;", added(VALID));
185         assertVariables(variable("Object", "$1"));
186         assertEval("(String) null;", added(VALID));
187         assertVariables(variable("Object", "$1"), variable("String", "$2"));
188         assertActiveKeys();
189         assertEval("\"\";", added(VALID));
190         assertVariables(
191                 variable("Object", "$1"),
192                 variable("String", "$2"),
193                 variable("String", "$3"));
194         assertActiveKeys();
195     }
196 
197     public void variablesTemporaryArrayOfCapturedType() {
198         assertEval("class Test<T> { T[][] get() { return null; } }", added(VALID));
199         assertEval("Test<? extends String> test() { return new Test<>(); }", added(VALID));
200         assertEval("test().get()", added(VALID));
201         assertVariables(variable("String[][]", "$1"));
202         assertEval("\"\".getClass().getEnumConstants()", added(VALID));
203         assertVariables(variable("String[][]", "$1"), variable("String[]", "$2"));
204         assertActiveKeys();
205     }
206 
207     public void variablesClassReplace() {
208         assertEval("import java.util.*;", added(VALID));
209         Snippet var = varKey(assertEval("List<Integer> list = new ArrayList<>();", "[]",
210                 added(VALID)));
211         assertVariables(variable("List<Integer>", "list"));
212         assertEval("class List {}",
213                 DiagCheck.DIAG_OK,
214                 DiagCheck.DIAG_ERROR,
215                 added(VALID),
216                 ste(var, VALID, RECOVERABLE_NOT_DEFINED, true, MAIN_SNIPPET));
217         assertVariables();
218         assertEval("List list = new List();",
219                 DiagCheck.DIAG_OK, DiagCheck.DIAG_IGNORE,
220                 ste(MAIN_SNIPPET, RECOVERABLE_NOT_DEFINED, VALID, true, null),
221                 ste(var, RECOVERABLE_NOT_DEFINED, OVERWRITTEN, false, MAIN_SNIPPET));
222         assertVariables(variable("List", "list"));
223         assertActiveKeys();
224     }
225 
226     public void variablesErrors() {
227         assertDeclareFail("String;", new ExpectedDiagnostic("compiler.err.cant.resolve.location", 0, 6, 0, -1, -1, Diagnostic.Kind.ERROR));
228         assertNumberOfActiveVariables(0);
229         assertActiveKeys();
230     }
231 
232     public void variablesUnresolvedActiveFailed() {
233         VarSnippet key = varKey(assertEval("und x;", added(RECOVERABLE_NOT_DEFINED)));
234         assertVariableDeclSnippet(key, "x", "und", RECOVERABLE_NOT_DEFINED, VAR_DECLARATION_SUBKIND, 1, 0);
235         assertUnresolvedDependencies1(key, RECOVERABLE_NOT_DEFINED, "class und");
236         assertNumberOfActiveVariables(1);
237         assertActiveKeys();
238     }
239 
240     public void variablesUnresolvedError() {
241         assertDeclareFail("und y = null;", new ExpectedDiagnostic("compiler.err.cant.resolve.location", 0, 3, 0, -1, -1, Diagnostic.Kind.ERROR));
242         assertNumberOfActiveVariables(0);
243         assertActiveKeys();
244     }
245 
246     public void variablesMultiByteCharacterType() {
247         assertEval("class \u3042 {}");
248         assertEval("\u3042 \u3042 = null;", added(VALID));
249         assertVariables(variable("\u3042", "\u3042"));
250         assertEval("new \u3042()", added(VALID));
251         assertVariables(variable("\u3042", "\u3042"), variable("\u3042", "$1"));
252 
253         assertEval("class \u3042\u3044\u3046\u3048\u304a {}");
254         assertEval("\u3042\u3044\u3046\u3048\u304a \u3042\u3044\u3046\u3048\u304a = null;", added(VALID));
255         assertVariables(variable("\u3042", "\u3042"), variable("\u3042", "$1"),
256                 variable("\u3042\u3044\u3046\u3048\u304a", "\u3042\u3044\u3046\u3048\u304a"));
257         assertEval("new \u3042\u3044\u3046\u3048\u304a();");
258         assertVariables(variable("\u3042", "\u3042"), variable("\u3042", "$1"),
259                 variable("\u3042\u3044\u3046\u3048\u304a", "\u3042\u3044\u3046\u3048\u304a"),
260                 variable("\u3042\u3044\u3046\u3048\u304a", "$2"));
261         assertActiveKeys();
262     }
263 
264     @Test(enabled = false) // TODO 8081689
265     public void methodVariablesAreNotVisible() {
266         Snippet foo = varKey(assertEval("int foo() {" +
267                         "int x = 10;" +
268                         "int y = 2 * x;" +
269                         "return x * y;" +
270                         "}", added(VALID)));
271         assertNumberOfActiveVariables(0);
272         assertActiveKeys();
273         assertEval("int x = 10;", "10");
274         assertEval("int foo() {" +
275                         "int y = 2 * x;" +
276                         "return x * y;" +
277                         "}",
278                 ste(foo, VALID, VALID, false, null));
279         assertVariables(variable("int", "x"));
280         assertActiveKeys();
281         assertEval("foo();", "200");
282         assertVariables(variable("int", "x"), variable("int", "$1"));
283         assertActiveKeys();
284     }
285 
286     @Test(enabled = false) // TODO 8081689
287     public void classFieldsAreNotVisible() {
288         Snippet key = classKey(assertEval("class clazz {" +
289                         "int x = 10;" +
290                         "int y = 2 * x;" +
291                         "}"));
292         assertNumberOfActiveVariables(0);
293         assertEval("int x = 10;", "10");
294         assertActiveKeys();
295         assertEval(
296                 "class clazz {" +
297                         "int y = 2 * x;" +
298                         "}",
299                 ste(key, VALID, VALID, true, null));
300         assertVariables(variable("int", "x"));
301         assertEval("new clazz().y;", "20");
302         assertVariables(variable("int", "x"), variable("int", "$1"));
303         assertActiveKeys();
304     }
305 
306     public void multiVariables() {
307         List<SnippetEvent> abc = assertEval("int a, b, c = 10;",
308                 DiagCheck.DIAG_OK, DiagCheck.DIAG_OK,
309                 chain(added(VALID)),
310                 chain(added(VALID)),
311                 chain(added(VALID)));
312         Snippet a = abc.get(0).snippet();
313         Snippet b = abc.get(1).snippet();
314         Snippet c = abc.get(2).snippet();
315         assertVariables(variable("int", "a"), variable("int", "b"), variable("int", "c"));
316         assertEval("double a = 1.4, b = 8.8;", DiagCheck.DIAG_OK, DiagCheck.DIAG_OK,
317                 chain(ste(MAIN_SNIPPET, VALID, VALID, true, null), ste(a, VALID, OVERWRITTEN, false, MAIN_SNIPPET)),
318                 chain(ste(MAIN_SNIPPET, VALID, VALID, true, null), ste(b, VALID, OVERWRITTEN, false, MAIN_SNIPPET)));
319         assertVariables(variable("double", "a"), variable("double", "b"), variable("int", "c"));
320         assertEval("double c = a + b;",
321                 ste(MAIN_SNIPPET, VALID, VALID, true, null),
322                 ste(c, VALID, OVERWRITTEN, false, MAIN_SNIPPET));
323         assertVariables(variable("double", "a"), variable("double", "b"), variable("double", "c"));
324         assertActiveKeys();
325     }
326 
327     public void syntheticVariables() {
328         assertEval("assert false;");
329         assertNumberOfActiveVariables(0);
330         assertActiveKeys();
331     }
332 
333     public void undefinedReplaceVariable() {
334         Snippet key = varKey(assertEval("int d = 234;", "234"));
335         assertVariables(variable("int", "d"));
336         String src = "undefined d;";
337         Snippet undefKey = varKey(assertEval(src,
338                 ste(MAIN_SNIPPET, VALID, RECOVERABLE_NOT_DEFINED, true, null),
339                 ste(key, VALID, OVERWRITTEN, false, MAIN_SNIPPET)));
340         //assertEquals(getState().source(snippet), src);
341         //assertEquals(snippet, undefKey);
342         assertEquals(getState().status(undefKey), RECOVERABLE_NOT_DEFINED);
343         List<String> unr = getState().unresolvedDependencies((VarSnippet) undefKey).collect(toList());;
344         assertEquals(unr.size(), 1);
345         assertEquals(unr.get(0), "class undefined");
346         assertVariables(variable("undefined", "d"));
347     }
348 
349     public void lvti() {
350         assertEval("var d = 234;", "234");
351         assertEval("class Test<T> { T[][] get() { return null; } }", added(VALID));
352         assertEval("Test<? extends String> test() { return new Test<>(); }", added(VALID));
353         assertEval("var t = test().get();", added(VALID));
354         assertEval("<Z extends Runnable & CharSequence> Z get1() { return null; }", added(VALID));
355         assertEval("var i1 = get1();", added(VALID));
356         assertEval("void t1() { i1.run(); i1.length(); }", added(VALID));
357         assertEval("i1 = 1;", DiagCheck.DIAG_ERROR, DiagCheck.DIAG_OK, ste(MAIN_SNIPPET, NONEXISTENT, REJECTED, false, null));
358         assertEval("<Z extends Number & CharSequence> Z get2() { return null; }", added(VALID));
359         assertEval("var i2 = get2();", added(VALID));
360         assertEval("void t2() { i2.length(); }", added(VALID));
361         assertEval("var r1 = new Runnable() { public void run() { } public String get() { return \"good\"; } };", added(VALID));
362         assertEval("Runnable r2 = r1;");
363         assertEval("r1.get()", "\"good\"");
364         assertEval("var v = r1.get();", "\"good\"");
365         assertEval("var r3 = new java.util.ArrayList<String>(42) { public String get() { return \"good\"; } };", added(VALID));
366         assertEval("r3.get()", "\"good\"");
367         assertEval("class O { public class Inner { public String test() { return \"good\"; } } }");
368         assertEval("var r4 = new O().new Inner() { public String get() { return \"good\"; } };");
369         assertEval("r4.get()", "\"good\"");
370         assertEval("class O2 { public class Inner { public Inner(int i) { } public String test() { return \"good\"; } } }");
371         assertEval("var r5 = new O2().new Inner(1) { public String get() { return \"good\"; } };");
372         assertEval("r5.get()", "\"good\"");
373         assertEval("<Z> Z identity(Z z) { return z; }");
374         assertEval("var r6 = identity(new Object() { String s = \"good\"; });");
375         assertEval("r6.s", "\"good\"");
376         assertEval("interface I<B, C> { C get(B b); }");
377         assertEval("<A, B, C> C cascade(A a, I<A, B> c1, I<B, C> c2) { return c2.get(c1.get(a)); }");
378         assertEval("var r7 = cascade(\"good\", a -> new Object() { String s = a; }, b -> new java.util.ArrayList<String>(5) { String s = b.s; });");
379         assertEval("r7.s", "\"good\"");
380         assertEval("var r8 = cascade(\"good\", a -> new Object() { String s = a; public String getS() { return s; } }, b -> new java.util.ArrayList<String>(5) { String s = b.getS(); public String getS() { return s; } });");
381         assertEval("r8.getS()", "\"good\"");
382         assertEval("var r9 = new Object() { class T { class Inner { public String g() { return outer(); } } public String outer() { return \"good\"; } public String test() { return new Inner() {}.g(); } } public String test() { return new T().test(); } };");
383         assertEval("r9.test()", "\"good\"");
384         assertEval("var nested1 = new Object() { class N { public String get() { return \"good\"; } } };");
385         assertEval("nested1.new N().get()", "\"good\"");
386         assertEval("var nested2 = cascade(\"good\", a -> new Object() { abstract class G { abstract String g(); } G g = new G() { String g() { return a; } }; }, b -> new java.util.ArrayList<String>(5) { String s = b.g.g(); });");
387         assertEval("nested2.s", "\"good\"");
388         assertEval("<A, B> B convert(A a, I<A, B> c) { return c.get(a); }");
389         assertEval("var r10 = convert(\"good\", a -> new api.C(12) { public String val = \"\" + i + s + l + a; } );");
390         assertEval("r10.val", "\"12empty[empty]good\"");
391         assertEval("var r11 = convert(\"good\", a -> new api.C(\"a\") { public String val = \"\" + i + s + l + a; } );");
392         assertEval("r11.val", "\"3a[empty]good\"");
393         assertEval("import api.C;");
394         assertEval("var r12 = convert(\"good\", a -> new C(java.util.List.of(\"a\")) { public String val = \"\" + i + s + l + a; } );");
395         assertEval("r12.val", "\"4empty[a]good\"");
396         assertEval("var r13 = convert(\"good\", a -> new api.G<String>(java.util.List.of(\"b\")) { public String val = \"\" + l + a; } );");
397         assertEval("r13.val", "\"[b]good\"");
398         assertEval("var r14 = convert(\"good\", a -> new api.J<String>() { public java.util.List<String> get() { return java.util.List.of(a, \"c\"); } } );");
399         assertEval("r14.get()", "[good, c]");
400         assertEval("var r15a = new java.util.ArrayList<String>();");
401         assertEval("r15a.add(\"a\");");
402         assertEval("var r15b = r15a.get(0);");
403         assertEval("r15b", "\"a\"");
404         assertEval("class Z { }");
405         assertEval("var r16a = new Z();");
406         assertEval("var r16b = (Runnable) () -> {int r16b_; int r16b__;};");
407         assertEval("class $ { }");
408         assertEval("var r16c = new $();");
409         assertEval("$ r16d() { return null; }");
410         assertEval("var r16d = r16d();");
411     }
412 
413     public void test8191842() {
414         assertEval("import java.util.stream.*;");
415         assertEval("var list = Stream.of(1, 2, 3).map(j -> new Object() { int i = j; }).collect(Collectors.toList());");
416         assertEval("list.stream().map(a -> String.valueOf(a.i)).collect(Collectors.joining(\", \"));", "\"1, 2, 3\"");
417     }
418 
419     public void lvtiRecompileDependentsWithIntersectionTypes() {
420         assertEval("<Z extends Runnable & CharSequence> Z get1() { return null; }", added(VALID));
421         assertEval("var i1 = get1();", added(VALID));
422         MethodSnippet get2 = methodKey(assertEval("<Z extends Runnable & Stream> Z get2() { return null; }",
423             ste(MAIN_SNIPPET, NONEXISTENT, RECOVERABLE_NOT_DEFINED, false, null)));
424         assertEval("import java.util.stream.*;", added(VALID),
425                                                  ste(get2, RECOVERABLE_NOT_DEFINED, VALID, true, MAIN_SNIPPET));
426         assertEval("void t1() { i1.run(); i1.length(); }", added(VALID));
427         assertEval("var i2 = get2();", added(VALID));
428         assertEval("void t2() { i2.run(); i2.count(); }", added(VALID));
429     }
430 
431     public void arrayInit() {
432         assertEval("int[] d = {1, 2, 3};");
433     }
434 
435     public void testAnonymousVar() {
436         assertEval("new Object() { public String get() { return \"a\"; } }");
437         assertEval("$1.get()", "\"a\"");
438     }
439 
440     public void testIntersectionVar() {
441         assertEval("<Z extends Runnable & CharSequence> Z get() { return null; }", added(VALID));
442         assertEval("get();", added(VALID));
443         assertEval("void t1() { $1.run(); $1.length(); }", added(VALID));
444     }
445 
446     public void multipleCaptures() {
447         assertEval("class D { D(int foo, String bar) { this.foo = foo; this.bar = bar; } int foo; String bar; } ");
448         assertEval("var d = new D(34, \"hi\") { String z = foo + bar; };");
449         assertEval("d.z", "\"34hi\"");
450     }
451 
452     public void multipleAnonymous() {
453         VarSnippet v1 = varKey(assertEval("new Object() { public int i = 42; public int i1 = i; public int m1() { return i1; } };"));
454         VarSnippet v2 = varKey(assertEval("new Object() { public int i = 42; public int i2 = i; public int m2() { return i2; } };"));
455         assertEval(v1.name() + ".i", "42");
456         assertEval(v1.name() + ".i1", "42");
457         assertEval(v1.name() + ".m1()", "42");
458         assertDeclareFail(v1.name() + ".i2",
459                           new ExpectedDiagnostic("compiler.err.cant.resolve.location", 0, 5, 2,
460                                                  -1, -1, Diagnostic.Kind.ERROR));
461         assertEval(v2.name() + ".i", "42");
462         assertEval(v2.name() + ".i2", "42");
463         assertEval(v2.name() + ".m2()", "42");
464         assertDeclareFail(v2.name() + ".i1",
465                           new ExpectedDiagnostic("compiler.err.cant.resolve.location", 0, 5, 2,
466                                                  -1, -1, Diagnostic.Kind.ERROR));
467     }
468 
469     public void displayName() {
470         assertVarDisplayName("var v1 = 234;", "int");
471         assertVarDisplayName("var v2 = new int[] {234};", "int[]");
472         assertEval("<Z extends Runnable & CharSequence> Z get() { return null; }", added(VALID));
473         assertVarDisplayName("var v3 = get();", "CharSequence&Runnable");
474         assertVarDisplayName("var v4a = new java.util.ArrayList<String>();", "java.util.ArrayList<String>");
475         assertEval("v4a.add(\"a\");");
476         assertVarDisplayName("var v4b = v4a.get(0);", "String");
477         assertVarDisplayName("var v5 = new Object() { };", "<anonymous class extending Object>");
478         assertVarDisplayName("var v6 = new Runnable() { public void run() { } };", "<anonymous class implementing Runnable>");
479     }
480 
481     public void varType() {
482         assertEval("import java.util.*;");
483         var firstVar = varKey(assertEval("var v1 = List.of(1);", added(VALID)));
484         assertEval("import list.List;", DiagCheck.DIAG_OK, DiagCheck.DIAG_ERROR, added(VALID),
485                                         ste(firstVar, VALID, RECOVERABLE_NOT_DEFINED, true, MAIN_SNIPPET));
486         assertEval("var v2 = java.util.List.of(1);", added(VALID));
487         assertEval("v2", "[1]");
488     }
489 
490     public void varDeclNoInit() {
491         assertVarDeclNoInit("byte", "b",  "0");
492         assertVarDeclNoInit("short", "h",  "0");
493         assertVarDeclNoInit("int", "i",  "0");
494         assertVarDeclNoInit("long", "l",  "0");
495         assertVarDeclNoInit("float", "f",  "0.0");
496         assertVarDeclNoInit("double", "d",  "0.0");
497         assertVarDeclNoInit("boolean", "n",  "false");
498         assertVarDeclNoInit("char", "c",  "'\\000'");
499         assertVarDeclNoInit("Object", "o",  "null");
500         assertVarDeclNoInit("String", "s", "null");
501     }
502 
503     public void varDeclRedefNoInit() {
504         assertVarDeclRedefNoInit("byte", "b", "1", "0");
505         assertVarDeclRedefNoInit("short", "h", "2", "0");
506         assertVarDeclRedefNoInit("int", "i", "3", "0");
507         assertVarDeclRedefNoInit("long", "l", "4L", IGNORE_VALUE, "0");
508         assertVarDeclRedefNoInit("float", "f", "3.14f", IGNORE_VALUE, "0.0");
509         assertVarDeclRedefNoInit("double", "d", "3.1415926", "0.0");
510         assertVarDeclRedefNoInit("boolean", "n", "true", "false");
511         assertVarDeclRedefNoInit("char", "c", "'x'", "'\\000'");
512         assertVarDeclRedefNoInit("Object", "o", "new Object()", IGNORE_VALUE, "null");
513         assertVarDeclRedefNoInit("String", "s", "\"hi\"", "null");
514     }
515 
516     public void badPkgVarDecl() {
517         Compiler compiler = new Compiler();
518         Path nopkgdirpath = Paths.get("cp", "xyz");
519         compiler.compile(nopkgdirpath,
520                 "public class TestZ { public static int V = 0; }\n");
521         assertDeclareFail("import static xyz.TestZ.V;",
522                         "compiler.err.cant.access");
523 
524 
525         VarSnippet v1 = varKey(assertEval("var v = xyz.TestZ.V;", IGNORE_VALUE, null,
526                 DiagCheck.DIAG_ERROR, DiagCheck.DIAG_OK, added(RECOVERABLE_NOT_DEFINED)));
527         assertVariableDeclSnippet(v1, "v", "java.lang.Object", RECOVERABLE_NOT_DEFINED, SubKind.VAR_DECLARATION_WITH_INITIALIZER_SUBKIND, 0, 1);
528         assertEval("1+1", "2");
529     }
530 
531     private void assertVarDeclRedefNoInit(String typeName, String name, String value, String dvalue) {
532         assertVarDeclRedefNoInit(typeName, name, value, value, dvalue);
533     }
534 
535     private void assertVarDeclRedefNoInit(String typeName, String name, String value, String rvalue, String dvalue) {
536         VarSnippet vs = varKey(assertEval(typeName + " " + name + " = " + value + ";", rvalue));
537         assertVarDeclNoInit(typeName,  name, dvalue,
538                 ste(vs, VALID, VALID, false, null),
539                 ste(vs, VALID, OVERWRITTEN, false, MAIN_SNIPPET));
540     }
541 
542     private VarSnippet assertVarDeclNoInit(String typeName, String name, String dvalue) {
543         return assertVarDeclNoInit(typeName, name, dvalue, added(VALID));
544     }
545 
546     private VarSnippet assertVarDeclNoInit(String typeName, String name, String dvalue, STEInfo mainInfo, STEInfo... updates) {
547         VarSnippet vs = varKey(assertEval(typeName + " " + name + ";", dvalue, mainInfo, updates));
548         assertEquals(vs.typeName(), typeName);
549         assertEval(name, dvalue, added(VALID));
550         return vs;
551     }
552 
553     private void assertVarDisplayName(String var, String typeName) {
554         assertEquals(varKey(assertEval(var)).typeName(), typeName);
555     }
556 
557     @BeforeMethod
558     @Override
559     public void setUp() {
560         Path path = Paths.get("cp");
561         Compiler compiler = new Compiler();
562         compiler.compile(path,
563                 "package api;\n" +
564                 "\n" +
565                 "import java.util.List;\n" +
566                 "\n" +
567                 "public class C {\n" +
568                 "   public int i;\n" +
569                 "   public String s;\n" +
570                 "   public List<String> l;\n" +
571                 "   public C(int i) {\n" +
572                 "       this.i = i;\n" +
573                 "       this.s = \"empty\";\n" +
574                 "       this.l = List.of(\"empty\");\n" +
575                 "   }\n" +
576                 "   public C(String s) {\n" +
577                 "       this.i = 3;\n" +
578                 "       this.s = s;\n" +
579                 "       this.l = List.of(\"empty\");\n" +
580                 "   }\n" +
581                 "   public C(List<String> l) {\n" +
582                 "       this.i = 4;\n" +
583                 "       this.s = \"empty\";\n" +
584                 "       this.l = l;\n" +
585                 "   }\n" +
586                 "}\n",
587                 "package api;\n" +
588                 "\n" +
589                 "import java.util.List;\n" +
590                 "\n" +
591                 "public class G<T> {\n" +
592                 "   public List<T> l;\n" +
593                 "   public G(List<T> l) {\n" +
594                 "       this.l = l;\n" +
595                 "   }\n" +
596                 "}\n",
597                 "package api;\n" +
598                 "\n" +
599                 "import java.util.List;\n" +
600                 "\n" +
601                 "public interface J<T> {\n" +
602                 "   public List<T> get();\n" +
603                 "}\n",
604                 "package list;\n" +
605                 "\n" +
606                 "public class List {\n" +
607                 "}\n");
608         String tpath = compiler.getPath(path).toString();
609         setUp(b -> b
610                 .remoteVMOptions("--class-path", tpath)
611                 .compilerOptions("--class-path", tpath));
612     }
613 
614     public void varIntersection() {
615         assertEval("interface Marker {}");
616         assertEval("var v = (Marker & Runnable) () -> {};", added(VALID));
617         assertEval("v.run()");
618     }
619 
620 }