CompletionSuggestionTest.java revision 3792:d516975e8110
1138593Ssam/* 2138593Ssam * Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved. 3138593Ssam * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4138593Ssam * 5138593Ssam * This code is free software; you can redistribute it and/or modify it 6138593Ssam * under the terms of the GNU General Public License version 2 only, as 7138593Ssam * published by the Free Software Foundation. 8138593Ssam * 9138593Ssam * This code is distributed in the hope that it will be useful, but WITHOUT 10138593Ssam * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11138593Ssam * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12138593Ssam * version 2 for more details (a copy is included in the LICENSE file that 13138593Ssam * accompanied this code). 14138593Ssam * 15138593Ssam * You should have received a copy of the GNU General Public License version 16138593Ssam * 2 along with this work; if not, write to the Free Software Foundation, 17138593Ssam * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18138593Ssam * 19138593Ssam * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20138593Ssam * or visit www.oracle.com if you need additional information or have any 21138593Ssam * questions. 22138593Ssam */ 23138593Ssam 24138593Ssam/* 25138593Ssam * @test 26138593Ssam * @bug 8131025 8141092 8153761 8145263 8131019 27138593Ssam * @summary Test Completion and Documentation 28138593Ssam * @library /tools/lib 29138593Ssam * @modules jdk.compiler/com.sun.tools.javac.api 30138593Ssam * jdk.compiler/com.sun.tools.javac.main 31138593Ssam * jdk.jdeps/com.sun.tools.javap 32138593Ssam * jdk.jshell/jdk.jshell:open 33138593Ssam * @build toolbox.ToolBox toolbox.JarTask toolbox.JavacTask 34138593Ssam * @build KullaTesting TestingInputStream Compiler 35138593Ssam * @run testng CompletionSuggestionTest 36138593Ssam */ 37138593Ssam 38138593Ssamimport java.io.IOException; 39138593Ssamimport java.lang.reflect.Field; 40138593Ssamimport java.nio.file.Files; 41138593Ssamimport java.nio.file.Path; 42138593Ssamimport java.nio.file.Paths; 43138593Ssamimport java.util.Arrays; 44138593Ssamimport java.util.Collections; 45166956Ssamimport java.util.Set; 46138593Ssamimport java.util.HashSet; 47138593Ssamimport java.util.function.BiFunction; 48138593Ssamimport java.util.function.Function; 49138593Ssamimport java.util.jar.JarEntry; 50138593Ssamimport java.util.jar.JarOutputStream; 51138593Ssam 52138593Ssamimport jdk.jshell.Snippet; 53138593Ssamimport org.testng.annotations.BeforeMethod; 54138593Ssamimport org.testng.annotations.Test; 55191121Sbrooks 56138593Ssamimport static jdk.jshell.Snippet.Status.VALID; 57138593Ssamimport static jdk.jshell.Snippet.Status.OVERWRITTEN; 58138593Ssam 59166956Ssam@Test 60138593Ssampublic class CompletionSuggestionTest extends KullaTesting { 61138593Ssam 62138593Ssam private final Compiler compiler = new Compiler(); 63138593Ssam private final Path outDir = Paths.get("completion_suggestion_test"); 64138593Ssam 65166956Ssam public void testMemberExpr() { 66138593Ssam assertEval("class Test { static void test() { } }"); 67138593Ssam assertCompletion("Test.t|", "test()"); 68138593Ssam assertEval("Test ccTestInstance = new Test();"); 69138593Ssam assertCompletion("ccTestInstance.t|", "toString()"); 70138593Ssam assertCompletion(" ccTe|", "ccTestInstance"); 71166956Ssam assertCompletion("String value = ccTestInstance.to|", "toString()"); 72166956Ssam assertCompletion("java.util.Coll|", "Collection", "Collections"); 73166956Ssam assertCompletion("String.cla|", "class"); 74138593Ssam assertCompletion("boolean.cla|", "class"); 75138593Ssam assertCompletion("byte.cla|", "class"); 76138593Ssam assertCompletion("short.cla|", "class"); 77138593Ssam assertCompletion("char.cla|", "class"); 78166956Ssam assertCompletion("int.cla|", "class"); 79166956Ssam assertCompletion("float.cla|", "class"); 80138593Ssam assertCompletion("long.cla|", "class"); 81138593Ssam assertCompletion("double.cla|", "class"); 82138593Ssam assertCompletion("void.cla|", "class"); 83166956Ssam assertCompletion("Object[].|", "class"); 84166956Ssam assertCompletion("int[].|", "class"); 85166956Ssam assertEval("Object[] ao = null;"); 86138593Ssam assertCompletion("int i = ao.|", "length"); 87138593Ssam assertEval("int[] ai = null;"); 88138593Ssam assertCompletion("int i = ai.|", "length"); 89138593Ssam assertCompletionIncludesExcludes("\"\".|", 90138593Ssam new HashSet<>(Collections.emptyList()), 91138593Ssam new HashSet<>(Arrays.asList("String("))); 92138593Ssam assertEval("double d = 0;"); 93138593Ssam assertEval("void m() {}"); 94138593Ssam assertCompletionIncludesExcludes("d.|", 95138593Ssam new HashSet<>(Collections.emptyList()), 96138593Ssam new HashSet<>(Arrays.asList("class"))); 97138593Ssam assertCompletionIncludesExcludes("m().|", 98138593Ssam new HashSet<>(Collections.emptyList()), 99138593Ssam new HashSet<>(Arrays.asList("class"))); 100138593Ssam assertEval("class C {class D {} static class E {} enum F {} interface H {} void method() {} int number;}"); 101138593Ssam assertCompletionIncludesExcludes("C.|", 102138593Ssam new HashSet<>(Arrays.asList("D", "E", "F", "H", "class")), 103138593Ssam new HashSet<>(Arrays.asList("method()", "number"))); 104138593Ssam assertCompletionIncludesExcludes("new C().|", 105138593Ssam new HashSet<>(Arrays.asList("method()", "number")), 106138593Ssam new HashSet<>(Arrays.asList("D", "E", "F", "H", "class"))); 107138593Ssam assertCompletionIncludesExcludes("new C() {}.|", 108138593Ssam new HashSet<>(Arrays.asList("method()", "number")), 109138593Ssam new HashSet<>(Arrays.asList("D", "E", "F", "H", "class"))); 110138593Ssam } 111138593Ssam 112138593Ssam public void testStartOfExpression() { 113138593Ssam assertEval("int ccTest = 0;"); 114138593Ssam assertCompletion("System.err.println(cc|", "ccTest"); 115138593Ssam assertCompletion("for (int i = cc|", "ccTest"); 116138593Ssam } 117138593Ssam 118138593Ssam public void testParameter() { 119138593Ssam assertCompletion("class C{void method(int num){num|", "num"); 120138593Ssam } 121138593Ssam 122138593Ssam public void testPrimitive() { 123138593Ssam Set<String> primitives = new HashSet<>(Arrays.asList("boolean", "char", "byte", "short", "int", "long", "float", "double")); 124138593Ssam Set<String> onlyVoid = new HashSet<>(Collections.singletonList("void")); 125138593Ssam Set<String> primitivesOrVoid = new HashSet<>(primitives); 126138593Ssam primitivesOrVoid.addAll(onlyVoid); 127138593Ssam 128138593Ssam assertCompletionIncludesExcludes("|", 129138593Ssam primitivesOrVoid, 130138593Ssam new HashSet<>(Collections.emptyList())); 131138593Ssam assertCompletionIncludesExcludes("int num = |", 132138593Ssam primitivesOrVoid, 133138593Ssam new HashSet<>(Collections.emptyList())); 134138593Ssam assertCompletionIncludesExcludes("num = |", 135138593Ssam primitivesOrVoid, 136138593Ssam new HashSet<>(Collections.emptyList())); 137138593Ssam assertCompletionIncludesExcludes("class C{void m() {|", 138138593Ssam primitivesOrVoid, 139138593Ssam new HashSet<>(Collections.emptyList())); 140138593Ssam assertCompletionIncludesExcludes("void method(|", 141138593Ssam primitives, 142138593Ssam onlyVoid); 143138593Ssam assertCompletionIncludesExcludes("void method(int num, |", 144138593Ssam primitives, 145138593Ssam onlyVoid); 146138593Ssam assertCompletion("new java.util.ArrayList<doub|"); 147138593Ssam assertCompletion("class A extends doubl|"); 148138593Ssam assertCompletion("class A implements doubl|"); 149138593Ssam assertCompletion("interface A extends doubl|"); 150138593Ssam assertCompletion("enum A implements doubl|"); 151138593Ssam assertCompletion("class A<T extends doubl|"); 152138593Ssam } 153138593Ssam 154147437Sume public void testEmpty() { 155147437Sume assertCompletionIncludesExcludes("|", 156138593Ssam new HashSet<>(Arrays.asList("Object", "Void")), 157138593Ssam new HashSet<>(Arrays.asList("$REPL00DOESNOTMATTER"))); 158138593Ssam assertCompletionIncludesExcludes("V|", 159138593Ssam new HashSet<>(Collections.singletonList("Void")), 160138593Ssam new HashSet<>(Collections.singletonList("Object"))); 161147437Sume assertCompletionIncludesExcludes("{ |", 162147437Sume new HashSet<>(Arrays.asList("Object", "Void")), 163138593Ssam new HashSet<>(Arrays.asList("$REPL00DOESNOTMATTER"))); 164138593Ssam } 165138593Ssam 166138593Ssam public void testSmartCompletion() { 167138593Ssam assertEval("int ccTest1 = 0;"); 168138593Ssam assertEval("int ccTest2 = 0;"); 169138593Ssam assertEval("String ccTest3 = null;"); 170138593Ssam assertEval("void method(int i, String str) { }"); 171138593Ssam assertEval("void method(String str, int i) { }"); 172191121Sbrooks assertEval("java.util.List<String> list = null;"); 173138593Ssam assertCompletion("int ccTest4 = |", true, "ccTest1", "ccTest2"); 174138593Ssam assertCompletion("ccTest2 = |", true, "ccTest1", "ccTest2"); 175138593Ssam assertCompletion("int ccTest4 = ccTe|", "ccTest1", "ccTest2", "ccTest3"); 176138593Ssam assertCompletion("int ccTest4 = ccTest3.len|", true, "length()"); 177138593Ssam assertCompletion("method(|", true, "ccTest1", "ccTest2", "ccTest3"); 178138593Ssam assertCompletion("method(0, |", true, "ccTest3"); 179138593Ssam assertCompletion("list.add(|", true, "ccTest1", "ccTest2", "ccTest3"); 180138593Ssam assertCompletion("list.add(0, |", true, "ccTest3"); 181138593Ssam assertCompletion("new String(|", true, "ccTest3"); 182138593Ssam assertCompletion("new String(new char[0], |", true, "ccTest1", "ccTest2"); 183138593Ssam assertCompletionIncludesExcludes("new jav|", new HashSet<>(Arrays.asList("java", "javax")), Collections.emptySet()); 184138593Ssam assertCompletion("Class<String> clazz = String.c|", true, "class"); 185138593Ssam 186138593Ssam Snippet klass = classKey(assertEval("class Klass {void method(int n) {} private void method(String str) {}}")); 187138593Ssam assertCompletion("new Klass().method(|", true, "ccTest1", "ccTest2"); 188138593Ssam Snippet klass2 = classKey(assertEval("class Klass {static void method(int n) {} void method(String str) {}}", 189138593Ssam ste(MAIN_SNIPPET, VALID, VALID, true, null), 190138593Ssam ste(klass, VALID, OVERWRITTEN, false, MAIN_SNIPPET))); 191138593Ssam assertCompletion("Klass.method(|", true, "ccTest1", "ccTest2"); 192138593Ssam assertEval("class Klass {Klass(int n) {} private Klass(String str) {}}", 193138593Ssam ste(MAIN_SNIPPET, VALID, VALID, true, null), 194138593Ssam ste(klass2, VALID, OVERWRITTEN, false, MAIN_SNIPPET)); 195138593Ssam assertCompletion("new Klass(|", true, "ccTest1", "ccTest2"); 196138593Ssam } 197138593Ssam 198138593Ssam public void testSmartCompletionInOverriddenMethodInvocation() { 199138593Ssam assertEval("int ccTest1 = 0;"); 200138593Ssam 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 testBrokenClassFile() throws Exception { 299 Compiler compiler = new Compiler(); 300 Path testOutDir = Paths.get("CompletionTestBrokenClassFile"); 301 String input = "package test.inner; public class Test {}"; 302 compiler.compile(testOutDir, input); 303 addToClasspath(compiler.getPath(testOutDir).resolve("test")); 304 assertCompletion("import inner.|"); 305 } 306 307 public void testDocumentation() throws Exception { 308 dontReadParameterNamesFromClassFile(); 309 assertSignature("System.getProperty(|", 310 "String System.getProperty(String key)", 311 "String System.getProperty(String key, String def)"); 312 assertEval("char[] chars = null;"); 313 assertSignature("new String(chars, |", 314 "String(char[], int, int)"); 315 assertSignature("String.format(|", 316 "String String.format(String, Object...)", 317 "String String.format(java.util.Locale, String, Object...)"); 318 assertSignature("\"\".getBytes(\"\"|", "void String.getBytes(int, int, byte[], int)", 319 "byte[] String.getBytes(String) throws java.io.UnsupportedEncodingException", 320 "byte[] String.getBytes(java.nio.charset.Charset)"); 321 assertSignature("\"\".getBytes(\"\" |", "void String.getBytes(int, int, byte[], int)", 322 "byte[] String.getBytes(String) throws java.io.UnsupportedEncodingException", 323 "byte[] String.getBytes(java.nio.charset.Charset)"); 324 } 325 326 public void testMethodsWithNoArguments() throws Exception { 327 dontReadParameterNamesFromClassFile(); 328 assertSignature("System.out.println(|", 329 "void java.io.PrintStream.println()", 330 "void java.io.PrintStream.println(boolean)", 331 "void java.io.PrintStream.println(char)", 332 "void java.io.PrintStream.println(int)", 333 "void java.io.PrintStream.println(long)", 334 "void java.io.PrintStream.println(float)", 335 "void java.io.PrintStream.println(double)", 336 "void java.io.PrintStream.println(char[])", 337 "void java.io.PrintStream.println(String)", 338 "void java.io.PrintStream.println(Object)"); 339 } 340 341 public void testErroneous() { 342 assertCompletion("Undefined.|"); 343 assertSignature("does.not.exist|"); 344 } 345 346 public void testClinit() { 347 assertEval("enum E{;}"); 348 assertEval("class C{static{}}"); 349 assertCompletionIncludesExcludes("E.|", Collections.emptySet(), new HashSet<>(Collections.singletonList("<clinit>"))); 350 assertCompletionIncludesExcludes("C.|", Collections.emptySet(), new HashSet<>(Collections.singletonList("<clinit>"))); 351 } 352 353 public void testMethodHeaderContext() { 354 assertCompletion("private void f(Runn|", "Runnable"); 355 assertCompletion("void f(Runn|", "Runnable"); 356 assertCompletion("void f(Object o1, Runn|", "Runnable"); 357 assertCompletion("void f(Object o1) throws Num|", true, "NumberFormatException"); 358 assertCompletion("void f(Object o1) throws java.lang.Num|", true, "NumberFormatException"); 359 assertEval("class HogeHoge {static class HogeHogeException extends Exception {}}"); 360 assertCompletion("void f(Object o1) throws Hoge|", "HogeHoge"); 361 assertCompletion("void f(Object o1) throws HogeHoge.|", true, "HogeHogeException"); 362 } 363 364 public void testTypeVariables() { 365 assertCompletion("class A<TYPE> { public void test() { TY|", "TYPE"); 366 assertCompletion("class A<TYPE> { public static void test() { TY|"); 367 assertCompletion("class A<TYPE> { public <TYPE> void test() { TY|", "TYPE"); 368 assertCompletion("class A<TYPE> { public static <TYPE> void test() { TY|", "TYPE"); 369 } 370 371 public void testGeneric() { 372 assertEval("import java.util.concurrent.*;"); 373 assertCompletion("java.util.List<Integ|", "Integer"); 374 assertCompletion("class A<TYPE extends Call|", "Callable"); 375 assertCompletion("class A<TYPE extends Callable<TY|", "TYPE"); 376 assertCompletion("<TYPE> void f(TY|", "TYPE"); 377 assertCompletion("class A<TYPE extends Callable<? sup|", "super"); 378 assertCompletion("class A<TYPE extends Callable<? super TY|", "TYPE"); 379 } 380 381 public void testFields() { 382 assertEval("interface Interface { int field = 0; }"); 383 Snippet clazz = classKey(assertEval("class Clazz {" + 384 "static int staticField = 0;" + 385 "int field = 0;" + 386 " }")); 387 assertCompletion("Interface.fiel|", "field"); 388 assertCompletion("Clazz.staticFiel|", "staticField"); 389 assertCompletion("new Interface() {}.fiel|"); 390 assertCompletion("new Clazz().staticFiel|"); 391 assertCompletion("new Clazz().fiel|", "field"); 392 assertCompletion("new Clazz() {}.fiel|", "field"); 393 assertEval("class Clazz implements Interface {}", 394 ste(MAIN_SNIPPET, VALID, VALID, true, null), 395 ste(clazz, VALID, OVERWRITTEN, false, MAIN_SNIPPET)); 396 assertCompletion("Clazz.fiel|", "field"); 397 assertCompletion("new Clazz().fiel|"); 398 assertCompletion("new Clazz() {}.fiel|"); 399 } 400 401 public void testMethods() { 402 assertEval("interface Interface {" + 403 "default int defaultMethod() { return 0; }" + 404 "static int staticMethod() { return 0; }" + 405 "}"); 406 Snippet clazz = classKey(assertEval("class Clazz {" + 407 "static int staticMethod() { return 0; }" + 408 "int method() { return 0; }" + 409 "}")); 410 assertCompletion("Interface.staticMeth|", "staticMethod()"); 411 assertCompletion("Clazz.staticMeth|", "staticMethod()"); 412 assertCompletion("new Interface() {}.defaultMe||", "defaultMethod()"); 413 assertCompletion("new Clazz().staticMeth|"); 414 assertCompletion("new Clazz().meth|", "method()"); 415 assertEval("class Clazz implements Interface {}", 416 ste(MAIN_SNIPPET, VALID, VALID, true, null), 417 ste(clazz, VALID, OVERWRITTEN, false, MAIN_SNIPPET)); 418 assertCompletion("Clazz.staticMeth|"); 419 assertCompletion("new Clazz() {}.defaultM|", "defaultMethod()"); 420 } 421 422 @Test 423 public void testUncompletedDeclaration() { 424 assertCompletion("class Clazz { Claz|", "Clazz"); 425 assertCompletion("class Clazz { class A extends Claz|", "Clazz"); 426 assertCompletion("class Clazz { Clazz clazz; Object o = claz|", "clazz"); 427 assertCompletion("class Clazz { static Clazz clazz; Object o = claz|", "clazz"); 428 assertCompletion("class Clazz { Clazz clazz; static Object o = claz|", true); 429 assertCompletion("class Clazz { void method(Claz|", "Clazz"); 430 assertCompletion("class A { int method() { return 0; } int a = meth|", "method()"); 431 assertCompletion("class A { int field = 0; int method() { return fiel|", "field"); 432 assertCompletion("class A { static int method() { return 0; } int a = meth|", "method()"); 433 assertCompletion("class A { static int field = 0; int method() { return fiel|", "field"); 434 assertCompletion("class A { int method() { return 0; } static int a = meth|", true); 435 assertCompletion("class A { int field = 0; static int method() { return fiel|", true); 436 } 437 438 @Test 439 public void testClassDeclaration() { 440 assertEval("void ClazzM() {}"); 441 assertEval("void InterfaceM() {}"); 442 assertEval("interface Interface {}"); 443 assertCompletion("interface A extends Interf|", "Interface"); 444 assertCompletion("class A implements Interf|", "Interface"); 445 assertEval("class Clazz {}"); 446 assertCompletion("class A extends Claz|", "Clazz"); 447 assertCompletion("class A extends Clazz implements Interf|", "Interface"); 448 assertEval("interface Interface1 {}"); 449 assertCompletion("class A extends Clazz implements Interface, Interf|", "Interface", "Interface1"); 450 assertCompletion("interface A implements Claz|"); 451 assertCompletion("interface A implements Inter|"); 452 assertCompletion("class A implements Claz|", true); 453 assertCompletion("class A extends Clazz implements Interface, Interf|", true, "Interface1"); 454 assertCompletion("class A extends Clazz implements Interface, Interf|", true, "Interface1"); 455 assertEval("class InterfaceClazz {}"); 456 assertCompletion("class A <T extends Claz|", "Clazz"); 457 assertCompletion("class A <T extends Interf|", "Interface", "Interface1", "InterfaceClazz"); 458 assertCompletion("class A <T extends Interface & Interf|", "Interface", "Interface1", "InterfaceClazz"); 459 assertCompletion("class A <T extends Clazz & Interf|", "Interface", "Interface1", "InterfaceClazz"); 460 assertCompletion("class A <T extends Claz|", true, "Clazz"); 461 assertCompletion("class A <T extends Interf|", true, "Interface", "Interface1", "InterfaceClazz"); 462 assertCompletion("class A <T extends Interface & Interf|", true, "Interface1"); 463 assertCompletion("class A <T extends Clazz & Interf|", true, "Interface", "Interface1"); 464 } 465 466 public void testMethodDeclaration() { 467 assertEval("void ClazzM() {}"); 468 assertEval("void InterfaceM() {}"); 469 assertEval("interface Interface {}"); 470 assertCompletion("void m(Interf|", "Interface"); 471 assertCompletion("void m(Interface i1, Interf|", "Interface"); 472 assertEval("class InterfaceException extends Exception {}"); 473 assertCompletion("void m(Interface i1) throws Interf|", "Interface", "InterfaceException"); 474 assertCompletion("void m(Interface i1) throws Interf|", true, "InterfaceException"); 475 } 476 477 public void testDocumentationOfUserDefinedMethods() { 478 assertEval("void f() {}"); 479 assertSignature("f(|", "void f()"); 480 assertEval("void f(int i) {}"); 481 assertSignature("f(|", "void f()", "void f(int i)"); 482 assertEval("<T> void f(T... ts) {}", DiagCheck.DIAG_WARNING, DiagCheck.DIAG_OK); 483 assertSignature("f(|", "void f()", "void f(int i)", "void <T>f(T... ts)"); 484 assertEval("class A {}"); 485 assertEval("void f(A a) {}"); 486 assertSignature("f(|", "void f()", "void f(int i)", "void <T>f(T... ts)", "void f(A a)"); 487 } 488 489 public void testClass() { 490 assertSignature("String|", "java.lang.String"); 491 } 492 493 public void testDocumentationOfUserDefinedConstructors() { 494 Snippet a = classKey(assertEval("class A {}")); 495 assertSignature("new A(|", "A()"); 496 Snippet a2 = classKey(assertEval("class A { A() {} A(int i) {}}", 497 ste(MAIN_SNIPPET, VALID, VALID, true, null), 498 ste(a, VALID, OVERWRITTEN, false, MAIN_SNIPPET))); 499 assertSignature("new A(|", "A()", "A(int i)"); 500 assertEval("class A<T> { A(T a) {} A(int i) {} <U> A(T t, U u) {}}", 501 ste(MAIN_SNIPPET, VALID, VALID, true, null), 502 ste(a2, VALID, OVERWRITTEN, false, MAIN_SNIPPET)); 503 assertSignature("new A(|", "A<T>(T a)", "A<T>(int i)", "<U> A<T>(T t, U u)"); 504 } 505 506 public void testDocumentationOfOverriddenMethods() throws Exception { 507 dontReadParameterNamesFromClassFile(); 508 assertSignature("\"\".wait(|", 509 "void Object.wait(long) throws InterruptedException", 510 "void Object.wait(long, int) throws InterruptedException", 511 "void Object.wait() throws InterruptedException"); 512 assertEval("class Base {void method() {}}"); 513 Snippet e = classKey(assertEval("class Extend extends Base {}")); 514 assertSignature("new Extend().method(|", "void Base.method()"); 515 assertEval("class Extend extends Base {void method() {}}", 516 ste(MAIN_SNIPPET, VALID, VALID, true, null), 517 ste(e, VALID, OVERWRITTEN, false, MAIN_SNIPPET)); 518 assertSignature("new Extend().method(|", "void Extend.method()"); 519 } 520 521 public void testDocumentationOfInvisibleMethods() { 522 assertSignature("Object.wait(|"); 523 assertSignature("\"\".indexOfSupplementary(|"); 524 Snippet a = classKey(assertEval("class A {void method() {}}")); 525 assertSignature("A.method(|"); 526 assertEval("class A {private void method() {}}", 527 ste(MAIN_SNIPPET, VALID, VALID, true, null), 528 ste(a, VALID, OVERWRITTEN, false, MAIN_SNIPPET)); 529 assertSignature("new A().method(|"); 530 } 531 532 public void testDocumentationOfInvisibleConstructors() { 533 assertSignature("new Compiler(|"); 534 assertEval("class A { private A() {} }"); 535 assertSignature("new A(|"); 536 } 537 538 public void testDocumentationWithBoxing() { 539 assertEval("int primitive = 0;"); 540 assertEval("Integer boxed = 0;"); 541 assertEval("Object object = null;"); 542 assertEval("void method(int n, Object o) { }"); 543 assertEval("void method(Object n, int o) { }"); 544 assertSignature("method(primitive,|", 545 "void method(int n, Object o)", 546 "void method(Object n, int o)"); 547 assertSignature("method(boxed,|", 548 "void method(int n, Object o)", 549 "void method(Object n, int o)"); 550 assertSignature("method(object,|", 551 "void method(Object n, int o)"); 552 } 553 554 public void testDocumentationWithGenerics() { 555 class TestDocumentationWithGenerics { 556 private final Function<Integer, String> codeFacotry; 557 private final BiFunction<String, Integer, String> evalFormatter; 558 private final BiFunction<String, Integer, String> docFormatter; 559 int count; 560 561 TestDocumentationWithGenerics( 562 Function<Integer, String> codeFactory, 563 BiFunction<String, Integer, String> evalFormatter, 564 BiFunction<String, Integer, String> documentationFormatter) { 565 this.codeFacotry = codeFactory; 566 this.evalFormatter = evalFormatter; 567 this.docFormatter = documentationFormatter; 568 } 569 570 void assertDoc(String generics) { 571 assertDoc(generics, generics); 572 } 573 574 void assertDoc(String generics, String expectedGenerics) { 575 assertEval(evalFormatter.apply(generics, count)); 576 assertSignature(codeFacotry.apply(count), docFormatter.apply(expectedGenerics, count)); 577 count++; 578 } 579 } 580 581 TestDocumentationWithGenerics[] tests = { 582 new TestDocumentationWithGenerics( 583 i -> "f" + i + "(|", 584 (g, i) -> "<" + g + "> void f" + i + "() {}", 585 (g, i) -> "void <" + g + ">f" + i + "()" 586 ), 587 new TestDocumentationWithGenerics( 588 i -> "new C" + i + "().f(|", 589 (g, i) -> "class C" + i + "<" + g + "> { void f() {} }", 590 (g, i) -> "void C" + i + "<" + g + ">.f()" 591 ) 592 }; 593 594 Arrays.stream(tests).forEach(t -> { 595 t.assertDoc("T"); 596 t.assertDoc("T extends Object", 597 "T"); 598 t.assertDoc("T extends String"); 599 t.assertDoc("T extends java.lang.String", 600 "T extends String"); 601 t.assertDoc("T extends Number & Comparable<T>"); 602 t.assertDoc("T extends java.io.Serializable & CharSequence"); 603 t.assertDoc("K, D, M extends java.util.Map<K, D>", 604 "K, D, M extends java.util.Map<K,D>"); 605 }); 606 } 607 608 public void testVarArgs() { 609 assertEval("int i = 0;"); 610 assertEval("class Foo1 { static void m(int... i) { } } "); 611 assertCompletion("Foo1.m(|", true, "i"); 612 assertCompletion("Foo1.m(i, |", true, "i"); 613 assertCompletion("Foo1.m(i, i, |", true, "i"); 614 assertEval("class Foo2 { static void m(String s, int... i) { } } "); 615 assertCompletion("Foo2.m(|", true); 616 assertCompletion("Foo2.m(i, |", true); 617 assertCompletion("Foo2.m(\"\", |", true, "i"); 618 assertCompletion("Foo2.m(\"\", i, |", true, "i"); 619 assertCompletion("Foo2.m(\"\", i, i, |", true, "i"); 620 assertEval("class Foo3 { Foo3(String s, int... i) { } } "); 621 assertCompletion("new Foo3(|", true); 622 assertCompletion("new Foo3(i, |", true); 623 assertCompletion("new Foo3(\"\", |", true, "i"); 624 assertCompletion("new Foo3(\"\", i, |", true, "i"); 625 assertCompletion("new Foo3(\"\", i, i, |", true, "i"); 626 assertEval("int[] ia = null;"); 627 assertCompletion("Foo1.m(ia, |", true); 628 assertEval("class Foo4 { static void m(int... i) { } static void m(int[] ia, String str) { } } "); 629 assertEval("String str = null;"); 630 assertCompletion("Foo4.m(ia, |", true, "str"); 631 } 632 633 public void testConstructorAsMemberOf() { 634 assertEval("class Baz<X> { Baz(X x) { } } "); 635 assertEval("String str = null;"); 636 assertEval("Integer i = null;"); 637 assertCompletion("new Baz(|", true, "i", "str"); 638 assertCompletion("new Baz<String>(|", true, "str"); 639 assertCompletion("Baz<String> bz = new Baz<>(|", true, "str"); 640 assertEval("class Foo { static void m(String str) {} static void m(Baz<String> baz) {} }"); 641 assertCompletion("Foo.m(new Baz<>(|", true, "str"); 642 } 643 644 @BeforeMethod 645 public void setUp() { 646 super.setUp(); 647 648 Path srcZip = Paths.get("src.zip"); 649 650 try (JarOutputStream out = new JarOutputStream(Files.newOutputStream(srcZip))) { 651 out.putNextEntry(new JarEntry("java/lang/System.java")); 652 out.write(("package java.lang;\n" + 653 "public class System {\n" + 654 " public String getProperty(String key) { return null; }\n" + 655 " public String getProperty(String key, String def) { return def; }\n" + 656 "}\n").getBytes()); 657 } catch (IOException ex) { 658 throw new IllegalStateException(ex); 659 } 660 661 try { 662 Field availableSources = getAnalysis().getClass().getDeclaredField("availableSources"); 663 availableSources.setAccessible(true); 664 availableSources.set(getAnalysis(), Arrays.asList(srcZip)); 665 } catch (NoSuchFieldException | IllegalArgumentException | IllegalAccessException ex) { 666 throw new IllegalStateException(ex); 667 } 668 } 669 670 private void dontReadParameterNamesFromClassFile() throws Exception { 671 Field keepParameterNames = getAnalysis().getClass().getDeclaredField("keepParameterNames"); 672 keepParameterNames.setAccessible(true); 673 keepParameterNames.set(getAnalysis(), new String[0]); 674 } 675 676 public void testBrokenClassFile2() throws IOException { 677 Path broken = outDir.resolve("broken"); 678 compiler.compile(broken, 679 "package p;\n" + 680 "public class BrokenA {\n" + 681 "}", 682 "package p.q;\n" + 683 "public class BrokenB {\n" + 684 "}", 685 "package p;\n" + 686 "public class BrokenC {\n" + 687 "}"); 688 Path cp = compiler.getPath(broken); 689 Path target = cp.resolve("p").resolve("BrokenB.class"); 690 Files.deleteIfExists(target); 691 Files.move(cp.resolve("p").resolve("q").resolve("BrokenB.class"), target); 692 addToClasspath(cp); 693 694 assertEval("import p.*;"); 695 assertCompletion("Broke|", "BrokenA", "BrokenC"); 696 } 697} 698