Find Usages: Fixed searching of Java usages for @JvmStatic properties and @JvmStatic @JvmOverloads functions

#KT-11736 Fixed
This commit is contained in:
Alexey Sedunov
2016-04-19 15:13:31 +03:00
parent 1635018fe7
commit 021c88e5ff
23 changed files with 182 additions and 15 deletions
+1
View File
@@ -27,6 +27,7 @@ Issues fixed:
- [KT-11866](https://youtrack.jetbrains.com/issue/KT-11866) Suggest "Create secondary constructor" when constructors exist but are not applicable
- [KT-11908](https://youtrack.jetbrains.com/issue/KT-11866) Allow properties with custom setters to be used in generated equals/hashCode/toString
- [KT-11845](https://youtrack.jetbrains.com/issue/KT-11845) Fixed exception on attempt to find derived classes
- [KT-11736](https://youtrack.jetbrains.com/issue/KT-11736) Fixed searching of Java usages for @JvmStatic properties and @JvmStatic @JvmOverloads functions
#### Debugger
@@ -210,17 +210,22 @@ class KotlinReferencesSearcher : QueryExecutorBase<PsiReference, ReferencesSearc
}
}
private fun findStaticMethodFromCompanionObject(function: KtFunction): PsiMethod? {
val originObject = function.parents
private fun findStaticMethodsFromCompanionObject(declaration: KtDeclaration): List<PsiMethod> {
val originObject = declaration.parents
.dropWhile { it is KtClassBody }
.firstOrNull() as? KtObjectDeclaration ?: return null
.firstOrNull() as? KtObjectDeclaration ?: return emptyList()
if (originObject.isCompanion()) {
val originClass = originObject.getStrictParentOfType<KtClass>()
val originLightClass = originClass?.toLightClass()
val allMethods = originLightClass?.allMethods
return allMethods?.find { it is KtLightMethod && it.kotlinOrigin == function }
val originLightClass = originClass?.toLightClass() ?: return emptyList()
val allMethods = originLightClass.allMethods
return allMethods.filter { it is KtLightMethod && it.kotlinOrigin == declaration }
}
return null
return emptyList()
}
private fun processStaticsFromCompanionObject(element: KtDeclaration, queryParameters: ReferencesSearch.SearchParameters) {
val staticsFromCompanionObject = runReadAction { findStaticMethodsFromCompanionObject(element) }
staticsFromCompanionObject.forEach { searchNamedElement(queryParameters, it) }
}
private fun searchPropertyMethods(queryParameters: ReferencesSearch.SearchParameters, parameter: KtParameter) {
@@ -252,15 +257,13 @@ class KotlinReferencesSearcher : QueryExecutorBase<PsiReference, ReferencesSearc
}
}
val staticFromCompanionObject = runReadAction { findStaticMethodFromCompanionObject(element) }
if (staticFromCompanionObject != null) {
searchNamedElement(queryParameters, staticFromCompanionObject)
}
processStaticsFromCompanionObject(element, queryParameters)
}
is KtProperty -> {
val propertyMethods = runReadAction { LightClassUtil.getLightClassPropertyMethods(element) }
propertyMethods.allDeclarations.forEach { searchNamedElement(queryParameters, it) }
processStaticsFromCompanionObject(element, queryParameters)
}
is KtParameter -> {
@@ -278,16 +281,14 @@ class KotlinReferencesSearcher : QueryExecutorBase<PsiReference, ReferencesSearc
val declaration = element.kotlinOrigin
if (declaration is KtProperty || (declaration is KtParameter && declaration.hasValOrVar())) {
searchNamedElement(queryParameters, declaration as PsiNamedElement)
processStaticsFromCompanionObject(declaration, queryParameters)
}
else if (declaration is KtPropertyAccessor) {
val property = declaration.getStrictParentOfType<KtProperty>()
searchNamedElement(queryParameters, property)
}
else if (declaration is KtFunction) {
val staticFromCompanionObject = runReadAction { findStaticMethodFromCompanionObject(declaration) }
if (staticFromCompanionObject != null) {
searchNamedElement(queryParameters, staticFromCompanionObject)
}
processStaticsFromCompanionObject(declaration, queryParameters)
}
}
@@ -0,0 +1,9 @@
// PSI_ELEMENT: org.jetbrains.kotlin.psi.KtNamedFunction
// OPTIONS: usages
class Foo {
companion object {
@JvmStatic fun <caret>foo() {
}
}
}
@@ -0,0 +1,5 @@
class Bar {
{
Foo.foo();
}
}
@@ -0,0 +1 @@
Unclassified usage 3 Foo.foo();
@@ -0,0 +1,11 @@
// PSI_ELEMENT: org.jetbrains.kotlin.psi.KtNamedFunction
// OPTIONS: usages
class Foo {
companion object {
@JvmStatic
@JvmOverloads
fun <caret>foo(n: Int = 1) {
}
}
}
@@ -0,0 +1,6 @@
class Bar {
{
Foo.foo(2);
Foo.foo();
}
}
@@ -0,0 +1,2 @@
Unclassified usage 3 Foo.foo(2);
Unclassified usage 4 Foo.foo();
@@ -0,0 +1,7 @@
// PSI_ELEMENT: org.jetbrains.kotlin.psi.KtProperty
// OPTIONS: usages
class Foo {
companion object {
@JvmStatic var <caret>foo = 1
}
}
@@ -0,0 +1,6 @@
class Bar {
{
Foo.getFoo();
Foo.setFoo(2);
}
}
@@ -0,0 +1,2 @@
Unclassified usage 3 Foo.getFoo();
Unclassified usage 4 Foo.setFoo(2);
@@ -0,0 +1,9 @@
package testing.rename
public open class C {
companion object {
@JvmStatic
@JvmOverloads
fun second(n: Int = 1) {}
}
}
@@ -0,0 +1,10 @@
package testing;
import testing.rename.C;
class JavaClient {
public void foo() {
C.second(2);
C.second();
}
}
@@ -0,0 +1,9 @@
package testing.rename
public open class C {
companion object {
@JvmStatic
@JvmOverloads
fun first(n: Int = 1) {}
}
}
@@ -0,0 +1,10 @@
package testing;
import testing.rename.C;
class JavaClient {
public void foo() {
C.first(2);
C.first();
}
}
@@ -0,0 +1,7 @@
{
"type": "KOTLIN_FUNCTION",
"classId": "testing/rename/C.Companion",
"oldName": "first",
"newName": "second",
"withRuntime": "true"
}
@@ -0,0 +1,7 @@
package testing.rename
public open class C {
companion object {
@JvmStatic var second = 1
}
}
@@ -0,0 +1,10 @@
package testing;
import testing.rename.C;
class JavaClient {
public void foo() {
C.getSecond();
C.setSecond(2);
}
}
@@ -0,0 +1,7 @@
package testing.rename
public open class C {
companion object {
@JvmStatic var first = 1
}
}
@@ -0,0 +1,10 @@
package testing;
import testing.rename.C;
class JavaClient {
public void foo() {
C.getFirst();
C.setFirst(2);
}
}
@@ -0,0 +1,7 @@
{
"type": "KOTLIN_PROPERTY",
"classId": "testing/rename/C.Companion",
"oldName": "first",
"newName": "second",
"withRuntime": "true"
}
@@ -558,6 +558,18 @@ public class FindUsagesTestGenerated extends AbstractFindUsagesTest {
doTest(fileName);
}
@TestMetadata("jvmStaticFun.0.kt")
public void testJvmStaticFun() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/findUsages/kotlin/findFunctionUsages/jvmStaticFun.0.kt");
doTest(fileName);
}
@TestMetadata("jvmStaticJvmOverloadsFun.0.kt")
public void testJvmStaticJvmOverloadsFun() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/findUsages/kotlin/findFunctionUsages/jvmStaticJvmOverloadsFun.0.kt");
doTest(fileName);
}
@TestMetadata("kotlinInternalMethodUsages.0.kt")
public void testKotlinInternalMethodUsages() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/findUsages/kotlin/findFunctionUsages/kotlinInternalMethodUsages.0.kt");
@@ -912,6 +924,12 @@ public class FindUsagesTestGenerated extends AbstractFindUsagesTest {
doTest(fileName);
}
@TestMetadata("jvmStaticProperty.0.kt")
public void testJvmStaticProperty() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/findUsages/kotlin/findPropertyUsages/jvmStaticProperty.0.kt");
doTest(fileName);
}
@TestMetadata("kotlinClassObjectPropertyUsage.0.kt")
public void testKotlinClassObjectPropertyUsage() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/findUsages/kotlin/findPropertyUsages/kotlinClassObjectPropertyUsage.0.kt");
@@ -371,6 +371,18 @@ public class RenameTestGenerated extends AbstractRenameTest {
doTest(fileName);
}
@TestMetadata("renameKotlinStaticMethodWithJvmOverloads/renameKotlinStaticMethodWithJvmOverloads.test")
public void testRenameKotlinStaticMethodWithJvmOverloads_RenameKotlinStaticMethodWithJvmOverloads() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/refactoring/rename/renameKotlinStaticMethodWithJvmOverloads/renameKotlinStaticMethodWithJvmOverloads.test");
doTest(fileName);
}
@TestMetadata("renameKotlinStaticProperty/renameKotlinStaticProperty.test")
public void testRenameKotlinStaticProperty_RenameKotlinStaticProperty() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/refactoring/rename/renameKotlinStaticProperty/renameKotlinStaticProperty.test");
doTest(fileName);
}
@TestMetadata("renameKotlinTopLevelVarProperty/renameTopLevelVarProperty.test")
public void testRenameKotlinTopLevelVarProperty_RenameTopLevelVarProperty() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/refactoring/rename/renameKotlinTopLevelVarProperty/renameTopLevelVarProperty.test");