|
1 /* |
|
2 * Copyright (c) 2016, 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 8173232 |
|
26 * @summary Test of forward referencing of snippets. |
|
27 * @build KullaTesting TestingInputStream |
|
28 * @run testng ForwardReferenceTest |
|
29 */ |
|
30 |
|
31 import java.util.List; |
|
32 import jdk.jshell.Snippet; |
|
33 import jdk.jshell.MethodSnippet; |
|
34 import jdk.jshell.VarSnippet; |
|
35 import jdk.jshell.DeclarationSnippet; |
|
36 import org.testng.annotations.Test; |
|
37 |
|
38 import jdk.jshell.SnippetEvent; |
|
39 import jdk.jshell.UnresolvedReferenceException; |
|
40 import static org.testng.Assert.assertEquals; |
|
41 import static jdk.jshell.Snippet.Status.*; |
|
42 import static org.testng.Assert.assertTrue; |
|
43 |
|
44 @Test |
|
45 public class ForwardReferenceTest extends KullaTesting { |
|
46 |
|
47 public void testOverwriteMethodForwardReferenceClass() { |
|
48 Snippet k1 = methodKey(assertEval("int q(Boo b) { return b.x; }", |
|
49 added(RECOVERABLE_NOT_DEFINED))); |
|
50 assertUnresolvedDependencies1((MethodSnippet) k1, RECOVERABLE_NOT_DEFINED, "class Boo"); |
|
51 assertEval("class Boo { int x = 55; }", |
|
52 added(VALID), |
|
53 ste(k1, RECOVERABLE_NOT_DEFINED, VALID, true, null)); |
|
54 assertMethodDeclSnippet((MethodSnippet) k1, "q", "(Boo)int", VALID, 0, 0); |
|
55 assertEval("q(new Boo());", "55"); |
|
56 assertActiveKeys(); |
|
57 } |
|
58 |
|
59 public void testOverwriteMethodForwardReferenceClassImport() { |
|
60 MethodSnippet k1 = methodKey(assertEval("int ff(List lis) { return lis.size(); }", |
|
61 added(RECOVERABLE_NOT_DEFINED))); |
|
62 assertUnresolvedDependencies1(k1, RECOVERABLE_NOT_DEFINED, "class List"); |
|
63 assertEval("import java.util.*;", |
|
64 added(VALID), |
|
65 ste(k1, RECOVERABLE_NOT_DEFINED, VALID, true, null)); |
|
66 assertMethodDeclSnippet(k1, "ff", "(List)int", VALID, 0, 0); |
|
67 assertEval("ff(new ArrayList());", "0"); |
|
68 assertActiveKeys(); |
|
69 } |
|
70 |
|
71 public void testForwardVarToMethod() { |
|
72 DeclarationSnippet t = methodKey(assertEval("int t() { return x; }", added(RECOVERABLE_DEFINED))); |
|
73 assertUnresolvedDependencies1(t, RECOVERABLE_DEFINED, "variable x"); |
|
74 assertEvalUnresolvedException("t();", "t", 1, 0); |
|
75 Snippet x = varKey(assertEval("int x = 33;", "33", |
|
76 added(VALID), |
|
77 ste(t, RECOVERABLE_DEFINED, VALID, false, null))); |
|
78 assertEval("t();", "33"); |
|
79 assertEval("double x = 0.88;", |
|
80 "0.88", null, |
|
81 DiagCheck.DIAG_OK, |
|
82 DiagCheck.DIAG_ERROR, |
|
83 ste(MAIN_SNIPPET, VALID, VALID, true, null), |
|
84 ste(x, VALID, OVERWRITTEN, false, MAIN_SNIPPET), |
|
85 ste(t, VALID, RECOVERABLE_DEFINED, false, MAIN_SNIPPET)); |
|
86 assertEvalUnresolvedException("t();", "t", 0, 1); |
|
87 assertActiveKeys(); |
|
88 } |
|
89 |
|
90 public void testForwardMethodToMethod() { |
|
91 Snippet t = methodKey(assertEval("int t() { return f(); }", added(RECOVERABLE_DEFINED))); |
|
92 Snippet f = methodKey(assertEval("int f() { return g(); }", |
|
93 added(RECOVERABLE_DEFINED), |
|
94 ste(t, RECOVERABLE_DEFINED, VALID, false, null))); |
|
95 assertUnresolvedDependencies1((DeclarationSnippet) f, RECOVERABLE_DEFINED, "method g()"); |
|
96 assertEvalUnresolvedException("t();", "f", 1, 0); |
|
97 Snippet g = methodKey(assertEval("int g() { return 55; }", |
|
98 added(VALID), |
|
99 ste(f, RECOVERABLE_DEFINED, VALID, false, null))); |
|
100 assertEval("t();", "55"); |
|
101 assertEval("double g() { return 3.14159; }", |
|
102 DiagCheck.DIAG_OK, |
|
103 DiagCheck.DIAG_ERROR, |
|
104 ste(MAIN_SNIPPET, VALID, VALID, true, null), |
|
105 ste(g, VALID, OVERWRITTEN, false, MAIN_SNIPPET), |
|
106 ste(f, VALID, RECOVERABLE_DEFINED, false, MAIN_SNIPPET)); |
|
107 DeclarationSnippet exsn = assertEvalUnresolvedException("t();", "f", 0, 1); |
|
108 assertTrue(exsn == f, "Identity must not change"); |
|
109 assertActiveKeys(); |
|
110 } |
|
111 |
|
112 public void testForwardClassToMethod() { |
|
113 DeclarationSnippet t = methodKey(assertEval("int t() { return new A().f(); }", added(RECOVERABLE_DEFINED))); |
|
114 assertUnresolvedDependencies1(t, RECOVERABLE_DEFINED, "class A"); |
|
115 assertEvalUnresolvedException("t();", "t", 1, 0); |
|
116 Snippet a = classKey(assertEval( |
|
117 "class A {\n" + |
|
118 " int f() { return 10; }\n" + |
|
119 "}", |
|
120 added(VALID), |
|
121 ste(t, RECOVERABLE_DEFINED, VALID, false, null))); |
|
122 assertEval("t();", "10"); |
|
123 assertEval( |
|
124 "class A {\n" + |
|
125 " double f() { return 88.0; }\n" + |
|
126 "}", |
|
127 DiagCheck.DIAG_OK, |
|
128 DiagCheck.DIAG_ERROR, |
|
129 ste(MAIN_SNIPPET, VALID, VALID, true, null), |
|
130 ste(a, VALID, OVERWRITTEN, false, MAIN_SNIPPET), |
|
131 ste(t, VALID, RECOVERABLE_DEFINED, false, MAIN_SNIPPET)); |
|
132 assertEvalUnresolvedException("t();", "t", 0, 1); |
|
133 assertActiveKeys(); |
|
134 } |
|
135 |
|
136 public void testForwardVarToClass() { |
|
137 DeclarationSnippet a = classKey(assertEval("class A { int f() { return g; } }", added(RECOVERABLE_DEFINED))); |
|
138 assertUnresolvedDependencies1(a, RECOVERABLE_DEFINED, "variable g"); |
|
139 Snippet g = varKey(assertEval("int g = 10;", "10", |
|
140 added(VALID), |
|
141 ste(a, RECOVERABLE_DEFINED, VALID, false, null))); |
|
142 assertEval("new A().f();", "10"); |
|
143 assertEval("double g = 10;", "10.0", null, |
|
144 DiagCheck.DIAG_OK, |
|
145 DiagCheck.DIAG_ERROR, |
|
146 ste(MAIN_SNIPPET, VALID, VALID, true, null), |
|
147 ste(g, VALID, OVERWRITTEN, false, MAIN_SNIPPET), |
|
148 ste(a, VALID, RECOVERABLE_DEFINED, false, MAIN_SNIPPET)); |
|
149 assertUnresolvedDependencies(a, 0); |
|
150 assertActiveKeys(); |
|
151 } |
|
152 |
|
153 public void testForwardVarToClassGeneric() { |
|
154 DeclarationSnippet a = classKey(assertEval("class A<T> { final T x; A(T v) { this.x = v; } ; T get() { return x; } int core() { return g; } }", added(RECOVERABLE_DEFINED))); |
|
155 assertUnresolvedDependencies1(a, RECOVERABLE_DEFINED, "variable g"); |
|
156 |
|
157 List<SnippetEvent> events = assertEval("A<String> as = new A<>(\"hi\");", null, |
|
158 UnresolvedReferenceException.class, DiagCheck.DIAG_OK, DiagCheck.DIAG_OK, null); |
|
159 SnippetEvent ste = events.get(0); |
|
160 Snippet assn = ste.snippet(); |
|
161 DeclarationSnippet unsn = ((UnresolvedReferenceException) ste.exception()).getSnippet(); |
|
162 assertEquals(unsn.name(), "A", "Wrong with unresolved"); |
|
163 assertEquals(getState().unresolvedDependencies(unsn).count(), 1, "Wrong size unresolved"); |
|
164 assertEquals(getState().diagnostics(unsn).count(), 0L, "Expected no diagnostics"); |
|
165 |
|
166 Snippet g = varKey(assertEval("int g = 10;", "10", |
|
167 added(VALID), |
|
168 ste(a, RECOVERABLE_DEFINED, VALID, false, MAIN_SNIPPET))); |
|
169 assertEval("A<String> as = new A<>(\"low\");", |
|
170 ste(MAIN_SNIPPET, VALID, VALID, false, null), |
|
171 ste(assn, VALID, OVERWRITTEN, false, MAIN_SNIPPET)); |
|
172 assertEval("as.get();", "\"low\""); |
|
173 assertUnresolvedDependencies(a, 0); |
|
174 assertActiveKeys(); |
|
175 } |
|
176 |
|
177 public void testForwardVarToClassExtendsImplements() { |
|
178 DeclarationSnippet ik = classKey(assertEval("interface I { default int ii() { return 1; } }", added(VALID))); |
|
179 DeclarationSnippet jk = classKey(assertEval("interface J { default int jj() { return 2; } }", added(VALID))); |
|
180 DeclarationSnippet ck = classKey(assertEval("class C { int cc() { return 3; } }", added(VALID))); |
|
181 DeclarationSnippet dk = classKey(assertEval("class D extends C implements I,J { int dd() { return g; } }", added(RECOVERABLE_DEFINED))); |
|
182 DeclarationSnippet ek = classKey(assertEval("class E extends D { int ee() { return 5; } }", added(VALID))); |
|
183 assertUnresolvedDependencies1(dk, RECOVERABLE_DEFINED, "variable g"); |
|
184 assertEvalUnresolvedException("new D();", "D", 1, 0); |
|
185 assertEvalUnresolvedException("new E();", "D", 1, 0); |
|
186 VarSnippet g = varKey(assertEval("int g = 10;", "10", |
|
187 added(VALID), |
|
188 ste(dk, RECOVERABLE_DEFINED, VALID, false, MAIN_SNIPPET))); |
|
189 assertEval("E e = new E();"); |
|
190 assertDrop(g, |
|
191 ste(g, VALID, DROPPED, true, null), |
|
192 ste(dk, VALID, RECOVERABLE_DEFINED, false, g)); |
|
193 assertEvalUnresolvedException("new D();", "D", 1, 0); |
|
194 assertEvalUnresolvedException("new E();", "D", 1, 0); |
|
195 assertEval("e.ee();", "5"); |
|
196 assertEvalUnresolvedException("e.dd();", "D", 1, 0); |
|
197 assertEval("e.cc();", "3"); |
|
198 assertEval("e.jj();", "2"); |
|
199 assertEval("e.ii();", "1"); |
|
200 assertActiveKeys(); |
|
201 } |
|
202 |
|
203 public void testForwardVarToInterface() { |
|
204 DeclarationSnippet i = classKey(assertEval("interface I { default int f() { return x; } }", added(RECOVERABLE_DEFINED))); |
|
205 assertUnresolvedDependencies1(i, RECOVERABLE_DEFINED, "variable x"); |
|
206 DeclarationSnippet c = classKey(assertEval("class C implements I { int z() { return 2; } }", added(VALID))); |
|
207 assertEval("C c = new C();"); |
|
208 assertEval("c.z();", "2"); |
|
209 assertEvalUnresolvedException("c.f()", "I", 1, 0); |
|
210 Snippet g = varKey(assertEval("int x = 55;", "55", |
|
211 added(VALID), |
|
212 ste(i, RECOVERABLE_DEFINED, VALID, false, null))); |
|
213 assertEval("c.f();", "55"); |
|
214 assertUnresolvedDependencies(i, 0); |
|
215 assertActiveKeys(); |
|
216 } |
|
217 |
|
218 public void testForwardVarToEnum() { |
|
219 DeclarationSnippet a = classKey(assertEval("enum E { Q, W, E; float ff() { return fff; } }", added(RECOVERABLE_DEFINED))); |
|
220 assertUnresolvedDependencies1(a, RECOVERABLE_DEFINED, "variable fff"); |
|
221 Snippet g = varKey(assertEval("float fff = 4.5f;", "4.5", |
|
222 added(VALID), |
|
223 ste(a, RECOVERABLE_DEFINED, VALID, false, null))); |
|
224 assertEval("E.Q.ff();", "4.5"); |
|
225 assertEval("double fff = 3.3;", "3.3", null, |
|
226 DiagCheck.DIAG_OK, |
|
227 DiagCheck.DIAG_ERROR, |
|
228 ste(MAIN_SNIPPET, VALID, VALID, true, null), |
|
229 ste(g, VALID, OVERWRITTEN, false, MAIN_SNIPPET), |
|
230 ste(a, VALID, RECOVERABLE_DEFINED, false, MAIN_SNIPPET)); |
|
231 assertUnresolvedDependencies(a, 0); |
|
232 assertActiveKeys(); |
|
233 } |
|
234 |
|
235 public void testForwardMethodToClass() { |
|
236 DeclarationSnippet a = classKey(assertEval("class A { int f() { return g(); } }", added(RECOVERABLE_DEFINED))); |
|
237 assertUnresolvedDependencies1(a, RECOVERABLE_DEFINED, "method g()"); |
|
238 assertEval("A foo() { return null; }"); |
|
239 assertEvalUnresolvedException("new A();", "A", 1, 0); |
|
240 Snippet g = methodKey(assertEval("int g() { return 10; }", |
|
241 added(VALID), |
|
242 ste(a, RECOVERABLE_DEFINED, VALID, false, null))); |
|
243 assertEval("new A().f();", "10"); |
|
244 assertEval("double g() { return 10; }", |
|
245 DiagCheck.DIAG_OK, |
|
246 DiagCheck.DIAG_ERROR, |
|
247 ste(MAIN_SNIPPET, VALID, VALID, true, null), |
|
248 ste(g, VALID, OVERWRITTEN, false, MAIN_SNIPPET), |
|
249 ste(a, VALID, RECOVERABLE_DEFINED, false, MAIN_SNIPPET)); |
|
250 assertUnresolvedDependencies(a, 0); |
|
251 assertActiveKeys(); |
|
252 } |
|
253 |
|
254 public void testForwardClassToClass1() { |
|
255 Snippet a = classKey(assertEval("class A { B b = new B(); }", added(RECOVERABLE_NOT_DEFINED))); |
|
256 assertDeclareFail("new A().b;", "compiler.err.cant.resolve.location"); |
|
257 |
|
258 Snippet b = classKey(assertEval("class B { public String toString() { return \"B\"; } }", |
|
259 added(VALID), |
|
260 ste(a, RECOVERABLE_NOT_DEFINED, VALID, true, null))); |
|
261 assertEval("new A().b;", "B"); |
|
262 assertEval("interface B { }", |
|
263 DiagCheck.DIAG_OK, |
|
264 DiagCheck.DIAG_ERROR, |
|
265 ste(MAIN_SNIPPET, VALID, VALID, true, null), |
|
266 ste(b, VALID, OVERWRITTEN, false, MAIN_SNIPPET), |
|
267 ste(a, VALID, RECOVERABLE_DEFINED, true, MAIN_SNIPPET)); |
|
268 assertEvalUnresolvedException("new A().b;", "A", 0, 1); |
|
269 assertActiveKeys(); |
|
270 } |
|
271 |
|
272 public void testForwardClassToClass2() { |
|
273 Snippet a = classKey(assertEval("class A extends B { }", added(RECOVERABLE_NOT_DEFINED))); |
|
274 assertDeclareFail("new A();", "compiler.err.cant.resolve.location"); |
|
275 |
|
276 Snippet b = classKey(assertEval("class B { public String toString() { return \"B\"; } }", |
|
277 added(VALID), |
|
278 ste(a, RECOVERABLE_NOT_DEFINED, VALID, true, null))); |
|
279 assertEval("new A();", "B"); |
|
280 assertEval("interface B { }", |
|
281 DiagCheck.DIAG_OK, |
|
282 DiagCheck.DIAG_ERROR, |
|
283 ste(MAIN_SNIPPET, VALID, VALID, true, null), |
|
284 ste(b, VALID, OVERWRITTEN, false, MAIN_SNIPPET), |
|
285 ste(a, VALID, RECOVERABLE_NOT_DEFINED, true, MAIN_SNIPPET)); |
|
286 assertDeclareFail("new A();", "compiler.err.cant.resolve.location"); |
|
287 assertActiveKeys(); |
|
288 } |
|
289 |
|
290 public void testForwardClassToClass3() { |
|
291 Snippet a = classKey(assertEval("interface A extends B { static int f() { return 10; } }", added(RECOVERABLE_NOT_DEFINED))); |
|
292 assertDeclareFail("A.f();", "compiler.err.cant.resolve.location"); |
|
293 |
|
294 Snippet b = classKey(assertEval("interface B { }", |
|
295 added(VALID), |
|
296 ste(a, RECOVERABLE_NOT_DEFINED, VALID, true, null))); |
|
297 assertEval("A.f();", "10"); |
|
298 assertEval("class B { }", |
|
299 DiagCheck.DIAG_OK, |
|
300 DiagCheck.DIAG_ERROR, |
|
301 ste(MAIN_SNIPPET, VALID, VALID, true, null), |
|
302 ste(b, VALID, OVERWRITTEN, false, MAIN_SNIPPET), |
|
303 ste(a, VALID, RECOVERABLE_NOT_DEFINED, true, MAIN_SNIPPET)); |
|
304 assertDeclareFail("A.f();", "compiler.err.cant.resolve.location"); |
|
305 assertActiveKeys(); |
|
306 } |
|
307 |
|
308 public void testForwardVariable() { |
|
309 assertEval("int f() { return x; }", added(RECOVERABLE_DEFINED)); |
|
310 assertEvalUnresolvedException("f();", "f", 1, 0); |
|
311 assertActiveKeys(); |
|
312 } |
|
313 |
|
314 public void testLocalClassInUnresolved() { |
|
315 Snippet f = methodKey(assertEval("void f() { class A {} g(); }", added(RECOVERABLE_DEFINED))); |
|
316 assertEval("void g() {}", |
|
317 added(VALID), |
|
318 ste(f, RECOVERABLE_DEFINED, VALID, false, null)); |
|
319 assertEval("f();", ""); |
|
320 } |
|
321 } |