Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/langtools/jdk/jshell/CompletionSuggestionTest.java
40930 views
1
/*
2
* Copyright (c) 2015, 2020, 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 8177466 8197439 8221759 8234896 8240658
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
assertCompletion("\"\".leng|", "length()");
111
assertCompletion("\"\"\"\n\"\"\".leng|", "length()");
112
}
113
114
public void testStartOfExpression() {
115
assertEval("int ccTest = 0;");
116
assertCompletion("System.err.println(cc|", "ccTest");
117
assertCompletion("for (int i = cc|", "ccTest");
118
}
119
120
public void testParameter() {
121
assertCompletion("class C{void method(int num){num|", "num");
122
}
123
124
public void testPrimitive() {
125
Set<String> primitives = new HashSet<>(Arrays.asList("boolean", "char", "byte", "short", "int", "long", "float", "double"));
126
Set<String> onlyVoid = new HashSet<>(Collections.singletonList("void"));
127
Set<String> primitivesOrVoid = new HashSet<>(primitives);
128
primitivesOrVoid.addAll(onlyVoid);
129
130
assertCompletionIncludesExcludes("|",
131
primitivesOrVoid,
132
new HashSet<>(Collections.emptyList()));
133
assertCompletionIncludesExcludes("int num = |",
134
primitivesOrVoid,
135
new HashSet<>(Collections.emptyList()));
136
assertCompletionIncludesExcludes("num = |",
137
primitivesOrVoid,
138
new HashSet<>(Collections.emptyList()));
139
assertCompletionIncludesExcludes("class C{void m() {|",
140
primitivesOrVoid,
141
new HashSet<>(Collections.emptyList()));
142
assertCompletionIncludesExcludes("void method(|",
143
primitives,
144
onlyVoid);
145
assertCompletionIncludesExcludes("void method(int num, |",
146
primitives,
147
onlyVoid);
148
assertCompletion("new java.util.ArrayList<doub|");
149
assertCompletion("class A extends doubl|");
150
assertCompletion("class A implements doubl|");
151
assertCompletion("interface A extends doubl|");
152
assertCompletion("enum A implements doubl|");
153
assertCompletion("class A<T extends doubl|");
154
}
155
156
public void testEmpty() {
157
assertCompletionIncludesExcludes("|",
158
new HashSet<>(Arrays.asList("Object", "Void")),
159
new HashSet<>(Arrays.asList("$REPL00DOESNOTMATTER")));
160
assertCompletionIncludesExcludes("V|",
161
new HashSet<>(Collections.singletonList("Void")),
162
new HashSet<>(Collections.singletonList("Object")));
163
assertCompletionIncludesExcludes("{ |",
164
new HashSet<>(Arrays.asList("Object", "Void")),
165
new HashSet<>(Arrays.asList("$REPL00DOESNOTMATTER")));
166
}
167
168
public void testSmartCompletion() {
169
assertEval("int ccTest1 = 0;");
170
assertEval("int ccTest2 = 0;");
171
assertEval("String ccTest3 = null;");
172
assertEval("void method(int i, String str) { }");
173
assertEval("void method(String str, int i) { }");
174
assertEval("java.util.List<String> list = null;");
175
assertCompletion("int ccTest4 = |", true, "ccTest1", "ccTest2");
176
assertCompletion("ccTest2 = |", true, "ccTest1", "ccTest2");
177
assertCompletion("int ccTest4 = ccTe|", "ccTest1", "ccTest2", "ccTest3");
178
assertCompletion("int ccTest4 = ccTest3.len|", true, "length()");
179
assertCompletion("method(|", true, "ccTest1", "ccTest2", "ccTest3");
180
assertCompletion("method(0, |", true, "ccTest3");
181
assertCompletion("list.add(|", true, "ccTest1", "ccTest2", "ccTest3");
182
assertCompletion("list.add(0, |", true, "ccTest3");
183
assertCompletion("new String(|", true, "ccTest3");
184
assertCompletion("new String(new char[0], |", true, "ccTest1", "ccTest2");
185
assertCompletionIncludesExcludes("new jav|", new HashSet<>(Arrays.asList("java.", "javax.")), Collections.emptySet());
186
assertCompletion("Class<String> clazz = String.c|", true, "class");
187
188
Snippet klass = classKey(assertEval("class Klass {void method(int n) {} private void method(String str) {}}"));
189
assertCompletion("new Klass().method(|", true, "ccTest1", "ccTest2");
190
Snippet klass2 = classKey(assertEval("class Klass {static void method(int n) {} void method(String str) {}}",
191
ste(MAIN_SNIPPET, VALID, VALID, true, null),
192
ste(klass, VALID, OVERWRITTEN, false, MAIN_SNIPPET)));
193
assertCompletion("Klass.method(|", true, "ccTest1", "ccTest2");
194
assertEval("class Klass {Klass(int n) {} private Klass(String str) {}}",
195
ste(MAIN_SNIPPET, VALID, VALID, true, null),
196
ste(klass2, VALID, OVERWRITTEN, false, MAIN_SNIPPET));
197
assertCompletion("new Klass(|", true, "ccTest1", "ccTest2");
198
}
199
200
public void testSmartCompletionInOverriddenMethodInvocation() {
201
assertEval("int ccTest1 = 0;");
202
assertEval("int ccTest2 = 0;");
203
assertEval("String ccTest3 = null;");
204
assertCompletion("\"\".wait(|", true, "ccTest1", "ccTest2");
205
assertEval("class Base {void method(int n) {}}");
206
assertEval("class Extend extends Base {}");
207
assertCompletion("new Extend().method(|", true, "ccTest1", "ccTest2");
208
}
209
210
public void testSmartCompletionForBoxedType() {
211
assertEval("int ccTest1 = 0;");
212
assertEval("Integer ccTest2 = 0;");
213
assertEval("Object ccTest3 = null;");
214
assertEval("int method1(int n) {return n;}");
215
assertEval("Integer method2(Integer n) {return n;}");
216
assertEval("Object method3(Object o) {return o;}");
217
assertCompletion("int ccTest4 = |", true, "ccTest1", "ccTest2", "method1(", "method2(");
218
assertCompletion("Integer ccTest4 = |", true, "ccTest1", "ccTest2", "method1(", "method2(");
219
assertCompletion("Object ccTest4 = |", true, "ccTest1", "ccTest2", "ccTest3", "method1(", "method2(", "method3(");
220
assertCompletion("method1(|", true, "ccTest1", "ccTest2", "method1(", "method2(");
221
assertCompletion("method2(|", true, "ccTest1", "ccTest2", "method1(", "method2(");
222
assertCompletion("method3(|", true, "ccTest1", "ccTest2", "ccTest3", "method1(", "method2(", "method3(");
223
}
224
225
public void testNewClass() {
226
assertCompletion("String str = new Strin|", "String(", "StringBuffer(", "StringBuilder(", "StringIndexOutOfBoundsException(");
227
assertCompletion("String str = new java.lang.Strin|", "String(", "StringBuffer(", "StringBuilder(", "StringIndexOutOfBoundsException(");
228
assertCompletion("String str = new |", true, "String(");
229
assertCompletion("String str = new java.lang.|", true, "String(");
230
assertCompletion("throw new Strin|", true, "StringIndexOutOfBoundsException(");
231
232
assertEval("class A{class B{} class C {C(int n) {}} static class D {} interface I {}}");
233
assertEval("A a;");
234
assertCompletion("new A().new |", "B()", "C(");
235
assertCompletion("a.new |", "B()", "C(");
236
assertCompletion("new A.|", "D()");
237
238
assertEval("enum E{; class A {}}");
239
assertEval("interface I{; class A {}}");
240
assertCompletion("new E.|", "A()");
241
assertCompletion("new I.|", "A()");
242
assertCompletion("new String(I.A|", "A");
243
}
244
245
public void testFullyQualified() {
246
assertCompletion("Optional<String> opt = java.u|", "util.");
247
assertCompletionIncludesExcludes("Optional<Strings> opt = java.util.O|", new HashSet<>(Collections.singletonList("Optional")), Collections.emptySet());
248
249
assertEval("void method(java.util.Optional<String> opt) {}");
250
assertCompletion("method(java.u|", "util.");
251
252
assertCompletion("Object.notElement.|");
253
assertCompletion("Object o = com.su|", "sun.");
254
255
Path p1 = outDir.resolve("dir1");
256
compiler.compile(p1,
257
"package p1.p2;\n" +
258
"public class Test {\n" +
259
"}",
260
"package p1.p3;\n" +
261
"public class Test {\n" +
262
"}");
263
String jarName = "test.jar";
264
compiler.jar(p1, jarName, "p1/p2/Test.class", "p1/p3/Test.class");
265
addToClasspath(compiler.getPath(p1.resolve(jarName)));
266
267
assertCompletionIncludesExcludes("|", new HashSet<>(Collections.singletonList("p1.")), Collections.emptySet());
268
assertCompletion("p1.|", "p2.", "p3.");
269
assertCompletion("p1.p2.|", "Test");
270
assertCompletion("p1.p3.|", "Test");
271
}
272
273
public void testCheckAccessibility() {
274
assertCompletion("java.util.regex.Pattern.co|", "compile(");
275
}
276
277
public void testCompletePackages() {
278
assertCompletion("java.u|", "util.");
279
assertCompletionIncludesExcludes("jav|", new HashSet<>(Arrays.asList("java.", "javax.")), Collections.emptySet());
280
}
281
282
public void testImports() {
283
assertCompletion("import java.u|", "util.");
284
assertCompletionIncludesExcludes("import jav|", new HashSet<>(Arrays.asList("java.", "javax.")), Collections.emptySet());
285
assertCompletion("import static java.u|", "util.");
286
assertCompletionIncludesExcludes("import static jav|", new HashSet<>(Arrays.asList("java.", "javax.")), Collections.emptySet());
287
assertCompletion("import static java.lang.Boolean.g|", "getBoolean");
288
assertCompletion("import java.util.*|");
289
assertCompletionIncludesExcludes("import java.lang.String.|",
290
Collections.emptySet(),
291
new HashSet<>(Arrays.asList("CASE_INSENSITIVE_ORDER", "copyValueOf", "format", "join", "valueOf", "class", "length")));
292
assertCompletionIncludesExcludes("import static java.lang.String.|",
293
new HashSet<>(Arrays.asList("CASE_INSENSITIVE_ORDER", "copyValueOf", "format", "join", "valueOf")),
294
new HashSet<>(Arrays.asList("class", "length")));
295
assertCompletionIncludesExcludes("import java.util.Map.|",
296
new HashSet<>(Arrays.asList("Entry")),
297
new HashSet<>(Arrays.asList("class")));
298
}
299
300
public void testImportStart() {
301
assertCompletionIncludesExcludes("import c|", Set.of("com."), Set.of());
302
}
303
304
public void testBrokenClassFile() throws Exception {
305
Compiler compiler = new Compiler();
306
Path testOutDir = Paths.get("CompletionTestBrokenClassFile");
307
String input = "package test.inner; public class Test {}";
308
compiler.compile(testOutDir, input);
309
addToClasspath(compiler.getPath(testOutDir).resolve("test"));
310
assertCompletion("import inner.|");
311
}
312
313
public void testDocumentation() throws Exception {
314
dontReadParameterNamesFromClassFile();
315
assertSignature("System.getProperty(|",
316
"String System.getProperty(String key)",
317
"String System.getProperty(String key, String def)");
318
assertEval("char[] chars = null;");
319
assertSignature("new String(chars, |",
320
"String(char[], int, int)");
321
assertSignature("String.format(|",
322
"String String.format(String, Object...)",
323
"String String.format(java.util.Locale, String, Object...)");
324
assertSignature("\"\".getBytes(\"\"|", "void String.getBytes(int, int, byte[], int)",
325
"byte[] String.getBytes(String) throws java.io.UnsupportedEncodingException",
326
"byte[] String.getBytes(java.nio.charset.Charset)");
327
assertSignature("\"\".getBytes(\"\" |", "void String.getBytes(int, int, byte[], int)",
328
"byte[] String.getBytes(String) throws java.io.UnsupportedEncodingException",
329
"byte[] String.getBytes(java.nio.charset.Charset)");
330
//JDK-8221759:
331
Compiler compiler = new Compiler();
332
Path testOutDir = Paths.get("WithPrivateField");
333
String input = "package field; public class FieldTest { private static String field; private static String field2; }";
334
compiler.compile(testOutDir, input);
335
addToClasspath(compiler.getPath(testOutDir));
336
assertSignature("field.FieldTest.field|");
337
assertSignature("field.FieldTest.field2|");
338
}
339
340
public void testMethodsWithNoArguments() throws Exception {
341
dontReadParameterNamesFromClassFile();
342
assertSignature("System.out.println(|",
343
"void java.io.PrintStream.println()",
344
"void java.io.PrintStream.println(boolean)",
345
"void java.io.PrintStream.println(char)",
346
"void java.io.PrintStream.println(int)",
347
"void java.io.PrintStream.println(long)",
348
"void java.io.PrintStream.println(float)",
349
"void java.io.PrintStream.println(double)",
350
"void java.io.PrintStream.println(char[])",
351
"void java.io.PrintStream.println(String)",
352
"void java.io.PrintStream.println(Object)");
353
}
354
355
public void testErroneous() {
356
assertCompletion("Undefined.|");
357
assertSignature("does.not.exist|");
358
}
359
360
public void testClinit() {
361
assertEval("enum E{;}");
362
assertEval("class C{static{}}");
363
assertCompletionIncludesExcludes("E.|", Collections.emptySet(), new HashSet<>(Collections.singletonList("<clinit>")));
364
assertCompletionIncludesExcludes("C.|", Collections.emptySet(), new HashSet<>(Collections.singletonList("<clinit>")));
365
}
366
367
public void testMethodHeaderContext() {
368
assertCompletion("private void f(Runn|", "Runnable");
369
assertCompletion("void f(Runn|", "Runnable");
370
assertCompletion("void f(Object o1, Runn|", "Runnable");
371
assertCompletion("void f(Object o1) throws Num|", true, "NumberFormatException");
372
assertCompletion("void f(Object o1) throws java.lang.Num|", true, "NumberFormatException");
373
assertEval("class HogeHoge {static class HogeHogeException extends Exception {}}");
374
assertCompletion("void f(Object o1) throws Hoge|", "HogeHoge");
375
assertCompletion("void f(Object o1) throws HogeHoge.|", true, "HogeHogeException");
376
}
377
378
public void testTypeVariables() {
379
assertCompletion("class A<TYPE> { public void test() { TY|", "TYPE");
380
assertCompletion("class A<TYPE> { public static void test() { TY|");
381
assertCompletion("class A<TYPE> { public <TYPE> void test() { TY|", "TYPE");
382
assertCompletion("class A<TYPE> { public static <TYPE> void test() { TY|", "TYPE");
383
}
384
385
public void testGeneric() {
386
assertEval("import java.util.concurrent.*;");
387
assertCompletion("java.util.List<Integ|", "Integer");
388
assertCompletion("class A<TYPE extends Call|", "Callable");
389
assertCompletion("class A<TYPE extends Callable<TY|", "TYPE");
390
assertCompletion("<TYPE> void f(TY|", "TYPE");
391
assertCompletion("class A<TYPE extends Callable<? sup|", "super");
392
assertCompletion("class A<TYPE extends Callable<? super TY|", "TYPE");
393
}
394
395
public void testFields() {
396
assertEval("interface Interface { int field = 0; }");
397
Snippet clazz = classKey(assertEval("class Clazz {" +
398
"static int staticField = 0;" +
399
"int field = 0;" +
400
" }"));
401
assertCompletion("Interface.fiel|", "field");
402
assertCompletion("Clazz.staticFiel|", "staticField");
403
assertCompletion("new Interface() {}.fiel|");
404
assertCompletion("new Clazz().staticFiel|");
405
assertCompletion("new Clazz().fiel|", "field");
406
assertCompletion("new Clazz() {}.fiel|", "field");
407
assertEval("class Clazz implements Interface {}",
408
ste(MAIN_SNIPPET, VALID, VALID, true, null),
409
ste(clazz, VALID, OVERWRITTEN, false, MAIN_SNIPPET));
410
assertCompletion("Clazz.fiel|", "field");
411
assertCompletion("new Clazz().fiel|");
412
assertCompletion("new Clazz() {}.fiel|");
413
}
414
415
public void testMethods() {
416
assertEval("interface Interface {" +
417
"default int defaultMethod() { return 0; }" +
418
"static int staticMethod() { return 0; }" +
419
"}");
420
Snippet clazz = classKey(assertEval("class Clazz {" +
421
"static int staticMethod() { return 0; }" +
422
"int method() { return 0; }" +
423
"}"));
424
assertCompletion("Interface.staticMeth|", "staticMethod()");
425
assertCompletion("Clazz.staticMeth|", "staticMethod()");
426
assertCompletion("new Interface() {}.defaultMe||", "defaultMethod()");
427
assertCompletion("new Clazz().staticMeth|");
428
assertCompletion("new Clazz().meth|", "method()");
429
assertEval("class Clazz implements Interface {}",
430
ste(MAIN_SNIPPET, VALID, VALID, true, null),
431
ste(clazz, VALID, OVERWRITTEN, false, MAIN_SNIPPET));
432
assertCompletion("Clazz.staticMeth|");
433
assertCompletion("new Clazz() {}.defaultM|", "defaultMethod()");
434
}
435
436
@Test
437
public void testUncompletedDeclaration() {
438
assertCompletion("class Clazz { Claz|", "Clazz");
439
assertCompletion("class Clazz { class A extends Claz|", "Clazz");
440
assertCompletion("class Clazz { Clazz clazz; Object o = claz|", "clazz");
441
assertCompletion("class Clazz { static Clazz clazz; Object o = claz|", "clazz");
442
assertCompletion("class Clazz { Clazz clazz; static Object o = claz|", true);
443
assertCompletion("class Clazz { void method(Claz|", "Clazz");
444
assertCompletion("class A { int method() { return 0; } int a = meth|", "method()");
445
assertCompletion("class A { int field = 0; int method() { return fiel|", "field");
446
assertCompletion("class A { static int method() { return 0; } int a = meth|", "method()");
447
assertCompletion("class A { static int field = 0; int method() { return fiel|", "field");
448
assertCompletion("class A { int method() { return 0; } static int a = meth|", true);
449
assertCompletion("class A { int field = 0; static int method() { return fiel|", true);
450
}
451
452
@Test
453
public void testClassDeclaration() {
454
assertEval("void ClazzM() {}");
455
assertEval("void InterfaceM() {}");
456
assertEval("interface Interface {}");
457
assertCompletion("interface A extends Interf|", "Interface");
458
assertCompletion("class A implements Interf|", "Interface");
459
assertEval("class Clazz {}");
460
assertCompletion("class A extends Claz|", "Clazz");
461
assertCompletion("class A extends Clazz implements Interf|", "Interface");
462
assertEval("interface Interface1 {}");
463
assertCompletion("class A extends Clazz implements Interface, Interf|", "Interface", "Interface1");
464
assertCompletion("interface A implements Claz|");
465
assertCompletion("interface A implements Inter|");
466
assertCompletion("class A implements Claz|", true);
467
assertCompletion("class A extends Clazz implements Interface, Interf|", true, "Interface1");
468
assertCompletion("class A extends Clazz implements Interface, Interf|", true, "Interface1");
469
assertEval("class InterfaceClazz {}");
470
assertCompletion("class A <T extends Claz|", "Clazz");
471
assertCompletion("class A <T extends Interf|", "Interface", "Interface1", "InterfaceClazz");
472
assertCompletion("class A <T extends Interface & Interf|", "Interface", "Interface1", "InterfaceClazz");
473
assertCompletion("class A <T extends Clazz & Interf|", "Interface", "Interface1", "InterfaceClazz");
474
assertCompletion("class A <T extends Claz|", true, "Clazz");
475
assertCompletion("class A <T extends Interf|", true, "Interface", "Interface1", "InterfaceClazz");
476
assertCompletion("class A <T extends Interface & Interf|", true, "Interface1");
477
assertCompletion("class A <T extends Clazz & Interf|", true, "Interface", "Interface1");
478
}
479
480
public void testMethodDeclaration() {
481
assertEval("void ClazzM() {}");
482
assertEval("void InterfaceM() {}");
483
assertEval("interface Interface {}");
484
assertCompletion("void m(Interf|", "Interface");
485
assertCompletion("void m(Interface i1, Interf|", "Interface");
486
assertEval("class InterfaceException extends Exception {}");
487
assertCompletion("void m(Interface i1) throws Interf|", "Interface", "InterfaceException");
488
assertCompletion("void m(Interface i1) throws Interf|", true, "InterfaceException");
489
}
490
491
public void testDocumentationOfUserDefinedMethods() {
492
assertEval("void f() {}");
493
assertSignature("f(|", "void f()");
494
assertEval("void f(int i) {}");
495
assertSignature("f(|", "void f()", "void f(int i)");
496
assertEval("<T> void f(T... ts) {}", DiagCheck.DIAG_WARNING, DiagCheck.DIAG_OK);
497
assertSignature("f(|", "void f()", "void f(int i)", "void <T>f(T... ts)");
498
assertEval("class A {}");
499
assertEval("void f(A a) {}");
500
assertSignature("f(|", "void f()", "void f(int i)", "void <T>f(T... ts)", "void f(A a)");
501
}
502
503
public void testClass() {
504
assertSignature("String|", "java.lang.String");
505
}
506
507
public void testDocumentationOfUserDefinedConstructors() {
508
Snippet a = classKey(assertEval("class A {}"));
509
assertSignature("new A(|", "A()");
510
Snippet a2 = classKey(assertEval("class A { A() {} A(int i) {}}",
511
ste(MAIN_SNIPPET, VALID, VALID, true, null),
512
ste(a, VALID, OVERWRITTEN, false, MAIN_SNIPPET)));
513
assertSignature("new A(|", "A()", "A(int i)");
514
assertEval("class A<T> { A(T a) {} A(int i) {} <U> A(T t, U u) {}}",
515
ste(MAIN_SNIPPET, VALID, VALID, true, null),
516
ste(a2, VALID, OVERWRITTEN, false, MAIN_SNIPPET));
517
assertSignature("new A(|", "A<T>(T a)", "A<T>(int i)", "<U> A<T>(T t, U u)");
518
}
519
520
public void testDocumentationOfOverriddenMethods() throws Exception {
521
dontReadParameterNamesFromClassFile();
522
assertSignature("\"\".wait(|",
523
"void Object.wait(long) throws InterruptedException",
524
"void Object.wait(long, int) throws InterruptedException",
525
"void Object.wait() throws InterruptedException");
526
assertEval("class Base {void method() {}}");
527
Snippet e = classKey(assertEval("class Extend extends Base {}"));
528
assertSignature("new Extend().method(|", "void Base.method()");
529
assertEval("class Extend extends Base {void method() {}}",
530
ste(MAIN_SNIPPET, VALID, VALID, true, null),
531
ste(e, VALID, OVERWRITTEN, false, MAIN_SNIPPET));
532
assertSignature("new Extend().method(|", "void Extend.method()");
533
}
534
535
public void testDocumentationOfInvisibleMethods() {
536
assertSignature("Object.wait(|");
537
assertSignature("\"\".indexOfSupplementary(|");
538
Snippet a = classKey(assertEval("class A {void method() {}}"));
539
assertSignature("A.method(|");
540
assertEval("class A {private void method() {}}",
541
ste(MAIN_SNIPPET, VALID, VALID, true, null),
542
ste(a, VALID, OVERWRITTEN, false, MAIN_SNIPPET));
543
assertSignature("new A().method(|");
544
}
545
546
public void testDocumentationOfInvisibleConstructors() {
547
assertSignature("new Compiler(|");
548
assertEval("class A { private A() {} }");
549
assertSignature("new A(|");
550
}
551
552
public void testDocumentationWithBoxing() {
553
assertEval("int primitive = 0;");
554
assertEval("Integer boxed = 0;");
555
assertEval("Object object = null;");
556
assertEval("void method(int n, Object o) { }");
557
assertEval("void method(Object n, int o) { }");
558
assertSignature("method(primitive,|",
559
"void method(int n, Object o)",
560
"void method(Object n, int o)");
561
assertSignature("method(boxed,|",
562
"void method(int n, Object o)",
563
"void method(Object n, int o)");
564
assertSignature("method(object,|",
565
"void method(Object n, int o)");
566
}
567
568
public void testDocumentationWithGenerics() {
569
class TestDocumentationWithGenerics {
570
private final Function<Integer, String> codeFacotry;
571
private final BiFunction<String, Integer, String> evalFormatter;
572
private final BiFunction<String, Integer, String> docFormatter;
573
int count;
574
575
TestDocumentationWithGenerics(
576
Function<Integer, String> codeFactory,
577
BiFunction<String, Integer, String> evalFormatter,
578
BiFunction<String, Integer, String> documentationFormatter) {
579
this.codeFacotry = codeFactory;
580
this.evalFormatter = evalFormatter;
581
this.docFormatter = documentationFormatter;
582
}
583
584
void assertDoc(String generics) {
585
assertDoc(generics, generics);
586
}
587
588
void assertDoc(String generics, String expectedGenerics) {
589
assertEval(evalFormatter.apply(generics, count));
590
assertSignature(codeFacotry.apply(count), docFormatter.apply(expectedGenerics, count));
591
count++;
592
}
593
}
594
595
TestDocumentationWithGenerics[] tests = {
596
new TestDocumentationWithGenerics(
597
i -> "f" + i + "(|",
598
(g, i) -> "<" + g + "> void f" + i + "() {}",
599
(g, i) -> "void <" + g + ">f" + i + "()"
600
),
601
new TestDocumentationWithGenerics(
602
i -> "new C" + i + "().f(|",
603
(g, i) -> "class C" + i + "<" + g + "> { void f() {} }",
604
(g, i) -> "void C" + i + "<" + g + ">.f()"
605
)
606
};
607
608
Arrays.stream(tests).forEach(t -> {
609
t.assertDoc("T");
610
t.assertDoc("T extends Object",
611
"T");
612
t.assertDoc("T extends String");
613
t.assertDoc("T extends java.lang.String",
614
"T extends String");
615
t.assertDoc("T extends Number & Comparable<T>");
616
t.assertDoc("T extends java.io.Serializable & CharSequence");
617
t.assertDoc("K, D, M extends java.util.Map<K, D>",
618
"K, D, M extends java.util.Map<K,D>");
619
});
620
}
621
622
public void testVarArgs() {
623
assertEval("int i = 0;");
624
assertEval("class Foo1 { static void m(int... i) { } } ");
625
assertCompletion("Foo1.m(|", true, "i");
626
assertCompletion("Foo1.m(i, |", true, "i");
627
assertCompletion("Foo1.m(i, i, |", true, "i");
628
assertEval("class Foo2 { static void m(String s, int... i) { } } ");
629
assertCompletion("Foo2.m(|", true);
630
assertCompletion("Foo2.m(i, |", true);
631
assertCompletion("Foo2.m(\"\", |", true, "i");
632
assertCompletion("Foo2.m(\"\", i, |", true, "i");
633
assertCompletion("Foo2.m(\"\", i, i, |", true, "i");
634
assertEval("class Foo3 { Foo3(String s, int... i) { } } ");
635
assertCompletion("new Foo3(|", true);
636
assertCompletion("new Foo3(i, |", true);
637
assertCompletion("new Foo3(\"\", |", true, "i");
638
assertCompletion("new Foo3(\"\", i, |", true, "i");
639
assertCompletion("new Foo3(\"\", i, i, |", true, "i");
640
assertEval("int[] ia = null;");
641
assertCompletion("Foo1.m(ia, |", true);
642
assertEval("class Foo4 { static void m(int... i) { } static void m(int[] ia, String str) { } } ");
643
assertEval("String str = null;");
644
assertCompletion("Foo4.m(ia, |", true, "str");
645
}
646
647
public void testConstructorAsMemberOf() {
648
assertEval("class Baz<X> { Baz(X x) { } } ");
649
assertEval("String str = null;");
650
assertEval("Integer i = null;");
651
assertCompletion("new Baz(|", true, "i", "str");
652
assertCompletion("new Baz<String>(|", true, "str");
653
assertCompletion("Baz<String> bz = new Baz<>(|", true, "str");
654
assertEval("class Foo { static void m(String str) {} static void m(Baz<String> baz) {} }");
655
assertCompletion("Foo.m(new Baz<>(|", true, "str");
656
}
657
658
public void testIntersection() {
659
assertEval("<Z extends Runnable & CharSequence> Z get() { return null; }");
660
assertEval("var v = get();");
661
assertCompletionIncludesExcludes("v.|", true, Set.of("run()", "length()"), Set.of());
662
assertCompletion("Runnable r = |", true, "get()", "v");
663
assertCompletion("CharSequence r = |", true, "get()", "v");
664
assertCompletion("Number r = |", true);
665
}
666
667
public void testAnonymous() {
668
assertEval("var v = new Runnable() { public void run() { } public int length() { return 0; } };");
669
assertCompletionIncludesExcludes("v.|", true, Set.of("run()", "length()"), Set.of());
670
assertCompletion("Runnable r = |", true, "v");
671
assertCompletion("CharSequence r = |", true);
672
}
673
674
public void testCompletionInAnonymous() {
675
assertCompletionIncludesExcludes("new Undefined() { int i = \"\".l|", Set.of("length()"), Set.of());
676
}
677
678
public void testMemberReferences() {
679
assertEval("class C {" +
680
" public static String stat() { return null; }" +
681
" public static void statVoid(String s) {}" +
682
" public static Integer statConvert1(String s) { return null; }" +
683
" public static String statConvert2(Integer s) { return null; }" +
684
" public static String statConvert3(CharSequence s) { return null; }" +
685
" public String inst() { return null; }" +
686
" public void instVoid(String s) { }" +
687
"}");
688
assertEval("interface FI { public void t(String s); }");
689
assertCompletion("FI fi = C::|", (Boolean) null, "stat", "statConvert1", "statConvert2", "statConvert3", "statVoid");
690
assertCompletion("FI fi = C::|", true, "statConvert1", "statConvert3","statVoid");
691
assertCompletion("FI fi = new C()::i|", (Boolean) null, "inst", "instVoid");
692
assertCompletion("FI fi = new C()::i|", true, "instVoid");
693
assertEval("interface FI2<R, P> { public R t(P p); }");
694
assertCompletion("FI2<String, Integer> fi = C::|", (Boolean) null, "stat", "statConvert1", "statConvert2", "statConvert3", "statVoid");
695
assertCompletion("FI2<String, Integer> fi = C::|", true, "statConvert2");
696
assertCompletion("FI2<String, CharSequence> fi = C::|", true, "statConvert3");
697
assertCompletion("FI2<String, String> fi = C::|", true, "statConvert3");
698
assertCompletion("FI2<Object, String> fi = C::|", true, "statConvert1", "statConvert3");
699
}
700
701
public void testBrokenLambdaCompletion() {
702
assertEval("interface Consumer<T> { public void consume(T t); }");
703
assertEval("interface Function<T, R> { public R convert(T t); }");
704
assertEval("<T> void m1(T t, Consumer<T> f) { }");
705
assertCompletion("m1(\"\", x -> {x.tri|", "trim()");
706
assertEval("<T> void m2(T t, Function<T, String> f) { }");
707
assertCompletion("m2(\"\", x -> {x.tri|", "trim()");
708
assertEval("<T> void m3(T t, Consumer<T> f, int i) { }");
709
assertCompletion("m3(\"\", x -> {x.tri|", "trim()");
710
assertEval("<T> void m4(T t, Function<T, String> f, int i) { }");
711
assertCompletion("m4(\"\", x -> {x.tri|", "trim()");
712
assertEval("<T> T m5(Consumer<T> f) { return null; }");
713
assertCompletion("String s = m5(x -> {x.tri|", "trim()");
714
assertEval("<T> T m6(Function<T, String> f) { return null; }");
715
assertCompletion("String s = m6(x -> {x.tri|", "trim()");
716
assertEval("<T> T m7(Consumer<T> f, int i) { return null; }");
717
assertCompletion("String s = m7(x -> {x.tri|", "trim()");
718
assertEval("<T> T m8(Function<T, String> f, int i) { return null; }");
719
assertCompletion("String s = m8(x -> {x.tri|", "trim()");
720
}
721
722
@BeforeMethod
723
public void setUp() {
724
super.setUp();
725
726
Path srcZip = Paths.get("src.zip");
727
728
try (JarOutputStream out = new JarOutputStream(Files.newOutputStream(srcZip))) {
729
out.putNextEntry(new JarEntry("java/lang/System.java"));
730
out.write(("package java.lang;\n" +
731
"public class System {\n" +
732
" public String getProperty(String key) { return null; }\n" +
733
" public String getProperty(String key, String def) { return def; }\n" +
734
"}\n").getBytes());
735
} catch (IOException ex) {
736
throw new IllegalStateException(ex);
737
}
738
739
try {
740
Field availableSources = getAnalysis().getClass().getDeclaredField("availableSources");
741
availableSources.setAccessible(true);
742
availableSources.set(getAnalysis(), Arrays.asList(srcZip));
743
} catch (NoSuchFieldException | IllegalArgumentException | IllegalAccessException ex) {
744
throw new IllegalStateException(ex);
745
}
746
}
747
748
private void dontReadParameterNamesFromClassFile() throws Exception {
749
Field keepParameterNames = getAnalysis().getClass().getDeclaredField("keepParameterNames");
750
keepParameterNames.setAccessible(true);
751
keepParameterNames.set(getAnalysis(), new String[0]);
752
}
753
754
@Test(enabled = false) //TODO 8171829
755
public void testBrokenClassFile2() throws IOException {
756
Path broken = outDir.resolve("broken");
757
compiler.compile(broken,
758
"package p;\n" +
759
"public class BrokenA {\n" +
760
"}",
761
"package p.q;\n" +
762
"public class BrokenB {\n" +
763
"}",
764
"package p;\n" +
765
"public class BrokenC {\n" +
766
"}");
767
Path cp = compiler.getPath(broken);
768
Path target = cp.resolve("p").resolve("BrokenB.class");
769
Files.deleteIfExists(target);
770
Files.move(cp.resolve("p").resolve("q").resolve("BrokenB.class"), target);
771
addToClasspath(cp);
772
773
assertEval("import p.*;");
774
assertCompletion("Broke|", "BrokenA", "BrokenC");
775
}
776
}
777
778