This commit is contained in:
Alexey Kudravtsev
2018-07-22 14:20:49 +03:00
parent cee1d7173f
commit 90681c922b
41 changed files with 357 additions and 296 deletions
@@ -33,11 +33,6 @@ public class AnnotateMethodTest extends LightQuickFixTestCase {
return new LocalInspectionTool[]{new NullableStuffInspection()};
}
@Override
protected boolean shouldBeAvailableAfterExecution() {
return false;
}
public void test() { doAllTests(); }
@Override
@@ -29,27 +29,30 @@ import com.intellij.testFramework.PlatformTestUtil;
import com.intellij.testFramework.fixtures.CodeInsightTestUtil;
import com.intellij.testFramework.fixtures.JavaCodeInsightFixtureTestCase;
import com.intellij.util.containers.ContainerUtil;
import org.intellij.lang.annotations.Language;
import java.util.Arrays;
import java.util.List;
public class GotoImplementationHandlerTest extends JavaCodeInsightFixtureTestCase {
public void testMultipleImplsFromAbstractCall() {
PsiFile file = myFixture.addFileToProject("Foo.java", "public abstract class Hello {\n" +
" abstract void foo();\n" +
"\n" +
" class A {\n" +
" {\n" +
" fo<caret>o();\n" +
" }\n" +
" }\n" +
" class Hello1 extends Hello {\n" +
" void foo() {}\n" +
" }\n" +
" class Hello2 extends Hello {\n" +
" void foo() {}\n" +
" }\n" +
"}");
@Language("JAVA")
String fileText = "public abstract class Hello {\n" +
" abstract void foo();\n" +
"\n" +
" class A {\n" +
" {\n" +
" fo<caret>o();\n" +
" }\n" +
" }\n" +
" class Hello1 extends Hello {\n" +
" void foo() {}\n" +
" }\n" +
" class Hello2 extends Hello {\n" +
" void foo() {}\n" +
" }\n" +
"}";
PsiFile file = myFixture.addFileToProject("Foo.java", fileText);
myFixture.configureFromExistingVirtualFile(file.getVirtualFile());
final PsiElement[] impls = getTargets(file);
@@ -57,21 +60,23 @@ public class GotoImplementationHandlerTest extends JavaCodeInsightFixtureTestCas
}
public void testFromIncompleteCode() {
PsiFile file = myFixture.addFileToProject("Foo.java", "public abstract class Hello {\n" +
" abstract void foo();\n" +
"\n" +
" class A {\n" +
" {\n" +
" Hello<caret>\n" +
" }\n" +
" }\n" +
" class Hello1 extends Hello {\n" +
" void foo() {}\n" +
" }\n" +
"}" +
"class Hello2 extends Hello {\n" +
" void foo() {}\n" +
"}\n");
@Language("JAVA")
String fileText = "public abstract class Hello {\n" +
" abstract void foo();\n" +
"\n" +
" class A {\n" +
" {\n" +
" Hello<caret>\n" +
" }\n" +
" }\n" +
" class Hello1 extends Hello {\n" +
" void foo() {}\n" +
" }\n" +
"}" +
"class Hello2 extends Hello {\n" +
" void foo() {}\n" +
"}\n";
PsiFile file = myFixture.addFileToProject("Foo.java", fileText);
myFixture.configureFromExistingVirtualFile(file.getVirtualFile());
final PsiElement[] impls = getTargets(file);
@@ -79,23 +84,25 @@ public class GotoImplementationHandlerTest extends JavaCodeInsightFixtureTestCas
}
public void testToStringOnUnqualified() {
final PsiFile file = myFixture.addFileToProject("Foo.java", "public class Fix {\n" +
" {\n" +
" <caret>toString();\n" +
" }\n" +
"}\n" +
"class FixImpl1 extends Fix {\n" +
" @Override\n" +
" public String toString() {\n" +
" return \"Impl1\";\n" +
" }\n" +
"}\n" +
"class FixImpl2 extends Fix {\n" +
" @Override\n" +
" public String toString() {\n" +
" return \"Impl2\";\n" +
" }\n" +
"}");
@Language("JAVA")
String fileText = "public class Fix {\n" +
" {\n" +
" <caret>toString();\n" +
" }\n" +
"}\n" +
"class FixImpl1 extends Fix {\n" +
" @Override\n" +
" public String toString() {\n" +
" return \"Impl1\";\n" +
" }\n" +
"}\n" +
"class FixImpl2 extends Fix {\n" +
" @Override\n" +
" public String toString() {\n" +
" return \"Impl2\";\n" +
" }\n" +
"}";
final PsiFile file = myFixture.addFileToProject("Foo.java", fileText);
myFixture.configureFromExistingVirtualFile(file.getVirtualFile());
PlatformTestUtil.startPerformanceTest(getTestName(false), 150, () -> {
@@ -105,26 +112,28 @@ public class GotoImplementationHandlerTest extends JavaCodeInsightFixtureTestCas
}
public void testToStringOnQualified() {
final PsiFile file = myFixture.addFileToProject("Foo.java", "public class Fix {\n" +
" {\n" +
" Fix ff = getFix();\n" +
" ff.<caret>toString();\n" +
" }\n" +
" \n" +
" Fix getFix() {return new FixImpl1();}\n" +
"}\n" +
"class FixImpl1 extends Fix {\n" +
" @Override\n" +
" public String toString() {\n" +
" return \"Impl1\";\n" +
" }\n" +
"}\n" +
"class FixImpl2 extends Fix {\n" +
" @Override\n" +
" public String toString() {\n" +
" return \"Impl2\";\n" +
" }\n" +
"}");
@Language("JAVA")
String fileText = "public class Fix {\n" +
" {\n" +
" Fix ff = getFix();\n" +
" ff.<caret>toString();\n" +
" }\n" +
" \n" +
" Fix getFix() {return new FixImpl1();}\n" +
"}\n" +
"class FixImpl1 extends Fix {\n" +
" @Override\n" +
" public String toString() {\n" +
" return \"Impl1\";\n" +
" }\n" +
"}\n" +
"class FixImpl2 extends Fix {\n" +
" @Override\n" +
" public String toString() {\n" +
" return \"Impl2\";\n" +
" }\n" +
"}";
final PsiFile file = myFixture.addFileToProject("Foo.java", fileText);
myFixture.configureFromExistingVirtualFile(file.getVirtualFile());
PlatformTestUtil.startPerformanceTest(getTestName(false), 150, () -> {
@@ -135,21 +144,23 @@ public class GotoImplementationHandlerTest extends JavaCodeInsightFixtureTestCas
public void testShowSelfNonAbstract() {
//fails if groovy plugin is enabled: org.jetbrains.plugins.groovy.codeInsight.JavaClsMethodElementEvaluator
PsiFile file = myFixture.addFileToProject("Foo.java", "public class Hello {\n" +
" void foo(){}\n" +
"\n" +
" class A {\n" +
" {\n" +
" fo<caret>o();\n" +
" }\n" +
" }\n" +
" class Hello1 extends Hello {\n" +
" void foo() {}\n" +
" }\n" +
" class Hello2 extends Hello {\n" +
" void foo() {}\n" +
" }\n" +
"}");
@Language("JAVA")
String fileText = "public class Hello {\n" +
" void foo(){}\n" +
"\n" +
" class A {\n" +
" {\n" +
" fo<caret>o();\n" +
" }\n" +
" }\n" +
" class Hello1 extends Hello {\n" +
" void foo() {}\n" +
" }\n" +
" class Hello2 extends Hello {\n" +
" void foo() {}\n" +
" }\n" +
"}";
PsiFile file = myFixture.addFileToProject("Foo.java", fileText);
myFixture.configureFromExistingVirtualFile(file.getVirtualFile());
final PsiElement[] impls = getTargets(file);
@@ -157,17 +168,19 @@ public class GotoImplementationHandlerTest extends JavaCodeInsightFixtureTestCas
}
public void testMultipleImplsFromStaticCall() {
PsiFile file = myFixture.addFileToProject("Foo.java", "public abstract class Hello {\n" +
" static void bar (){}\n" +
" class Hello1 extends Hello {\n" +
" }\n" +
" class Hello2 extends Hello {\n" +
" }\n" +
"class D {\n" +
" {\n" +
" He<caret>llo.bar();\n" +
" }\n" +
"}");
@Language("JAVA")
String fileText = "public abstract class Hello {\n" +
" static void bar (){}\n" +
" class Hello1 extends Hello {\n" +
" }\n" +
" class Hello2 extends Hello {\n" +
" }\n" +
"class D {\n" +
" {\n" +
" He<caret>llo.bar();\n" +
" }\n" +
"}}";
PsiFile file = myFixture.addFileToProject("Foo.java", fileText);
myFixture.configureFromExistingVirtualFile(file.getVirtualFile());
final PsiElement[] impls = getTargets(file);
@@ -175,25 +188,27 @@ public class GotoImplementationHandlerTest extends JavaCodeInsightFixtureTestCas
}
public void testFilterOutImpossibleVariants() {
PsiFile file = myFixture.addFileToProject("Foo.java", "interface A {\n" +
" void save();\n" +
"}\n" +
"interface B extends A {\n" +
" void foo();\n" +
"}\n" +
"class X implements B {\n" +
" public void foo() { }\n" +
" public void save(){}\n" +
"}\n" +
"class Y implements A {\n" +
" public void save(){}\n" +
"}\n" +
"class App {\n" +
" private B b;\n" +
" private void some() {\n" +
" b.sa<caret>ve();\n" +
" }\n" +
"}");
@Language("JAVA")
String fileText = "interface A {\n" +
" void save();\n" +
"}\n" +
"interface B extends A {\n" +
" void foo();\n" +
"}\n" +
"class X implements B {\n" +
" public void foo() { }\n" +
" public void save(){}\n" +
"}\n" +
"class Y implements A {\n" +
" public void save(){}\n" +
"}\n" +
"class App {\n" +
" private B b;\n" +
" private void some() {\n" +
" b.sa<caret>ve();\n" +
" }\n" +
"}";
PsiFile file = myFixture.addFileToProject("Foo.java", fileText);
myFixture.configureFromExistingVirtualFile(file.getVirtualFile());
final PsiElement[] impls = getTargets(file);
@@ -206,22 +221,24 @@ public class GotoImplementationHandlerTest extends JavaCodeInsightFixtureTestCas
}
public void testImplicitInheritance() {
PsiFile file = myFixture.addFileToProject("Foo.java", "interface PackContainer {\n" +
" void foo();\n" +
"}\n" +
"interface PsiPackage extends PackContainer {}\n" +
"class PsiPackageBase implements PackContainer {\n" +
" public void foo() {}\n" +
"}\n" +
"class PsiPackageImpl extends PsiPackageBase implements PsiPackage {}\n" +
"\n" +
"class Foo {\n" +
" class Bar {\n" +
" void bar(PsiPackage i) {\n" +
" i.fo<caret>o();\n" +
" }\n" +
" }\n" +
"}");
@Language("JAVA")
String fileText = "interface PackContainer {\n" +
" void foo();\n" +
"}\n" +
"interface PsiPackage extends PackContainer {}\n" +
"class PsiPackageBase implements PackContainer {\n" +
" public void foo() {}\n" +
"}\n" +
"class PsiPackageImpl extends PsiPackageBase implements PsiPackage {}\n" +
"\n" +
"class Foo {\n" +
" class Bar {\n" +
" void bar(PsiPackage i) {\n" +
" i.fo<caret>o();\n" +
" }\n" +
" }\n" +
"}";
PsiFile file = myFixture.addFileToProject("Foo.java", fileText);
myFixture.configureFromExistingVirtualFile(file.getVirtualFile());
final PsiElement[] impls = getTargets(file);
@@ -234,12 +251,14 @@ public class GotoImplementationHandlerTest extends JavaCodeInsightFixtureTestCas
}
public void testMethodReferences() {
PsiFile file = myFixture.addFileToProject("Foo.java", "interface I {void f();}\n" +
"class A implements I { public void f(){}}\n" +
"class B implements I { public void f(){}}\n" +
"class C {\n" +
" void foo(java.util.List<I> l) {l.stream().forEach(I::<caret>f);}" +
"}");
@Language("JAVA")
String fileText = "interface I {void f();}\n" +
"class A implements I { public void f(){}}\n" +
"class B implements I { public void f(){}}\n" +
"class C {\n" +
" void foo(java.util.List<I> l) {l.stream().forEach(I::<caret>f);}" +
"}";
PsiFile file = myFixture.addFileToProject("Foo.java", fileText);
myFixture.configureFromExistingVirtualFile(file.getVirtualFile());
final PsiElement[] impls = getTargets(file);
@@ -258,32 +277,36 @@ public class GotoImplementationHandlerTest extends JavaCodeInsightFixtureTestCas
}
public void testMethodImplementationsOnTypeVariable() {
PsiFile file = myFixture.addFileToProject("Foo.java", "interface I {}\n" +
"interface Im {\n" +
" void m();\n" +
"}\n" +
"class Im1 implements Im {\n" +
" public void m() {}\n" +
"}\n" +
"class Im2 implements Im {\n" +
" public void m() {}\n" +
"}\n" +
"class JavaClass<T extends K, K extends I & Im> {\n" +
" void a(T t){\n" +
" t.<caret>m();\n" +
" }\n" +
"}");
@Language("JAVA")
String fileText = "interface I {}\n" +
"interface Im {\n" +
" void m();\n" +
"}\n" +
"class Im1 implements Im {\n" +
" public void m() {}\n" +
"}\n" +
"class Im2 implements Im {\n" +
" public void m() {}\n" +
"}\n" +
"class JavaClass<T extends K, K extends I & Im> {\n" +
" void a(T t){\n" +
" t.<caret>m();\n" +
" }\n" +
"}";
PsiFile file = myFixture.addFileToProject("Foo.java", fileText);
myFixture.configureFromExistingVirtualFile(file.getVirtualFile());
PsiElement[] targets = getTargets(file);
assertSize(2, targets);
}
public void testStaticMethodReference() {
@Language("JAVA")
String fileText = "class C {\n" +
" static void a(){}\n" +
" {a<caret>();}" +
"}";
PsiFile file = myFixture.addFileToProject("Foo.java",
"class C {\n" +
" static void a(){}\n" +
" {a<caret>();}" +
"}");
fileText);
myFixture.configureFromExistingVirtualFile(file.getVirtualFile());
final PsiElement[] impls = getTargets(file);
@@ -291,11 +314,13 @@ public class GotoImplementationHandlerTest extends JavaCodeInsightFixtureTestCas
}
public void testPrivateClassInheritors() {
@Language("JAVA")
String fileText = "class C {\n" +
" private static class Pr<caret>ivate {}\n" +
" public static class Public extends Private {}" +
"}";
PsiFile file = myFixture.addFileToProject("Foo.java",
"class C {\n" +
" private static class Pr<caret>ivate {}\n" +
" public static class Public extends Private {}" +
"}");
fileText);
myFixture.addClass("class Inheritor extends C.Public {}");
myFixture.configureFromExistingVirtualFile(file.getVirtualFile());
@@ -314,10 +339,12 @@ public class GotoImplementationHandlerTest extends JavaCodeInsightFixtureTestCas
}
public void testScopeForPrivateMethod() {
PsiFile file = myFixture.configureByText(JavaFileType.INSTANCE, "class Foo {" +
" {f<caret>oo();}" +
" private void foo() {}" +
"}");
@Language("JAVA")
String text = "class Foo {" +
" {f<caret>oo();}" +
" private void foo() {}" +
"}";
PsiFile file = myFixture.configureByText(JavaFileType.INSTANCE, text);
PsiClass inheritor = myFixture.addClass("class FooImpl extends Foo {}");
SearchScope scope = TargetElementUtil.getInstance().getSearchScope(myFixture.getEditor(), ((PsiJavaFile)file).getClasses()[0].getMethods()[0]);
assertFalse(scope.contains(PsiUtilCore.getVirtualFile(inheritor)));
@@ -328,13 +355,15 @@ public class GotoImplementationHandlerTest extends JavaCodeInsightFixtureTestCas
myModule,
"jar://" + JavaTestUtil.getJavaTestDataPath() + "/codeInsight/navigation/MyInterfaceLibrary.jar!/"
);
@Language("JAVA")
String fileText = "import com.company.MyInterface;\n" +
"\n" +
"public class MyInterfaceImplementation implements My<caret>Interface {\n" +
" @Override\n" +
" public void doIt() {}\n" +
"}\n";
PsiFile psiFile = myFixture.addFileToProject("MyInterfaceImplementation.java",
"import com.company.MyInterface;\n" +
"\n" +
"public class MyInterfaceImplementation implements My<caret>Interface {\n" +
" @Override\n" +
" public void doIt() {}\n" +
"}\n");
fileText);
myFixture.configureFromExistingVirtualFile(psiFile.getVirtualFile());
@@ -67,23 +67,23 @@ public class GotoImplementationTest extends CodeInsightTestCase {
Module module1 = moduleManager.findModuleByName("test1");
Module module2 = moduleManager.findModuleByName("test2");
Module module3 = moduleManager.findModuleByName("test3");
final GlobalSearchScope moduleScope = GlobalSearchScope.moduleScope(module1);
PsiClass test1 = myJavaFacade.findClass("com.test.TestI", moduleScope);
final GlobalSearchScope module1Scope = GlobalSearchScope.moduleScope(module1);
PsiClass test1 = myJavaFacade.findClass("com.test.TestI", module1Scope);
PsiClass test2 = myJavaFacade.findClass("com.test.TestI", GlobalSearchScope.moduleScope(module2));
PsiClass test3 = myJavaFacade.findClass("com.test.TestI", GlobalSearchScope.moduleScope(module3));
HashSet<PsiElement> expectedImpls1 = new HashSet<>(Arrays.asList(
myJavaFacade.findClass("com.test.TestIImpl1", moduleScope),
myJavaFacade.findClass("com.test.TestIImpl2", moduleScope)
myJavaFacade.findClass("com.test.TestIImpl1", module1Scope),
myJavaFacade.findClass("com.test.TestIImpl2", module1Scope)
));
assertEquals(expectedImpls1, new HashSet<>(getClassImplementations(test1)));
PsiMethod psiMethod = test1.findMethodsByName("test", false)[0];
Set<PsiElement> expectedMethodImpl1 = new HashSet<>(Arrays.asList(
myJavaFacade.findClass("com.test.TestIImpl1", moduleScope).findMethodsByName("test", false)[0],
myJavaFacade.findClass("com.test.TestIImpl2", moduleScope).findMethodsByName("test", false)[0]
myJavaFacade.findClass("com.test.TestIImpl1", module1Scope).findMethodsByName("test", false)[0],
myJavaFacade.findClass("com.test.TestIImpl2", module1Scope).findMethodsByName("test", false)[0]
));
CommonProcessors.CollectProcessor<PsiElement> processor = new CommonProcessors.CollectProcessor<>();
MethodImplementationsSearch.processImplementations(psiMethod, processor, moduleScope);
MethodImplementationsSearch.processImplementations(psiMethod, processor, module1Scope);
assertEquals(expectedMethodImpl1, new HashSet<>(processor.getResults()));
HashSet<PsiElement> expectedImpls2 = new HashSet<>(Arrays.asList(