Java Resolver: Substitute original element when resolving PsiMethod to FunctionDescriptor

#KT-7353 Fixed
This commit is contained in:
Alexey Sedunov
2015-05-22 15:08:52 +03:00
parent babb7e58bc
commit b2201026d1
18 changed files with 214 additions and 5 deletions
@@ -33,6 +33,7 @@ import org.jetbrains.kotlin.codegen.flags.AbstractWriteFlagsTest
import org.jetbrains.kotlin.codegen.generated.AbstractBlackBoxCodegenTest
import org.jetbrains.kotlin.codegen.generated.AbstractBlackBoxInlineCodegenTest
import org.jetbrains.kotlin.findUsages.AbstractJetFindUsagesTest
import org.jetbrains.kotlin.findUsages.AbstractKotlinFindUsagesInLibrarySourceTest
import org.jetbrains.kotlin.formatter.AbstractJetFormatterTest
import org.jetbrains.kotlin.formatter.AbstractJetTypingIndentationTestBase
import org.jetbrains.kotlin.generators.tests.generator.*
@@ -497,6 +498,10 @@ fun main(args: Array<String>) {
model("findUsages/java", pattern = """^(.+)\.0\.java$""")
}
testClass(javaClass<AbstractKotlinFindUsagesInLibrarySourceTest>()) {
model("findUsages/librarySources", pattern = """^(.+)\.0\.kt$""")
}
testClass(javaClass<AbstractJetMoveTest>()) {
model("refactoring/move", extension = "test", singleClass = true)
}
@@ -46,12 +46,13 @@ public object JavaResolveExtension : CacheExtension<(PsiElement) -> Pair<JavaDes
}
fun PsiMethod.getJavaMethodDescriptor(): FunctionDescriptor {
val resolver = JavaResolveExtension.getResolver(getProject(), this)
val method = getOriginalElement() as PsiMethod
val resolver = JavaResolveExtension.getResolver(method.getProject(), method)
val methodDescriptor = when {
this.isConstructor() -> resolver.resolveConstructor(JavaConstructorImpl(this))
else -> resolver.resolveMethod(JavaMethodImpl(this))
method.isConstructor() -> resolver.resolveConstructor(JavaConstructorImpl(method))
else -> resolver.resolveMethod(JavaMethodImpl(method))
}
assert(methodDescriptor != null) { "No descriptor found for " + getText() }
assert(methodDescriptor != null) { "No descriptor found for " + method.getText() }
return methodDescriptor!!
}
@@ -0,0 +1,18 @@
package library;
public class Foo {
public int x;
public static int X;
public Foo(int n) {
}
public void bar(int n) {
}
public static void baz(int n) {
}
}
@@ -0,0 +1,11 @@
// PSI_ELEMENT: com.intellij.psi.PsiClass
// OPTIONS: usages
// FIND_BY_REF
// FIND_BY_NAVIGATION_ELEMENT
package usages
import library.Foo
fun test() {
val foo: <caret>Foo
}
@@ -0,0 +1,2 @@
Local variable declaration (10: 14) val foo: Foo
Usage in import (7: 16) import library.Foo
@@ -0,0 +1,11 @@
// PSI_ELEMENT: com.intellij.psi.PsiMethod
// OPTIONS: usages
// FIND_BY_REF
// FIND_BY_NAVIGATION_ELEMENT
package usages
import library.Foo
fun test() {
<caret>Foo(1)
}
@@ -0,0 +1 @@
New instance creation (10: 5) Foo(1)
@@ -0,0 +1,11 @@
// PSI_ELEMENT: com.intellij.psi.PsiField
// OPTIONS: usages
// FIND_BY_REF
// FIND_BY_NAVIGATION_ELEMENT
package usages
import library.Foo
fun test() {
Foo().<caret>x = 1
}
@@ -0,0 +1 @@
Value write (10: 11) Foo().x = 1
@@ -0,0 +1,11 @@
// PSI_ELEMENT: com.intellij.psi.PsiMethod
// OPTIONS: usages
// FIND_BY_REF
// FIND_BY_NAVIGATION_ELEMENT
package usages
import library.Foo
fun test() {
Foo().<caret>bar(1)
}
@@ -0,0 +1 @@
Function call (10: 11) Foo().bar(1)
@@ -0,0 +1,11 @@
// PSI_ELEMENT: com.intellij.psi.PsiField
// OPTIONS: usages
// FIND_BY_REF
// FIND_BY_NAVIGATION_ELEMENT
package usages
import library.Foo
fun test() {
Foo.<caret>X = 1
}
@@ -0,0 +1 @@
Value write (10: 9) Foo.X = 1
@@ -0,0 +1,11 @@
// PSI_ELEMENT: com.intellij.psi.PsiMethod
// OPTIONS: usages
// FIND_BY_REF
// FIND_BY_NAVIGATION_ELEMENT
package usages
import library.Foo
fun test() {
Foo.<caret>baz(1)
}
@@ -0,0 +1 @@
Function call (10: 9) Foo.baz(1)
@@ -330,10 +330,22 @@ public abstract class AbstractJetFindUsagesTest extends JetLightCodeInsightFixtu
? TargetElementUtilBase.findTargetElement(myFixture.getEditor(),
TargetElementUtilBase.REFERENCED_ELEMENT_ACCEPTED | TargetElementUtil.NEW_AS_CONSTRUCTOR)
: myFixture.getElementAtCaret();
if (InTextDirectivesUtils.isDirectiveDefined(mainFileText, "// FIND_BY_MIRROR_ELEMENT")) {
boolean findByMirrorElement = InTextDirectivesUtils.isDirectiveDefined(mainFileText, "// FIND_BY_MIRROR_ELEMENT");
boolean findByNavigationElement = InTextDirectivesUtils.isDirectiveDefined(mainFileText, "// FIND_BY_NAVIGATION_ELEMENT");
if (findByMirrorElement && findByNavigationElement) {
fail("Incompatible directives");
}
if (findByMirrorElement) {
assert originalElement instanceof PsiCompiledElement : "PsiCompiledElement is expected: " + originalElement;
originalElement = ((PsiCompiledElement)originalElement).getMirror();
}
if (findByNavigationElement) {
assert originalElement != null : "Original element is not found";
originalElement = originalElement.getNavigationElement();
}
T caretElement = PsiTreeUtil.getParentOfType(originalElement, caretElementClass, false);
assertNotNull(String.format("Element with type '%s' wasn't found at caret position", caretElementClass), caretElement);
@@ -0,0 +1,27 @@
/*
* Copyright 2010-2015 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.kotlin.findUsages
import com.intellij.psi.PsiElement
import com.intellij.testFramework.LightProjectDescriptor
import org.jetbrains.kotlin.idea.test.JdkAndMockLibraryProjectDescriptor
import org.jetbrains.kotlin.idea.test.PluginTestCaseBase
public abstract class AbstractKotlinFindUsagesInLibrarySourceTest: AbstractJetFindUsagesTest() {
override fun getProjectDescriptor() =
JdkAndMockLibraryProjectDescriptor(PluginTestCaseBase.getTestDataPathBase() + "/findUsages/_library", true)
}
@@ -0,0 +1,73 @@
/*
* Copyright 2010-2015 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.kotlin.findUsages;
import com.intellij.testFramework.TestDataPath;
import org.jetbrains.kotlin.test.JUnit3RunnerWithInners;
import org.jetbrains.kotlin.test.JetTestUtils;
import org.jetbrains.kotlin.test.TestMetadata;
import org.junit.runner.RunWith;
import java.io.File;
import java.util.regex.Pattern;
/** This class is generated by {@link org.jetbrains.kotlin.generators.tests.TestsPackage}. DO NOT MODIFY MANUALLY */
@SuppressWarnings("all")
@TestMetadata("idea/testData/findUsages/librarySources")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public class KotlinFindUsagesInLibrarySourceTestGenerated extends AbstractKotlinFindUsagesInLibrarySourceTest {
public void testAllFilesPresentInLibrarySources() throws Exception {
JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/findUsages/librarySources"), Pattern.compile("^(.+)\\.0\\.kt$"), true);
}
@TestMetadata("LibraryClassUsages.0.kt")
public void testLibraryClassUsages() throws Exception {
String fileName = JetTestUtils.navigationMetadata("idea/testData/findUsages/librarySources/LibraryClassUsages.0.kt");
doTest(fileName);
}
@TestMetadata("LibraryConstructorUsages.0.kt")
public void testLibraryConstructorUsages() throws Exception {
String fileName = JetTestUtils.navigationMetadata("idea/testData/findUsages/librarySources/LibraryConstructorUsages.0.kt");
doTest(fileName);
}
@TestMetadata("LibraryFieldUsages.0.kt")
public void testLibraryFieldUsages() throws Exception {
String fileName = JetTestUtils.navigationMetadata("idea/testData/findUsages/librarySources/LibraryFieldUsages.0.kt");
doTest(fileName);
}
@TestMetadata("LibraryMethodUsages.0.kt")
public void testLibraryMethodUsages() throws Exception {
String fileName = JetTestUtils.navigationMetadata("idea/testData/findUsages/librarySources/LibraryMethodUsages.0.kt");
doTest(fileName);
}
@TestMetadata("LibraryStaticFieldUsages.0.kt")
public void testLibraryStaticFieldUsages() throws Exception {
String fileName = JetTestUtils.navigationMetadata("idea/testData/findUsages/librarySources/LibraryStaticFieldUsages.0.kt");
doTest(fileName);
}
@TestMetadata("LibraryStaticMethodUsages.0.kt")
public void testLibraryStaticMethodUsages() throws Exception {
String fileName = JetTestUtils.navigationMetadata("idea/testData/findUsages/librarySources/LibraryStaticMethodUsages.0.kt");
doTest(fileName);
}
}