|
1 /* |
|
2 * Copyright (c) 2015, 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 |
|
26 * @bug 8131025 8141092 8153761 8145263 8131019 8175886 8176184 8176241 8176110 |
|
27 * @summary Test Completion and Documentation |
|
28 * @library /tools/lib |
|
29 * @modules jdk.compiler/com.sun.tools.javac.api |
|
30 * jdk.compiler/com.sun.tools.javac.main |
|
31 * jdk.jdeps/com.sun.tools.javap |
|
32 * jdk.jshell/jdk.jshell:open |
|
33 * @build toolbox.ToolBox toolbox.JarTask toolbox.JavacTask |
|
34 * @build KullaTesting TestingInputStream Compiler |
|
35 * @run testng CompletionSuggestionTest |
|
36 */ |
|
37 |
|
38 import java.io.IOException; |
|
39 import java.lang.reflect.Field; |
|
40 import java.nio.file.Files; |
|
41 import java.nio.file.Path; |
|
42 import java.nio.file.Paths; |
|
43 import java.util.Arrays; |
|
44 import java.util.Collections; |
|
45 import java.util.Set; |
|
46 import java.util.HashSet; |
|
47 import java.util.function.BiFunction; |
|
48 import java.util.function.Function; |
|
49 import java.util.jar.JarEntry; |
|
50 import java.util.jar.JarOutputStream; |
|
51 |
|
52 import jdk.jshell.Snippet; |
|
53 import org.testng.annotations.BeforeMethod; |
|
54 import org.testng.annotations.Test; |
|
55 |
|
56 import static jdk.jshell.Snippet.Status.VALID; |
|
57 import static jdk.jshell.Snippet.Status.OVERWRITTEN; |
|
58 |
|
59 @Test |
|
60 public class CompletionSuggestionTest extends KullaTesting { |
|
61 |
|
62 private final Compiler compiler = new Compiler(); |
|
63 private final Path outDir = Paths.get("completion_suggestion_test"); |
|
64 |
|
65 public void testMemberExpr() { |
|
66 assertEval("class Test { static void test() { } }"); |
|
67 assertCompletion("Test.t|", "test()"); |
|
68 assertEval("Test ccTestInstance = new Test();"); |
|
69 assertCompletion("ccTestInstance.t|", "toString()"); |
|
70 assertCompletion(" ccTe|", "ccTestInstance"); |
|
71 assertCompletion("String value = ccTestInstance.to|", "toString()"); |
|
72 assertCompletion("java.util.Coll|", "Collection", "Collections"); |
|
73 assertCompletion("String.cla|", "class"); |
|
74 assertCompletion("boolean.cla|", "class"); |
|
75 assertCompletion("byte.cla|", "class"); |
|
76 assertCompletion("short.cla|", "class"); |
|
77 assertCompletion("char.cla|", "class"); |
|
78 assertCompletion("int.cla|", "class"); |
|
79 assertCompletion("float.cla|", "class"); |
|
80 assertCompletion("long.cla|", "class"); |
|
81 assertCompletion("double.cla|", "class"); |
|
82 assertCompletion("void.cla|", "class"); |
|
83 assertCompletion("Object[].|", "class"); |
|
84 assertCompletion("int[].|", "class"); |
|
85 assertEval("Object[] ao = null;"); |
|
86 assertCompletion("int i = ao.|", "length"); |
|
87 assertEval("int[] ai = null;"); |
|
88 assertCompletion("int i = ai.|", "length"); |
|
89 assertCompletionIncludesExcludes("\"\".|", |
|
90 new HashSet<>(Collections.emptyList()), |
|
91 new HashSet<>(Arrays.asList("String("))); |
|
92 assertEval("double d = 0;"); |
|
93 assertEval("void m() {}"); |
|
94 assertCompletionIncludesExcludes("d.|", |
|
95 new HashSet<>(Collections.emptyList()), |
|
96 new HashSet<>(Arrays.asList("class"))); |
|
97 assertCompletionIncludesExcludes("m().|", |
|
98 new HashSet<>(Collections.emptyList()), |
|
99 new HashSet<>(Arrays.asList("class"))); |
|
100 assertEval("class C {class D {} static class E {} enum F {} interface H {} void method() {} int number;}"); |
|
101 assertCompletionIncludesExcludes("C.|", |
|
102 new HashSet<>(Arrays.asList("D", "E", "F", "H", "class")), |
|
103 new HashSet<>(Arrays.asList("method()", "number"))); |
|
104 assertCompletionIncludesExcludes("new C().|", |
|
105 new HashSet<>(Arrays.asList("method()", "number")), |
|
106 new HashSet<>(Arrays.asList("D", "E", "F", "H", "class"))); |
|
107 assertCompletionIncludesExcludes("new C() {}.|", |
|
108 new HashSet<>(Arrays.asList("method()", "number")), |
|
109 new HashSet<>(Arrays.asList("D", "E", "F", "H", "class"))); |
|
110 } |
|
111 |
|
112 public void testStartOfExpression() { |
|
113 assertEval("int ccTest = 0;"); |
|
114 assertCompletion("System.err.println(cc|", "ccTest"); |
|
115 assertCompletion("for (int i = cc|", "ccTest"); |
|
116 } |
|
117 |
|
118 public void testParameter() { |
|
119 assertCompletion("class C{void method(int num){num|", "num"); |
|
120 } |
|
121 |
|
122 public void testPrimitive() { |
|
123 Set<String> primitives = new HashSet<>(Arrays.asList("boolean", "char", "byte", "short", "int", "long", "float", "double")); |
|
124 Set<String> onlyVoid = new HashSet<>(Collections.singletonList("void")); |
|
125 Set<String> primitivesOrVoid = new HashSet<>(primitives); |
|
126 primitivesOrVoid.addAll(onlyVoid); |
|
127 |
|
128 assertCompletionIncludesExcludes("|", |
|
129 primitivesOrVoid, |
|
130 new HashSet<>(Collections.emptyList())); |
|
131 assertCompletionIncludesExcludes("int num = |", |
|
132 primitivesOrVoid, |
|
133 new HashSet<>(Collections.emptyList())); |
|
134 assertCompletionIncludesExcludes("num = |", |
|
135 primitivesOrVoid, |
|
136 new HashSet<>(Collections.emptyList())); |
|
137 assertCompletionIncludesExcludes("class C{void m() {|", |
|
138 primitivesOrVoid, |
|
139 new HashSet<>(Collections.emptyList())); |
|
140 assertCompletionIncludesExcludes("void method(|", |
|
141 primitives, |
|
142 onlyVoid); |
|
143 assertCompletionIncludesExcludes("void method(int num, |", |
|
144 primitives, |
|
145 onlyVoid); |
|
146 assertCompletion("new java.util.ArrayList<doub|"); |
|
147 assertCompletion("class A extends doubl|"); |
|
148 assertCompletion("class A implements doubl|"); |
|
149 assertCompletion("interface A extends doubl|"); |
|
150 assertCompletion("enum A implements doubl|"); |
|
151 assertCompletion("class A<T extends doubl|"); |
|
152 } |
|
153 |
|
154 public void testEmpty() { |
|
155 assertCompletionIncludesExcludes("|", |
|
156 new HashSet<>(Arrays.asList("Object", "Void")), |
|
157 new HashSet<>(Arrays.asList("$REPL00DOESNOTMATTER"))); |
|
158 assertCompletionIncludesExcludes("V|", |
|
159 new HashSet<>(Collections.singletonList("Void")), |
|
160 new HashSet<>(Collections.singletonList("Object"))); |
|
161 assertCompletionIncludesExcludes("{ |", |
|
162 new HashSet<>(Arrays.asList("Object", "Void")), |
|
163 new HashSet<>(Arrays.asList("$REPL00DOESNOTMATTER"))); |
|
164 } |
|
165 |
|
166 public void testSmartCompletion() { |
|
167 assertEval("int ccTest1 = 0;"); |
|
168 assertEval("int ccTest2 = 0;"); |
|
169 assertEval("String ccTest3 = null;"); |
|
170 assertEval("void method(int i, String str) { }"); |
|
171 assertEval("void method(String str, int i) { }"); |
|
172 assertEval("java.util.List<String> list = null;"); |
|
173 assertCompletion("int ccTest4 = |", true, "ccTest1", "ccTest2"); |
|
174 assertCompletion("ccTest2 = |", true, "ccTest1", "ccTest2"); |
|
175 assertCompletion("int ccTest4 = ccTe|", "ccTest1", "ccTest2", "ccTest3"); |
|
176 assertCompletion("int ccTest4 = ccTest3.len|", true, "length()"); |
|
177 assertCompletion("method(|", true, "ccTest1", "ccTest2", "ccTest3"); |
|
178 assertCompletion("method(0, |", true, "ccTest3"); |
|
179 assertCompletion("list.add(|", true, "ccTest1", "ccTest2", "ccTest3"); |
|
180 assertCompletion("list.add(0, |", true, "ccTest3"); |
|
181 assertCompletion("new String(|", true, "ccTest3"); |
|
182 assertCompletion("new String(new char[0], |", true, "ccTest1", "ccTest2"); |
|
183 assertCompletionIncludesExcludes("new jav|", new HashSet<>(Arrays.asList("java.", "javax.")), Collections.emptySet()); |
|
184 assertCompletion("Class<String> clazz = String.c|", true, "class"); |
|
185 |
|
186 Snippet klass = classKey(assertEval("class Klass {void method(int n) {} private void method(String str) {}}")); |
|
187 assertCompletion("new Klass().method(|", true, "ccTest1", "ccTest2"); |
|
188 Snippet klass2 = classKey(assertEval("class Klass {static void method(int n) {} void method(String str) {}}", |
|
189 ste(MAIN_SNIPPET, VALID, VALID, true, null), |
|
190 ste(klass, VALID, OVERWRITTEN, false, MAIN_SNIPPET))); |
|
191 assertCompletion("Klass.method(|", true, "ccTest1", "ccTest2"); |
|
192 assertEval("class Klass {Klass(int n) {} private Klass(String str) {}}", |
|
193 ste(MAIN_SNIPPET, VALID, VALID, true, null), |
|
194 ste(klass2, VALID, OVERWRITTEN, false, MAIN_SNIPPET)); |
|
195 assertCompletion("new Klass(|", true, "ccTest1", "ccTest2"); |
|
196 } |
|
197 |
|
198 public void testSmartCompletionInOverriddenMethodInvocation() { |
|
199 assertEval("int ccTest1 = 0;"); |
|
200 assertEval("int ccTest2 = 0;"); |
|
201 assertEval("String ccTest3 = null;"); |
|
202 assertCompletion("\"\".wait(|", true, "ccTest1", "ccTest2"); |
|
203 assertEval("class Base {void method(int n) {}}"); |
|
204 assertEval("class Extend extends Base {}"); |
|
205 assertCompletion("new Extend().method(|", true, "ccTest1", "ccTest2"); |
|
206 } |
|
207 |
|
208 public void testSmartCompletionForBoxedType() { |
|
209 assertEval("int ccTest1 = 0;"); |
|
210 assertEval("Integer ccTest2 = 0;"); |
|
211 assertEval("Object ccTest3 = null;"); |
|
212 assertEval("int method1(int n) {return n;}"); |
|
213 assertEval("Integer method2(Integer n) {return n;}"); |
|
214 assertEval("Object method3(Object o) {return o;}"); |
|
215 assertCompletion("int ccTest4 = |", true, "ccTest1", "ccTest2", "method1(", "method2("); |
|
216 assertCompletion("Integer ccTest4 = |", true, "ccTest1", "ccTest2", "method1(", "method2("); |
|
217 assertCompletion("Object ccTest4 = |", true, "ccTest1", "ccTest2", "ccTest3", "method1(", "method2(", "method3("); |
|
218 assertCompletion("method1(|", true, "ccTest1", "ccTest2", "method1(", "method2("); |
|
219 assertCompletion("method2(|", true, "ccTest1", "ccTest2", "method1(", "method2("); |
|
220 assertCompletion("method3(|", true, "ccTest1", "ccTest2", "ccTest3", "method1(", "method2(", "method3("); |
|
221 } |
|
222 |
|
223 public void testNewClass() { |
|
224 assertCompletion("String str = new Strin|", "String(", "StringBuffer(", "StringBuilder(", "StringIndexOutOfBoundsException("); |
|
225 assertCompletion("String str = new java.lang.Strin|", "String(", "StringBuffer(", "StringBuilder(", "StringIndexOutOfBoundsException("); |
|
226 assertCompletion("String str = new |", true, "String("); |
|
227 assertCompletion("String str = new java.lang.|", true, "String("); |
|
228 assertCompletion("throw new Strin|", true, "StringIndexOutOfBoundsException("); |
|
229 |
|
230 assertEval("class A{class B{} class C {C(int n) {}} static class D {} interface I {}}"); |
|
231 assertEval("A a;"); |
|
232 assertCompletion("new A().new |", "B()", "C("); |
|
233 assertCompletion("a.new |", "B()", "C("); |
|
234 assertCompletion("new A.|", "D()"); |
|
235 |
|
236 assertEval("enum E{; class A {}}"); |
|
237 assertEval("interface I{; class A {}}"); |
|
238 assertCompletion("new E.|", "A()"); |
|
239 assertCompletion("new I.|", "A()"); |
|
240 assertCompletion("new String(I.A|", "A"); |
|
241 } |
|
242 |
|
243 public void testFullyQualified() { |
|
244 assertCompletion("Optional<String> opt = java.u|", "util."); |
|
245 assertCompletionIncludesExcludes("Optional<Strings> opt = java.util.O|", new HashSet<>(Collections.singletonList("Optional")), Collections.emptySet()); |
|
246 |
|
247 assertEval("void method(java.util.Optional<String> opt) {}"); |
|
248 assertCompletion("method(java.u|", "util."); |
|
249 |
|
250 assertCompletion("Object.notElement.|"); |
|
251 assertCompletion("Object o = com.su|", "sun."); |
|
252 |
|
253 Path p1 = outDir.resolve("dir1"); |
|
254 compiler.compile(p1, |
|
255 "package p1.p2;\n" + |
|
256 "public class Test {\n" + |
|
257 "}", |
|
258 "package p1.p3;\n" + |
|
259 "public class Test {\n" + |
|
260 "}"); |
|
261 String jarName = "test.jar"; |
|
262 compiler.jar(p1, jarName, "p1/p2/Test.class", "p1/p3/Test.class"); |
|
263 addToClasspath(compiler.getPath(p1.resolve(jarName))); |
|
264 |
|
265 assertCompletionIncludesExcludes("|", new HashSet<>(Collections.singletonList("p1.")), Collections.emptySet()); |
|
266 assertCompletion("p1.|", "p2.", "p3."); |
|
267 assertCompletion("p1.p2.|", "Test"); |
|
268 assertCompletion("p1.p3.|", "Test"); |
|
269 } |
|
270 |
|
271 public void testCheckAccessibility() { |
|
272 assertCompletion("java.util.regex.Pattern.co|", "compile("); |
|
273 } |
|
274 |
|
275 public void testCompletePackages() { |
|
276 assertCompletion("java.u|", "util."); |
|
277 assertCompletionIncludesExcludes("jav|", new HashSet<>(Arrays.asList("java.", "javax.")), Collections.emptySet()); |
|
278 } |
|
279 |
|
280 public void testImports() { |
|
281 assertCompletion("import java.u|", "util."); |
|
282 assertCompletionIncludesExcludes("import jav|", new HashSet<>(Arrays.asList("java.", "javax.")), Collections.emptySet()); |
|
283 assertCompletion("import static java.u|", "util."); |
|
284 assertCompletionIncludesExcludes("import static jav|", new HashSet<>(Arrays.asList("java.", "javax.")), Collections.emptySet()); |
|
285 assertCompletion("import static java.lang.Boolean.g|", "getBoolean"); |
|
286 assertCompletion("import java.util.*|"); |
|
287 assertCompletionIncludesExcludes("import java.lang.String.|", |
|
288 Collections.emptySet(), |
|
289 new HashSet<>(Arrays.asList("CASE_INSENSITIVE_ORDER", "copyValueOf", "format", "join", "valueOf", "class", "length"))); |
|
290 assertCompletionIncludesExcludes("import static java.lang.String.|", |
|
291 new HashSet<>(Arrays.asList("CASE_INSENSITIVE_ORDER", "copyValueOf", "format", "join", "valueOf")), |
|
292 new HashSet<>(Arrays.asList("class", "length"))); |
|
293 assertCompletionIncludesExcludes("import java.util.Map.|", |
|
294 new HashSet<>(Arrays.asList("Entry")), |
|
295 new HashSet<>(Arrays.asList("class"))); |
|
296 } |
|
297 |
|
298 public void testImportStart() { |
|
299 assertCompletionIncludesExcludes("import c|", Set.of("com."), Set.of()); |
|
300 } |
|
301 |
|
302 public void testBrokenClassFile() throws Exception { |
|
303 Compiler compiler = new Compiler(); |
|
304 Path testOutDir = Paths.get("CompletionTestBrokenClassFile"); |
|
305 String input = "package test.inner; public class Test {}"; |
|
306 compiler.compile(testOutDir, input); |
|
307 addToClasspath(compiler.getPath(testOutDir).resolve("test")); |
|
308 assertCompletion("import inner.|"); |
|
309 } |
|
310 |
|
311 public void testDocumentation() throws Exception { |
|
312 dontReadParameterNamesFromClassFile(); |
|
313 assertSignature("System.getProperty(|", |
|
314 "String System.getProperty(String key)", |
|
315 "String System.getProperty(String key, String def)"); |
|
316 assertEval("char[] chars = null;"); |
|
317 assertSignature("new String(chars, |", |
|
318 "String(char[], int, int)"); |
|
319 assertSignature("String.format(|", |
|
320 "String String.format(String, Object...)", |
|
321 "String String.format(java.util.Locale, String, Object...)"); |
|
322 assertSignature("\"\".getBytes(\"\"|", "void String.getBytes(int, int, byte[], int)", |
|
323 "byte[] String.getBytes(String) throws java.io.UnsupportedEncodingException", |
|
324 "byte[] String.getBytes(java.nio.charset.Charset)"); |
|
325 assertSignature("\"\".getBytes(\"\" |", "void String.getBytes(int, int, byte[], int)", |
|
326 "byte[] String.getBytes(String) throws java.io.UnsupportedEncodingException", |
|
327 "byte[] String.getBytes(java.nio.charset.Charset)"); |
|
328 } |
|
329 |
|
330 public void testMethodsWithNoArguments() throws Exception { |
|
331 dontReadParameterNamesFromClassFile(); |
|
332 assertSignature("System.out.println(|", |
|
333 "void java.io.PrintStream.println()", |
|
334 "void java.io.PrintStream.println(boolean)", |
|
335 "void java.io.PrintStream.println(char)", |
|
336 "void java.io.PrintStream.println(int)", |
|
337 "void java.io.PrintStream.println(long)", |
|
338 "void java.io.PrintStream.println(float)", |
|
339 "void java.io.PrintStream.println(double)", |
|
340 "void java.io.PrintStream.println(char[])", |
|
341 "void java.io.PrintStream.println(String)", |
|
342 "void java.io.PrintStream.println(Object)"); |
|
343 } |
|
344 |
|
345 public void testErroneous() { |
|
346 assertCompletion("Undefined.|"); |
|
347 assertSignature("does.not.exist|"); |
|
348 } |
|
349 |
|
350 public void testClinit() { |
|
351 assertEval("enum E{;}"); |
|
352 assertEval("class C{static{}}"); |
|
353 assertCompletionIncludesExcludes("E.|", Collections.emptySet(), new HashSet<>(Collections.singletonList("<clinit>"))); |
|
354 assertCompletionIncludesExcludes("C.|", Collections.emptySet(), new HashSet<>(Collections.singletonList("<clinit>"))); |
|
355 } |
|
356 |
|
357 public void testMethodHeaderContext() { |
|
358 assertCompletion("private void f(Runn|", "Runnable"); |
|
359 assertCompletion("void f(Runn|", "Runnable"); |
|
360 assertCompletion("void f(Object o1, Runn|", "Runnable"); |
|
361 assertCompletion("void f(Object o1) throws Num|", true, "NumberFormatException"); |
|
362 assertCompletion("void f(Object o1) throws java.lang.Num|", true, "NumberFormatException"); |
|
363 assertEval("class HogeHoge {static class HogeHogeException extends Exception {}}"); |
|
364 assertCompletion("void f(Object o1) throws Hoge|", "HogeHoge"); |
|
365 assertCompletion("void f(Object o1) throws HogeHoge.|", true, "HogeHogeException"); |
|
366 } |
|
367 |
|
368 public void testTypeVariables() { |
|
369 assertCompletion("class A<TYPE> { public void test() { TY|", "TYPE"); |
|
370 assertCompletion("class A<TYPE> { public static void test() { TY|"); |
|
371 assertCompletion("class A<TYPE> { public <TYPE> void test() { TY|", "TYPE"); |
|
372 assertCompletion("class A<TYPE> { public static <TYPE> void test() { TY|", "TYPE"); |
|
373 } |
|
374 |
|
375 public void testGeneric() { |
|
376 assertEval("import java.util.concurrent.*;"); |
|
377 assertCompletion("java.util.List<Integ|", "Integer"); |
|
378 assertCompletion("class A<TYPE extends Call|", "Callable"); |
|
379 assertCompletion("class A<TYPE extends Callable<TY|", "TYPE"); |
|
380 assertCompletion("<TYPE> void f(TY|", "TYPE"); |
|
381 assertCompletion("class A<TYPE extends Callable<? sup|", "super"); |
|
382 assertCompletion("class A<TYPE extends Callable<? super TY|", "TYPE"); |
|
383 } |
|
384 |
|
385 public void testFields() { |
|
386 assertEval("interface Interface { int field = 0; }"); |
|
387 Snippet clazz = classKey(assertEval("class Clazz {" + |
|
388 "static int staticField = 0;" + |
|
389 "int field = 0;" + |
|
390 " }")); |
|
391 assertCompletion("Interface.fiel|", "field"); |
|
392 assertCompletion("Clazz.staticFiel|", "staticField"); |
|
393 assertCompletion("new Interface() {}.fiel|"); |
|
394 assertCompletion("new Clazz().staticFiel|"); |
|
395 assertCompletion("new Clazz().fiel|", "field"); |
|
396 assertCompletion("new Clazz() {}.fiel|", "field"); |
|
397 assertEval("class Clazz implements Interface {}", |
|
398 ste(MAIN_SNIPPET, VALID, VALID, true, null), |
|
399 ste(clazz, VALID, OVERWRITTEN, false, MAIN_SNIPPET)); |
|
400 assertCompletion("Clazz.fiel|", "field"); |
|
401 assertCompletion("new Clazz().fiel|"); |
|
402 assertCompletion("new Clazz() {}.fiel|"); |
|
403 } |
|
404 |
|
405 public void testMethods() { |
|
406 assertEval("interface Interface {" + |
|
407 "default int defaultMethod() { return 0; }" + |
|
408 "static int staticMethod() { return 0; }" + |
|
409 "}"); |
|
410 Snippet clazz = classKey(assertEval("class Clazz {" + |
|
411 "static int staticMethod() { return 0; }" + |
|
412 "int method() { return 0; }" + |
|
413 "}")); |
|
414 assertCompletion("Interface.staticMeth|", "staticMethod()"); |
|
415 assertCompletion("Clazz.staticMeth|", "staticMethod()"); |
|
416 assertCompletion("new Interface() {}.defaultMe||", "defaultMethod()"); |
|
417 assertCompletion("new Clazz().staticMeth|"); |
|
418 assertCompletion("new Clazz().meth|", "method()"); |
|
419 assertEval("class Clazz implements Interface {}", |
|
420 ste(MAIN_SNIPPET, VALID, VALID, true, null), |
|
421 ste(clazz, VALID, OVERWRITTEN, false, MAIN_SNIPPET)); |
|
422 assertCompletion("Clazz.staticMeth|"); |
|
423 assertCompletion("new Clazz() {}.defaultM|", "defaultMethod()"); |
|
424 } |
|
425 |
|
426 @Test |
|
427 public void testUncompletedDeclaration() { |
|
428 assertCompletion("class Clazz { Claz|", "Clazz"); |
|
429 assertCompletion("class Clazz { class A extends Claz|", "Clazz"); |
|
430 assertCompletion("class Clazz { Clazz clazz; Object o = claz|", "clazz"); |
|
431 assertCompletion("class Clazz { static Clazz clazz; Object o = claz|", "clazz"); |
|
432 assertCompletion("class Clazz { Clazz clazz; static Object o = claz|", true); |
|
433 assertCompletion("class Clazz { void method(Claz|", "Clazz"); |
|
434 assertCompletion("class A { int method() { return 0; } int a = meth|", "method()"); |
|
435 assertCompletion("class A { int field = 0; int method() { return fiel|", "field"); |
|
436 assertCompletion("class A { static int method() { return 0; } int a = meth|", "method()"); |
|
437 assertCompletion("class A { static int field = 0; int method() { return fiel|", "field"); |
|
438 assertCompletion("class A { int method() { return 0; } static int a = meth|", true); |
|
439 assertCompletion("class A { int field = 0; static int method() { return fiel|", true); |
|
440 } |
|
441 |
|
442 @Test |
|
443 public void testClassDeclaration() { |
|
444 assertEval("void ClazzM() {}"); |
|
445 assertEval("void InterfaceM() {}"); |
|
446 assertEval("interface Interface {}"); |
|
447 assertCompletion("interface A extends Interf|", "Interface"); |
|
448 assertCompletion("class A implements Interf|", "Interface"); |
|
449 assertEval("class Clazz {}"); |
|
450 assertCompletion("class A extends Claz|", "Clazz"); |
|
451 assertCompletion("class A extends Clazz implements Interf|", "Interface"); |
|
452 assertEval("interface Interface1 {}"); |
|
453 assertCompletion("class A extends Clazz implements Interface, Interf|", "Interface", "Interface1"); |
|
454 assertCompletion("interface A implements Claz|"); |
|
455 assertCompletion("interface A implements Inter|"); |
|
456 assertCompletion("class A implements Claz|", true); |
|
457 assertCompletion("class A extends Clazz implements Interface, Interf|", true, "Interface1"); |
|
458 assertCompletion("class A extends Clazz implements Interface, Interf|", true, "Interface1"); |
|
459 assertEval("class InterfaceClazz {}"); |
|
460 assertCompletion("class A <T extends Claz|", "Clazz"); |
|
461 assertCompletion("class A <T extends Interf|", "Interface", "Interface1", "InterfaceClazz"); |
|
462 assertCompletion("class A <T extends Interface & Interf|", "Interface", "Interface1", "InterfaceClazz"); |
|
463 assertCompletion("class A <T extends Clazz & Interf|", "Interface", "Interface1", "InterfaceClazz"); |
|
464 assertCompletion("class A <T extends Claz|", true, "Clazz"); |
|
465 assertCompletion("class A <T extends Interf|", true, "Interface", "Interface1", "InterfaceClazz"); |
|
466 assertCompletion("class A <T extends Interface & Interf|", true, "Interface1"); |
|
467 assertCompletion("class A <T extends Clazz & Interf|", true, "Interface", "Interface1"); |
|
468 } |
|
469 |
|
470 public void testMethodDeclaration() { |
|
471 assertEval("void ClazzM() {}"); |
|
472 assertEval("void InterfaceM() {}"); |
|
473 assertEval("interface Interface {}"); |
|
474 assertCompletion("void m(Interf|", "Interface"); |
|
475 assertCompletion("void m(Interface i1, Interf|", "Interface"); |
|
476 assertEval("class InterfaceException extends Exception {}"); |
|
477 assertCompletion("void m(Interface i1) throws Interf|", "Interface", "InterfaceException"); |
|
478 assertCompletion("void m(Interface i1) throws Interf|", true, "InterfaceException"); |
|
479 } |
|
480 |
|
481 public void testDocumentationOfUserDefinedMethods() { |
|
482 assertEval("void f() {}"); |
|
483 assertSignature("f(|", "void f()"); |
|
484 assertEval("void f(int i) {}"); |
|
485 assertSignature("f(|", "void f()", "void f(int i)"); |
|
486 assertEval("<T> void f(T... ts) {}", DiagCheck.DIAG_WARNING, DiagCheck.DIAG_OK); |
|
487 assertSignature("f(|", "void f()", "void f(int i)", "void <T>f(T... ts)"); |
|
488 assertEval("class A {}"); |
|
489 assertEval("void f(A a) {}"); |
|
490 assertSignature("f(|", "void f()", "void f(int i)", "void <T>f(T... ts)", "void f(A a)"); |
|
491 } |
|
492 |
|
493 public void testClass() { |
|
494 assertSignature("String|", "java.lang.String"); |
|
495 } |
|
496 |
|
497 public void testDocumentationOfUserDefinedConstructors() { |
|
498 Snippet a = classKey(assertEval("class A {}")); |
|
499 assertSignature("new A(|", "A()"); |
|
500 Snippet a2 = classKey(assertEval("class A { A() {} A(int i) {}}", |
|
501 ste(MAIN_SNIPPET, VALID, VALID, true, null), |
|
502 ste(a, VALID, OVERWRITTEN, false, MAIN_SNIPPET))); |
|
503 assertSignature("new A(|", "A()", "A(int i)"); |
|
504 assertEval("class A<T> { A(T a) {} A(int i) {} <U> A(T t, U u) {}}", |
|
505 ste(MAIN_SNIPPET, VALID, VALID, true, null), |
|
506 ste(a2, VALID, OVERWRITTEN, false, MAIN_SNIPPET)); |
|
507 assertSignature("new A(|", "A<T>(T a)", "A<T>(int i)", "<U> A<T>(T t, U u)"); |
|
508 } |
|
509 |
|
510 public void testDocumentationOfOverriddenMethods() throws Exception { |
|
511 dontReadParameterNamesFromClassFile(); |
|
512 assertSignature("\"\".wait(|", |
|
513 "void Object.wait(long) throws InterruptedException", |
|
514 "void Object.wait(long, int) throws InterruptedException", |
|
515 "void Object.wait() throws InterruptedException"); |
|
516 assertEval("class Base {void method() {}}"); |
|
517 Snippet e = classKey(assertEval("class Extend extends Base {}")); |
|
518 assertSignature("new Extend().method(|", "void Base.method()"); |
|
519 assertEval("class Extend extends Base {void method() {}}", |
|
520 ste(MAIN_SNIPPET, VALID, VALID, true, null), |
|
521 ste(e, VALID, OVERWRITTEN, false, MAIN_SNIPPET)); |
|
522 assertSignature("new Extend().method(|", "void Extend.method()"); |
|
523 } |
|
524 |
|
525 public void testDocumentationOfInvisibleMethods() { |
|
526 assertSignature("Object.wait(|"); |
|
527 assertSignature("\"\".indexOfSupplementary(|"); |
|
528 Snippet a = classKey(assertEval("class A {void method() {}}")); |
|
529 assertSignature("A.method(|"); |
|
530 assertEval("class A {private void method() {}}", |
|
531 ste(MAIN_SNIPPET, VALID, VALID, true, null), |
|
532 ste(a, VALID, OVERWRITTEN, false, MAIN_SNIPPET)); |
|
533 assertSignature("new A().method(|"); |
|
534 } |
|
535 |
|
536 public void testDocumentationOfInvisibleConstructors() { |
|
537 assertSignature("new Compiler(|"); |
|
538 assertEval("class A { private A() {} }"); |
|
539 assertSignature("new A(|"); |
|
540 } |
|
541 |
|
542 public void testDocumentationWithBoxing() { |
|
543 assertEval("int primitive = 0;"); |
|
544 assertEval("Integer boxed = 0;"); |
|
545 assertEval("Object object = null;"); |
|
546 assertEval("void method(int n, Object o) { }"); |
|
547 assertEval("void method(Object n, int o) { }"); |
|
548 assertSignature("method(primitive,|", |
|
549 "void method(int n, Object o)", |
|
550 "void method(Object n, int o)"); |
|
551 assertSignature("method(boxed,|", |
|
552 "void method(int n, Object o)", |
|
553 "void method(Object n, int o)"); |
|
554 assertSignature("method(object,|", |
|
555 "void method(Object n, int o)"); |
|
556 } |
|
557 |
|
558 public void testDocumentationWithGenerics() { |
|
559 class TestDocumentationWithGenerics { |
|
560 private final Function<Integer, String> codeFacotry; |
|
561 private final BiFunction<String, Integer, String> evalFormatter; |
|
562 private final BiFunction<String, Integer, String> docFormatter; |
|
563 int count; |
|
564 |
|
565 TestDocumentationWithGenerics( |
|
566 Function<Integer, String> codeFactory, |
|
567 BiFunction<String, Integer, String> evalFormatter, |
|
568 BiFunction<String, Integer, String> documentationFormatter) { |
|
569 this.codeFacotry = codeFactory; |
|
570 this.evalFormatter = evalFormatter; |
|
571 this.docFormatter = documentationFormatter; |
|
572 } |
|
573 |
|
574 void assertDoc(String generics) { |
|
575 assertDoc(generics, generics); |
|
576 } |
|
577 |
|
578 void assertDoc(String generics, String expectedGenerics) { |
|
579 assertEval(evalFormatter.apply(generics, count)); |
|
580 assertSignature(codeFacotry.apply(count), docFormatter.apply(expectedGenerics, count)); |
|
581 count++; |
|
582 } |
|
583 } |
|
584 |
|
585 TestDocumentationWithGenerics[] tests = { |
|
586 new TestDocumentationWithGenerics( |
|
587 i -> "f" + i + "(|", |
|
588 (g, i) -> "<" + g + "> void f" + i + "() {}", |
|
589 (g, i) -> "void <" + g + ">f" + i + "()" |
|
590 ), |
|
591 new TestDocumentationWithGenerics( |
|
592 i -> "new C" + i + "().f(|", |
|
593 (g, i) -> "class C" + i + "<" + g + "> { void f() {} }", |
|
594 (g, i) -> "void C" + i + "<" + g + ">.f()" |
|
595 ) |
|
596 }; |
|
597 |
|
598 Arrays.stream(tests).forEach(t -> { |
|
599 t.assertDoc("T"); |
|
600 t.assertDoc("T extends Object", |
|
601 "T"); |
|
602 t.assertDoc("T extends String"); |
|
603 t.assertDoc("T extends java.lang.String", |
|
604 "T extends String"); |
|
605 t.assertDoc("T extends Number & Comparable<T>"); |
|
606 t.assertDoc("T extends java.io.Serializable & CharSequence"); |
|
607 t.assertDoc("K, D, M extends java.util.Map<K, D>", |
|
608 "K, D, M extends java.util.Map<K,D>"); |
|
609 }); |
|
610 } |
|
611 |
|
612 public void testVarArgs() { |
|
613 assertEval("int i = 0;"); |
|
614 assertEval("class Foo1 { static void m(int... i) { } } "); |
|
615 assertCompletion("Foo1.m(|", true, "i"); |
|
616 assertCompletion("Foo1.m(i, |", true, "i"); |
|
617 assertCompletion("Foo1.m(i, i, |", true, "i"); |
|
618 assertEval("class Foo2 { static void m(String s, int... i) { } } "); |
|
619 assertCompletion("Foo2.m(|", true); |
|
620 assertCompletion("Foo2.m(i, |", true); |
|
621 assertCompletion("Foo2.m(\"\", |", true, "i"); |
|
622 assertCompletion("Foo2.m(\"\", i, |", true, "i"); |
|
623 assertCompletion("Foo2.m(\"\", i, i, |", true, "i"); |
|
624 assertEval("class Foo3 { Foo3(String s, int... i) { } } "); |
|
625 assertCompletion("new Foo3(|", true); |
|
626 assertCompletion("new Foo3(i, |", true); |
|
627 assertCompletion("new Foo3(\"\", |", true, "i"); |
|
628 assertCompletion("new Foo3(\"\", i, |", true, "i"); |
|
629 assertCompletion("new Foo3(\"\", i, i, |", true, "i"); |
|
630 assertEval("int[] ia = null;"); |
|
631 assertCompletion("Foo1.m(ia, |", true); |
|
632 assertEval("class Foo4 { static void m(int... i) { } static void m(int[] ia, String str) { } } "); |
|
633 assertEval("String str = null;"); |
|
634 assertCompletion("Foo4.m(ia, |", true, "str"); |
|
635 } |
|
636 |
|
637 public void testConstructorAsMemberOf() { |
|
638 assertEval("class Baz<X> { Baz(X x) { } } "); |
|
639 assertEval("String str = null;"); |
|
640 assertEval("Integer i = null;"); |
|
641 assertCompletion("new Baz(|", true, "i", "str"); |
|
642 assertCompletion("new Baz<String>(|", true, "str"); |
|
643 assertCompletion("Baz<String> bz = new Baz<>(|", true, "str"); |
|
644 assertEval("class Foo { static void m(String str) {} static void m(Baz<String> baz) {} }"); |
|
645 assertCompletion("Foo.m(new Baz<>(|", true, "str"); |
|
646 } |
|
647 |
|
648 @BeforeMethod |
|
649 public void setUp() { |
|
650 super.setUp(); |
|
651 |
|
652 Path srcZip = Paths.get("src.zip"); |
|
653 |
|
654 try (JarOutputStream out = new JarOutputStream(Files.newOutputStream(srcZip))) { |
|
655 out.putNextEntry(new JarEntry("java/lang/System.java")); |
|
656 out.write(("package java.lang;\n" + |
|
657 "public class System {\n" + |
|
658 " public String getProperty(String key) { return null; }\n" + |
|
659 " public String getProperty(String key, String def) { return def; }\n" + |
|
660 "}\n").getBytes()); |
|
661 } catch (IOException ex) { |
|
662 throw new IllegalStateException(ex); |
|
663 } |
|
664 |
|
665 try { |
|
666 Field availableSources = getAnalysis().getClass().getDeclaredField("availableSources"); |
|
667 availableSources.setAccessible(true); |
|
668 availableSources.set(getAnalysis(), Arrays.asList(srcZip)); |
|
669 } catch (NoSuchFieldException | IllegalArgumentException | IllegalAccessException ex) { |
|
670 throw new IllegalStateException(ex); |
|
671 } |
|
672 } |
|
673 |
|
674 private void dontReadParameterNamesFromClassFile() throws Exception { |
|
675 Field keepParameterNames = getAnalysis().getClass().getDeclaredField("keepParameterNames"); |
|
676 keepParameterNames.setAccessible(true); |
|
677 keepParameterNames.set(getAnalysis(), new String[0]); |
|
678 } |
|
679 |
|
680 @Test(enabled = false) //TODO 8171829 |
|
681 public void testBrokenClassFile2() throws IOException { |
|
682 Path broken = outDir.resolve("broken"); |
|
683 compiler.compile(broken, |
|
684 "package p;\n" + |
|
685 "public class BrokenA {\n" + |
|
686 "}", |
|
687 "package p.q;\n" + |
|
688 "public class BrokenB {\n" + |
|
689 "}", |
|
690 "package p;\n" + |
|
691 "public class BrokenC {\n" + |
|
692 "}"); |
|
693 Path cp = compiler.getPath(broken); |
|
694 Path target = cp.resolve("p").resolve("BrokenB.class"); |
|
695 Files.deleteIfExists(target); |
|
696 Files.move(cp.resolve("p").resolve("q").resolve("BrokenB.class"), target); |
|
697 addToClasspath(cp); |
|
698 |
|
699 assertEval("import p.*;"); |
|
700 assertCompletion("Broke|", "BrokenA", "BrokenC"); |
|
701 } |
|
702 } |