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