Path: blob/master/test/langtools/jdk/jshell/CompletionSuggestionTest.java
40930 views
/*1* Copyright (c) 2015, 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 8131025 8141092 8153761 8145263 8131019 8175886 8176184 8176241 8176110 8177466 8197439 8221759 8234896 824065826* @summary Test Completion and Documentation27* @library /tools/lib28* @modules jdk.compiler/com.sun.tools.javac.api29* jdk.compiler/com.sun.tools.javac.main30* jdk.jdeps/com.sun.tools.javap31* jdk.jshell/jdk.jshell:open32* @build toolbox.ToolBox toolbox.JarTask toolbox.JavacTask33* @build KullaTesting TestingInputStream Compiler34* @run testng CompletionSuggestionTest35*/3637import 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;5051import jdk.jshell.Snippet;52import org.testng.annotations.BeforeMethod;53import org.testng.annotations.Test;5455import static jdk.jshell.Snippet.Status.VALID;56import static jdk.jshell.Snippet.Status.OVERWRITTEN;5758@Test59public class CompletionSuggestionTest extends KullaTesting {6061private final Compiler compiler = new Compiler();62private final Path outDir = Paths.get("completion_suggestion_test");6364public void testMemberExpr() {65assertEval("class Test { static void test() { } }");66assertCompletion("Test.t|", "test()");67assertEval("Test ccTestInstance = new Test();");68assertCompletion("ccTestInstance.t|", "toString()");69assertCompletion(" ccTe|", "ccTestInstance");70assertCompletion("String value = ccTestInstance.to|", "toString()");71assertCompletion("java.util.Coll|", "Collection", "Collections");72assertCompletion("String.cla|", "class");73assertCompletion("boolean.cla|", "class");74assertCompletion("byte.cla|", "class");75assertCompletion("short.cla|", "class");76assertCompletion("char.cla|", "class");77assertCompletion("int.cla|", "class");78assertCompletion("float.cla|", "class");79assertCompletion("long.cla|", "class");80assertCompletion("double.cla|", "class");81assertCompletion("void.cla|", "class");82assertCompletion("Object[].|", "class");83assertCompletion("int[].|", "class");84assertEval("Object[] ao = null;");85assertCompletion("int i = ao.|", "length");86assertEval("int[] ai = null;");87assertCompletion("int i = ai.|", "length");88assertCompletionIncludesExcludes("\"\".|",89new HashSet<>(Collections.emptyList()),90new HashSet<>(Arrays.asList("String(")));91assertEval("double d = 0;");92assertEval("void m() {}");93assertCompletionIncludesExcludes("d.|",94new HashSet<>(Collections.emptyList()),95new HashSet<>(Arrays.asList("class")));96assertCompletionIncludesExcludes("m().|",97new HashSet<>(Collections.emptyList()),98new HashSet<>(Arrays.asList("class")));99assertEval("class C {class D {} static class E {} enum F {} interface H {} void method() {} int number;}");100assertCompletionIncludesExcludes("C.|",101new HashSet<>(Arrays.asList("D", "E", "F", "H", "class")),102new HashSet<>(Arrays.asList("method()", "number")));103assertCompletionIncludesExcludes("new C().|",104new HashSet<>(Arrays.asList("method()", "number")),105new HashSet<>(Arrays.asList("D", "E", "F", "H", "class")));106assertCompletionIncludesExcludes("new C() {}.|",107new HashSet<>(Arrays.asList("method()", "number")),108new HashSet<>(Arrays.asList("D", "E", "F", "H", "class")));109assertCompletion("\"\".leng|", "length()");110assertCompletion("\"\"\"\n\"\"\".leng|", "length()");111}112113public void testStartOfExpression() {114assertEval("int ccTest = 0;");115assertCompletion("System.err.println(cc|", "ccTest");116assertCompletion("for (int i = cc|", "ccTest");117}118119public void testParameter() {120assertCompletion("class C{void method(int num){num|", "num");121}122123public void testPrimitive() {124Set<String> primitives = new HashSet<>(Arrays.asList("boolean", "char", "byte", "short", "int", "long", "float", "double"));125Set<String> onlyVoid = new HashSet<>(Collections.singletonList("void"));126Set<String> primitivesOrVoid = new HashSet<>(primitives);127primitivesOrVoid.addAll(onlyVoid);128129assertCompletionIncludesExcludes("|",130primitivesOrVoid,131new HashSet<>(Collections.emptyList()));132assertCompletionIncludesExcludes("int num = |",133primitivesOrVoid,134new HashSet<>(Collections.emptyList()));135assertCompletionIncludesExcludes("num = |",136primitivesOrVoid,137new HashSet<>(Collections.emptyList()));138assertCompletionIncludesExcludes("class C{void m() {|",139primitivesOrVoid,140new HashSet<>(Collections.emptyList()));141assertCompletionIncludesExcludes("void method(|",142primitives,143onlyVoid);144assertCompletionIncludesExcludes("void method(int num, |",145primitives,146onlyVoid);147assertCompletion("new java.util.ArrayList<doub|");148assertCompletion("class A extends doubl|");149assertCompletion("class A implements doubl|");150assertCompletion("interface A extends doubl|");151assertCompletion("enum A implements doubl|");152assertCompletion("class A<T extends doubl|");153}154155public void testEmpty() {156assertCompletionIncludesExcludes("|",157new HashSet<>(Arrays.asList("Object", "Void")),158new HashSet<>(Arrays.asList("$REPL00DOESNOTMATTER")));159assertCompletionIncludesExcludes("V|",160new HashSet<>(Collections.singletonList("Void")),161new HashSet<>(Collections.singletonList("Object")));162assertCompletionIncludesExcludes("{ |",163new HashSet<>(Arrays.asList("Object", "Void")),164new HashSet<>(Arrays.asList("$REPL00DOESNOTMATTER")));165}166167public void testSmartCompletion() {168assertEval("int ccTest1 = 0;");169assertEval("int ccTest2 = 0;");170assertEval("String ccTest3 = null;");171assertEval("void method(int i, String str) { }");172assertEval("void method(String str, int i) { }");173assertEval("java.util.List<String> list = null;");174assertCompletion("int ccTest4 = |", true, "ccTest1", "ccTest2");175assertCompletion("ccTest2 = |", true, "ccTest1", "ccTest2");176assertCompletion("int ccTest4 = ccTe|", "ccTest1", "ccTest2", "ccTest3");177assertCompletion("int ccTest4 = ccTest3.len|", true, "length()");178assertCompletion("method(|", true, "ccTest1", "ccTest2", "ccTest3");179assertCompletion("method(0, |", true, "ccTest3");180assertCompletion("list.add(|", true, "ccTest1", "ccTest2", "ccTest3");181assertCompletion("list.add(0, |", true, "ccTest3");182assertCompletion("new String(|", true, "ccTest3");183assertCompletion("new String(new char[0], |", true, "ccTest1", "ccTest2");184assertCompletionIncludesExcludes("new jav|", new HashSet<>(Arrays.asList("java.", "javax.")), Collections.emptySet());185assertCompletion("Class<String> clazz = String.c|", true, "class");186187Snippet klass = classKey(assertEval("class Klass {void method(int n) {} private void method(String str) {}}"));188assertCompletion("new Klass().method(|", true, "ccTest1", "ccTest2");189Snippet klass2 = classKey(assertEval("class Klass {static void method(int n) {} void method(String str) {}}",190ste(MAIN_SNIPPET, VALID, VALID, true, null),191ste(klass, VALID, OVERWRITTEN, false, MAIN_SNIPPET)));192assertCompletion("Klass.method(|", true, "ccTest1", "ccTest2");193assertEval("class Klass {Klass(int n) {} private Klass(String str) {}}",194ste(MAIN_SNIPPET, VALID, VALID, true, null),195ste(klass2, VALID, OVERWRITTEN, false, MAIN_SNIPPET));196assertCompletion("new Klass(|", true, "ccTest1", "ccTest2");197}198199public void testSmartCompletionInOverriddenMethodInvocation() {200assertEval("int ccTest1 = 0;");201assertEval("int ccTest2 = 0;");202assertEval("String ccTest3 = null;");203assertCompletion("\"\".wait(|", true, "ccTest1", "ccTest2");204assertEval("class Base {void method(int n) {}}");205assertEval("class Extend extends Base {}");206assertCompletion("new Extend().method(|", true, "ccTest1", "ccTest2");207}208209public void testSmartCompletionForBoxedType() {210assertEval("int ccTest1 = 0;");211assertEval("Integer ccTest2 = 0;");212assertEval("Object ccTest3 = null;");213assertEval("int method1(int n) {return n;}");214assertEval("Integer method2(Integer n) {return n;}");215assertEval("Object method3(Object o) {return o;}");216assertCompletion("int ccTest4 = |", true, "ccTest1", "ccTest2", "method1(", "method2(");217assertCompletion("Integer ccTest4 = |", true, "ccTest1", "ccTest2", "method1(", "method2(");218assertCompletion("Object ccTest4 = |", true, "ccTest1", "ccTest2", "ccTest3", "method1(", "method2(", "method3(");219assertCompletion("method1(|", true, "ccTest1", "ccTest2", "method1(", "method2(");220assertCompletion("method2(|", true, "ccTest1", "ccTest2", "method1(", "method2(");221assertCompletion("method3(|", true, "ccTest1", "ccTest2", "ccTest3", "method1(", "method2(", "method3(");222}223224public void testNewClass() {225assertCompletion("String str = new Strin|", "String(", "StringBuffer(", "StringBuilder(", "StringIndexOutOfBoundsException(");226assertCompletion("String str = new java.lang.Strin|", "String(", "StringBuffer(", "StringBuilder(", "StringIndexOutOfBoundsException(");227assertCompletion("String str = new |", true, "String(");228assertCompletion("String str = new java.lang.|", true, "String(");229assertCompletion("throw new Strin|", true, "StringIndexOutOfBoundsException(");230231assertEval("class A{class B{} class C {C(int n) {}} static class D {} interface I {}}");232assertEval("A a;");233assertCompletion("new A().new |", "B()", "C(");234assertCompletion("a.new |", "B()", "C(");235assertCompletion("new A.|", "D()");236237assertEval("enum E{; class A {}}");238assertEval("interface I{; class A {}}");239assertCompletion("new E.|", "A()");240assertCompletion("new I.|", "A()");241assertCompletion("new String(I.A|", "A");242}243244public void testFullyQualified() {245assertCompletion("Optional<String> opt = java.u|", "util.");246assertCompletionIncludesExcludes("Optional<Strings> opt = java.util.O|", new HashSet<>(Collections.singletonList("Optional")), Collections.emptySet());247248assertEval("void method(java.util.Optional<String> opt) {}");249assertCompletion("method(java.u|", "util.");250251assertCompletion("Object.notElement.|");252assertCompletion("Object o = com.su|", "sun.");253254Path p1 = outDir.resolve("dir1");255compiler.compile(p1,256"package p1.p2;\n" +257"public class Test {\n" +258"}",259"package p1.p3;\n" +260"public class Test {\n" +261"}");262String jarName = "test.jar";263compiler.jar(p1, jarName, "p1/p2/Test.class", "p1/p3/Test.class");264addToClasspath(compiler.getPath(p1.resolve(jarName)));265266assertCompletionIncludesExcludes("|", new HashSet<>(Collections.singletonList("p1.")), Collections.emptySet());267assertCompletion("p1.|", "p2.", "p3.");268assertCompletion("p1.p2.|", "Test");269assertCompletion("p1.p3.|", "Test");270}271272public void testCheckAccessibility() {273assertCompletion("java.util.regex.Pattern.co|", "compile(");274}275276public void testCompletePackages() {277assertCompletion("java.u|", "util.");278assertCompletionIncludesExcludes("jav|", new HashSet<>(Arrays.asList("java.", "javax.")), Collections.emptySet());279}280281public void testImports() {282assertCompletion("import java.u|", "util.");283assertCompletionIncludesExcludes("import jav|", new HashSet<>(Arrays.asList("java.", "javax.")), Collections.emptySet());284assertCompletion("import static java.u|", "util.");285assertCompletionIncludesExcludes("import static jav|", new HashSet<>(Arrays.asList("java.", "javax.")), Collections.emptySet());286assertCompletion("import static java.lang.Boolean.g|", "getBoolean");287assertCompletion("import java.util.*|");288assertCompletionIncludesExcludes("import java.lang.String.|",289Collections.emptySet(),290new HashSet<>(Arrays.asList("CASE_INSENSITIVE_ORDER", "copyValueOf", "format", "join", "valueOf", "class", "length")));291assertCompletionIncludesExcludes("import static java.lang.String.|",292new HashSet<>(Arrays.asList("CASE_INSENSITIVE_ORDER", "copyValueOf", "format", "join", "valueOf")),293new HashSet<>(Arrays.asList("class", "length")));294assertCompletionIncludesExcludes("import java.util.Map.|",295new HashSet<>(Arrays.asList("Entry")),296new HashSet<>(Arrays.asList("class")));297}298299public void testImportStart() {300assertCompletionIncludesExcludes("import c|", Set.of("com."), Set.of());301}302303public void testBrokenClassFile() throws Exception {304Compiler compiler = new Compiler();305Path testOutDir = Paths.get("CompletionTestBrokenClassFile");306String input = "package test.inner; public class Test {}";307compiler.compile(testOutDir, input);308addToClasspath(compiler.getPath(testOutDir).resolve("test"));309assertCompletion("import inner.|");310}311312public void testDocumentation() throws Exception {313dontReadParameterNamesFromClassFile();314assertSignature("System.getProperty(|",315"String System.getProperty(String key)",316"String System.getProperty(String key, String def)");317assertEval("char[] chars = null;");318assertSignature("new String(chars, |",319"String(char[], int, int)");320assertSignature("String.format(|",321"String String.format(String, Object...)",322"String String.format(java.util.Locale, String, Object...)");323assertSignature("\"\".getBytes(\"\"|", "void String.getBytes(int, int, byte[], int)",324"byte[] String.getBytes(String) throws java.io.UnsupportedEncodingException",325"byte[] String.getBytes(java.nio.charset.Charset)");326assertSignature("\"\".getBytes(\"\" |", "void String.getBytes(int, int, byte[], int)",327"byte[] String.getBytes(String) throws java.io.UnsupportedEncodingException",328"byte[] String.getBytes(java.nio.charset.Charset)");329//JDK-8221759:330Compiler compiler = new Compiler();331Path testOutDir = Paths.get("WithPrivateField");332String input = "package field; public class FieldTest { private static String field; private static String field2; }";333compiler.compile(testOutDir, input);334addToClasspath(compiler.getPath(testOutDir));335assertSignature("field.FieldTest.field|");336assertSignature("field.FieldTest.field2|");337}338339public void testMethodsWithNoArguments() throws Exception {340dontReadParameterNamesFromClassFile();341assertSignature("System.out.println(|",342"void java.io.PrintStream.println()",343"void java.io.PrintStream.println(boolean)",344"void java.io.PrintStream.println(char)",345"void java.io.PrintStream.println(int)",346"void java.io.PrintStream.println(long)",347"void java.io.PrintStream.println(float)",348"void java.io.PrintStream.println(double)",349"void java.io.PrintStream.println(char[])",350"void java.io.PrintStream.println(String)",351"void java.io.PrintStream.println(Object)");352}353354public void testErroneous() {355assertCompletion("Undefined.|");356assertSignature("does.not.exist|");357}358359public void testClinit() {360assertEval("enum E{;}");361assertEval("class C{static{}}");362assertCompletionIncludesExcludes("E.|", Collections.emptySet(), new HashSet<>(Collections.singletonList("<clinit>")));363assertCompletionIncludesExcludes("C.|", Collections.emptySet(), new HashSet<>(Collections.singletonList("<clinit>")));364}365366public void testMethodHeaderContext() {367assertCompletion("private void f(Runn|", "Runnable");368assertCompletion("void f(Runn|", "Runnable");369assertCompletion("void f(Object o1, Runn|", "Runnable");370assertCompletion("void f(Object o1) throws Num|", true, "NumberFormatException");371assertCompletion("void f(Object o1) throws java.lang.Num|", true, "NumberFormatException");372assertEval("class HogeHoge {static class HogeHogeException extends Exception {}}");373assertCompletion("void f(Object o1) throws Hoge|", "HogeHoge");374assertCompletion("void f(Object o1) throws HogeHoge.|", true, "HogeHogeException");375}376377public void testTypeVariables() {378assertCompletion("class A<TYPE> { public void test() { TY|", "TYPE");379assertCompletion("class A<TYPE> { public static void test() { TY|");380assertCompletion("class A<TYPE> { public <TYPE> void test() { TY|", "TYPE");381assertCompletion("class A<TYPE> { public static <TYPE> void test() { TY|", "TYPE");382}383384public void testGeneric() {385assertEval("import java.util.concurrent.*;");386assertCompletion("java.util.List<Integ|", "Integer");387assertCompletion("class A<TYPE extends Call|", "Callable");388assertCompletion("class A<TYPE extends Callable<TY|", "TYPE");389assertCompletion("<TYPE> void f(TY|", "TYPE");390assertCompletion("class A<TYPE extends Callable<? sup|", "super");391assertCompletion("class A<TYPE extends Callable<? super TY|", "TYPE");392}393394public void testFields() {395assertEval("interface Interface { int field = 0; }");396Snippet clazz = classKey(assertEval("class Clazz {" +397"static int staticField = 0;" +398"int field = 0;" +399" }"));400assertCompletion("Interface.fiel|", "field");401assertCompletion("Clazz.staticFiel|", "staticField");402assertCompletion("new Interface() {}.fiel|");403assertCompletion("new Clazz().staticFiel|");404assertCompletion("new Clazz().fiel|", "field");405assertCompletion("new Clazz() {}.fiel|", "field");406assertEval("class Clazz implements Interface {}",407ste(MAIN_SNIPPET, VALID, VALID, true, null),408ste(clazz, VALID, OVERWRITTEN, false, MAIN_SNIPPET));409assertCompletion("Clazz.fiel|", "field");410assertCompletion("new Clazz().fiel|");411assertCompletion("new Clazz() {}.fiel|");412}413414public void testMethods() {415assertEval("interface Interface {" +416"default int defaultMethod() { return 0; }" +417"static int staticMethod() { return 0; }" +418"}");419Snippet clazz = classKey(assertEval("class Clazz {" +420"static int staticMethod() { return 0; }" +421"int method() { return 0; }" +422"}"));423assertCompletion("Interface.staticMeth|", "staticMethod()");424assertCompletion("Clazz.staticMeth|", "staticMethod()");425assertCompletion("new Interface() {}.defaultMe||", "defaultMethod()");426assertCompletion("new Clazz().staticMeth|");427assertCompletion("new Clazz().meth|", "method()");428assertEval("class Clazz implements Interface {}",429ste(MAIN_SNIPPET, VALID, VALID, true, null),430ste(clazz, VALID, OVERWRITTEN, false, MAIN_SNIPPET));431assertCompletion("Clazz.staticMeth|");432assertCompletion("new Clazz() {}.defaultM|", "defaultMethod()");433}434435@Test436public void testUncompletedDeclaration() {437assertCompletion("class Clazz { Claz|", "Clazz");438assertCompletion("class Clazz { class A extends Claz|", "Clazz");439assertCompletion("class Clazz { Clazz clazz; Object o = claz|", "clazz");440assertCompletion("class Clazz { static Clazz clazz; Object o = claz|", "clazz");441assertCompletion("class Clazz { Clazz clazz; static Object o = claz|", true);442assertCompletion("class Clazz { void method(Claz|", "Clazz");443assertCompletion("class A { int method() { return 0; } int a = meth|", "method()");444assertCompletion("class A { int field = 0; int method() { return fiel|", "field");445assertCompletion("class A { static int method() { return 0; } int a = meth|", "method()");446assertCompletion("class A { static int field = 0; int method() { return fiel|", "field");447assertCompletion("class A { int method() { return 0; } static int a = meth|", true);448assertCompletion("class A { int field = 0; static int method() { return fiel|", true);449}450451@Test452public void testClassDeclaration() {453assertEval("void ClazzM() {}");454assertEval("void InterfaceM() {}");455assertEval("interface Interface {}");456assertCompletion("interface A extends Interf|", "Interface");457assertCompletion("class A implements Interf|", "Interface");458assertEval("class Clazz {}");459assertCompletion("class A extends Claz|", "Clazz");460assertCompletion("class A extends Clazz implements Interf|", "Interface");461assertEval("interface Interface1 {}");462assertCompletion("class A extends Clazz implements Interface, Interf|", "Interface", "Interface1");463assertCompletion("interface A implements Claz|");464assertCompletion("interface A implements Inter|");465assertCompletion("class A implements Claz|", true);466assertCompletion("class A extends Clazz implements Interface, Interf|", true, "Interface1");467assertCompletion("class A extends Clazz implements Interface, Interf|", true, "Interface1");468assertEval("class InterfaceClazz {}");469assertCompletion("class A <T extends Claz|", "Clazz");470assertCompletion("class A <T extends Interf|", "Interface", "Interface1", "InterfaceClazz");471assertCompletion("class A <T extends Interface & Interf|", "Interface", "Interface1", "InterfaceClazz");472assertCompletion("class A <T extends Clazz & Interf|", "Interface", "Interface1", "InterfaceClazz");473assertCompletion("class A <T extends Claz|", true, "Clazz");474assertCompletion("class A <T extends Interf|", true, "Interface", "Interface1", "InterfaceClazz");475assertCompletion("class A <T extends Interface & Interf|", true, "Interface1");476assertCompletion("class A <T extends Clazz & Interf|", true, "Interface", "Interface1");477}478479public void testMethodDeclaration() {480assertEval("void ClazzM() {}");481assertEval("void InterfaceM() {}");482assertEval("interface Interface {}");483assertCompletion("void m(Interf|", "Interface");484assertCompletion("void m(Interface i1, Interf|", "Interface");485assertEval("class InterfaceException extends Exception {}");486assertCompletion("void m(Interface i1) throws Interf|", "Interface", "InterfaceException");487assertCompletion("void m(Interface i1) throws Interf|", true, "InterfaceException");488}489490public void testDocumentationOfUserDefinedMethods() {491assertEval("void f() {}");492assertSignature("f(|", "void f()");493assertEval("void f(int i) {}");494assertSignature("f(|", "void f()", "void f(int i)");495assertEval("<T> void f(T... ts) {}", DiagCheck.DIAG_WARNING, DiagCheck.DIAG_OK);496assertSignature("f(|", "void f()", "void f(int i)", "void <T>f(T... ts)");497assertEval("class A {}");498assertEval("void f(A a) {}");499assertSignature("f(|", "void f()", "void f(int i)", "void <T>f(T... ts)", "void f(A a)");500}501502public void testClass() {503assertSignature("String|", "java.lang.String");504}505506public void testDocumentationOfUserDefinedConstructors() {507Snippet a = classKey(assertEval("class A {}"));508assertSignature("new A(|", "A()");509Snippet a2 = classKey(assertEval("class A { A() {} A(int i) {}}",510ste(MAIN_SNIPPET, VALID, VALID, true, null),511ste(a, VALID, OVERWRITTEN, false, MAIN_SNIPPET)));512assertSignature("new A(|", "A()", "A(int i)");513assertEval("class A<T> { A(T a) {} A(int i) {} <U> A(T t, U u) {}}",514ste(MAIN_SNIPPET, VALID, VALID, true, null),515ste(a2, VALID, OVERWRITTEN, false, MAIN_SNIPPET));516assertSignature("new A(|", "A<T>(T a)", "A<T>(int i)", "<U> A<T>(T t, U u)");517}518519public void testDocumentationOfOverriddenMethods() throws Exception {520dontReadParameterNamesFromClassFile();521assertSignature("\"\".wait(|",522"void Object.wait(long) throws InterruptedException",523"void Object.wait(long, int) throws InterruptedException",524"void Object.wait() throws InterruptedException");525assertEval("class Base {void method() {}}");526Snippet e = classKey(assertEval("class Extend extends Base {}"));527assertSignature("new Extend().method(|", "void Base.method()");528assertEval("class Extend extends Base {void method() {}}",529ste(MAIN_SNIPPET, VALID, VALID, true, null),530ste(e, VALID, OVERWRITTEN, false, MAIN_SNIPPET));531assertSignature("new Extend().method(|", "void Extend.method()");532}533534public void testDocumentationOfInvisibleMethods() {535assertSignature("Object.wait(|");536assertSignature("\"\".indexOfSupplementary(|");537Snippet a = classKey(assertEval("class A {void method() {}}"));538assertSignature("A.method(|");539assertEval("class A {private void method() {}}",540ste(MAIN_SNIPPET, VALID, VALID, true, null),541ste(a, VALID, OVERWRITTEN, false, MAIN_SNIPPET));542assertSignature("new A().method(|");543}544545public void testDocumentationOfInvisibleConstructors() {546assertSignature("new Compiler(|");547assertEval("class A { private A() {} }");548assertSignature("new A(|");549}550551public void testDocumentationWithBoxing() {552assertEval("int primitive = 0;");553assertEval("Integer boxed = 0;");554assertEval("Object object = null;");555assertEval("void method(int n, Object o) { }");556assertEval("void method(Object n, int o) { }");557assertSignature("method(primitive,|",558"void method(int n, Object o)",559"void method(Object n, int o)");560assertSignature("method(boxed,|",561"void method(int n, Object o)",562"void method(Object n, int o)");563assertSignature("method(object,|",564"void method(Object n, int o)");565}566567public void testDocumentationWithGenerics() {568class TestDocumentationWithGenerics {569private final Function<Integer, String> codeFacotry;570private final BiFunction<String, Integer, String> evalFormatter;571private final BiFunction<String, Integer, String> docFormatter;572int count;573574TestDocumentationWithGenerics(575Function<Integer, String> codeFactory,576BiFunction<String, Integer, String> evalFormatter,577BiFunction<String, Integer, String> documentationFormatter) {578this.codeFacotry = codeFactory;579this.evalFormatter = evalFormatter;580this.docFormatter = documentationFormatter;581}582583void assertDoc(String generics) {584assertDoc(generics, generics);585}586587void assertDoc(String generics, String expectedGenerics) {588assertEval(evalFormatter.apply(generics, count));589assertSignature(codeFacotry.apply(count), docFormatter.apply(expectedGenerics, count));590count++;591}592}593594TestDocumentationWithGenerics[] tests = {595new TestDocumentationWithGenerics(596i -> "f" + i + "(|",597(g, i) -> "<" + g + "> void f" + i + "() {}",598(g, i) -> "void <" + g + ">f" + i + "()"599),600new TestDocumentationWithGenerics(601i -> "new C" + i + "().f(|",602(g, i) -> "class C" + i + "<" + g + "> { void f() {} }",603(g, i) -> "void C" + i + "<" + g + ">.f()"604)605};606607Arrays.stream(tests).forEach(t -> {608t.assertDoc("T");609t.assertDoc("T extends Object",610"T");611t.assertDoc("T extends String");612t.assertDoc("T extends java.lang.String",613"T extends String");614t.assertDoc("T extends Number & Comparable<T>");615t.assertDoc("T extends java.io.Serializable & CharSequence");616t.assertDoc("K, D, M extends java.util.Map<K, D>",617"K, D, M extends java.util.Map<K,D>");618});619}620621public void testVarArgs() {622assertEval("int i = 0;");623assertEval("class Foo1 { static void m(int... i) { } } ");624assertCompletion("Foo1.m(|", true, "i");625assertCompletion("Foo1.m(i, |", true, "i");626assertCompletion("Foo1.m(i, i, |", true, "i");627assertEval("class Foo2 { static void m(String s, int... i) { } } ");628assertCompletion("Foo2.m(|", true);629assertCompletion("Foo2.m(i, |", true);630assertCompletion("Foo2.m(\"\", |", true, "i");631assertCompletion("Foo2.m(\"\", i, |", true, "i");632assertCompletion("Foo2.m(\"\", i, i, |", true, "i");633assertEval("class Foo3 { Foo3(String s, int... i) { } } ");634assertCompletion("new Foo3(|", true);635assertCompletion("new Foo3(i, |", true);636assertCompletion("new Foo3(\"\", |", true, "i");637assertCompletion("new Foo3(\"\", i, |", true, "i");638assertCompletion("new Foo3(\"\", i, i, |", true, "i");639assertEval("int[] ia = null;");640assertCompletion("Foo1.m(ia, |", true);641assertEval("class Foo4 { static void m(int... i) { } static void m(int[] ia, String str) { } } ");642assertEval("String str = null;");643assertCompletion("Foo4.m(ia, |", true, "str");644}645646public void testConstructorAsMemberOf() {647assertEval("class Baz<X> { Baz(X x) { } } ");648assertEval("String str = null;");649assertEval("Integer i = null;");650assertCompletion("new Baz(|", true, "i", "str");651assertCompletion("new Baz<String>(|", true, "str");652assertCompletion("Baz<String> bz = new Baz<>(|", true, "str");653assertEval("class Foo { static void m(String str) {} static void m(Baz<String> baz) {} }");654assertCompletion("Foo.m(new Baz<>(|", true, "str");655}656657public void testIntersection() {658assertEval("<Z extends Runnable & CharSequence> Z get() { return null; }");659assertEval("var v = get();");660assertCompletionIncludesExcludes("v.|", true, Set.of("run()", "length()"), Set.of());661assertCompletion("Runnable r = |", true, "get()", "v");662assertCompletion("CharSequence r = |", true, "get()", "v");663assertCompletion("Number r = |", true);664}665666public void testAnonymous() {667assertEval("var v = new Runnable() { public void run() { } public int length() { return 0; } };");668assertCompletionIncludesExcludes("v.|", true, Set.of("run()", "length()"), Set.of());669assertCompletion("Runnable r = |", true, "v");670assertCompletion("CharSequence r = |", true);671}672673public void testCompletionInAnonymous() {674assertCompletionIncludesExcludes("new Undefined() { int i = \"\".l|", Set.of("length()"), Set.of());675}676677public void testMemberReferences() {678assertEval("class C {" +679" public static String stat() { return null; }" +680" public static void statVoid(String s) {}" +681" public static Integer statConvert1(String s) { return null; }" +682" public static String statConvert2(Integer s) { return null; }" +683" public static String statConvert3(CharSequence s) { return null; }" +684" public String inst() { return null; }" +685" public void instVoid(String s) { }" +686"}");687assertEval("interface FI { public void t(String s); }");688assertCompletion("FI fi = C::|", (Boolean) null, "stat", "statConvert1", "statConvert2", "statConvert3", "statVoid");689assertCompletion("FI fi = C::|", true, "statConvert1", "statConvert3","statVoid");690assertCompletion("FI fi = new C()::i|", (Boolean) null, "inst", "instVoid");691assertCompletion("FI fi = new C()::i|", true, "instVoid");692assertEval("interface FI2<R, P> { public R t(P p); }");693assertCompletion("FI2<String, Integer> fi = C::|", (Boolean) null, "stat", "statConvert1", "statConvert2", "statConvert3", "statVoid");694assertCompletion("FI2<String, Integer> fi = C::|", true, "statConvert2");695assertCompletion("FI2<String, CharSequence> fi = C::|", true, "statConvert3");696assertCompletion("FI2<String, String> fi = C::|", true, "statConvert3");697assertCompletion("FI2<Object, String> fi = C::|", true, "statConvert1", "statConvert3");698}699700public void testBrokenLambdaCompletion() {701assertEval("interface Consumer<T> { public void consume(T t); }");702assertEval("interface Function<T, R> { public R convert(T t); }");703assertEval("<T> void m1(T t, Consumer<T> f) { }");704assertCompletion("m1(\"\", x -> {x.tri|", "trim()");705assertEval("<T> void m2(T t, Function<T, String> f) { }");706assertCompletion("m2(\"\", x -> {x.tri|", "trim()");707assertEval("<T> void m3(T t, Consumer<T> f, int i) { }");708assertCompletion("m3(\"\", x -> {x.tri|", "trim()");709assertEval("<T> void m4(T t, Function<T, String> f, int i) { }");710assertCompletion("m4(\"\", x -> {x.tri|", "trim()");711assertEval("<T> T m5(Consumer<T> f) { return null; }");712assertCompletion("String s = m5(x -> {x.tri|", "trim()");713assertEval("<T> T m6(Function<T, String> f) { return null; }");714assertCompletion("String s = m6(x -> {x.tri|", "trim()");715assertEval("<T> T m7(Consumer<T> f, int i) { return null; }");716assertCompletion("String s = m7(x -> {x.tri|", "trim()");717assertEval("<T> T m8(Function<T, String> f, int i) { return null; }");718assertCompletion("String s = m8(x -> {x.tri|", "trim()");719}720721@BeforeMethod722public void setUp() {723super.setUp();724725Path srcZip = Paths.get("src.zip");726727try (JarOutputStream out = new JarOutputStream(Files.newOutputStream(srcZip))) {728out.putNextEntry(new JarEntry("java/lang/System.java"));729out.write(("package java.lang;\n" +730"public class System {\n" +731" public String getProperty(String key) { return null; }\n" +732" public String getProperty(String key, String def) { return def; }\n" +733"}\n").getBytes());734} catch (IOException ex) {735throw new IllegalStateException(ex);736}737738try {739Field availableSources = getAnalysis().getClass().getDeclaredField("availableSources");740availableSources.setAccessible(true);741availableSources.set(getAnalysis(), Arrays.asList(srcZip));742} catch (NoSuchFieldException | IllegalArgumentException | IllegalAccessException ex) {743throw new IllegalStateException(ex);744}745}746747private void dontReadParameterNamesFromClassFile() throws Exception {748Field keepParameterNames = getAnalysis().getClass().getDeclaredField("keepParameterNames");749keepParameterNames.setAccessible(true);750keepParameterNames.set(getAnalysis(), new String[0]);751}752753@Test(enabled = false) //TODO 8171829754public void testBrokenClassFile2() throws IOException {755Path broken = outDir.resolve("broken");756compiler.compile(broken,757"package p;\n" +758"public class BrokenA {\n" +759"}",760"package p.q;\n" +761"public class BrokenB {\n" +762"}",763"package p;\n" +764"public class BrokenC {\n" +765"}");766Path cp = compiler.getPath(broken);767Path target = cp.resolve("p").resolve("BrokenB.class");768Files.deleteIfExists(target);769Files.move(cp.resolve("p").resolve("q").resolve("BrokenB.class"), target);770addToClasspath(cp);771772assertEval("import p.*;");773assertCompletion("Broke|", "BrokenA", "BrokenC");774}775}776777778