correcty find Java usages of static methods generated for companion object members annotated as jvmStatic
#KT-8902 Fixed
This commit is contained in:
+26
@@ -38,6 +38,8 @@ import org.jetbrains.kotlin.idea.stubindex.JetSourceFilterScope
|
||||
import org.jetbrains.kotlin.idea.util.ProjectRootsUtil
|
||||
import org.jetbrains.kotlin.idea.util.application.runReadAction
|
||||
import org.jetbrains.kotlin.psi.*
|
||||
import org.jetbrains.kotlin.psi.psiUtil.getStrictParentOfType
|
||||
import org.jetbrains.kotlin.psi.psiUtil.parents
|
||||
|
||||
public class KotlinReferencesSearcher : QueryExecutorBase<PsiReference, ReferencesSearch.SearchParameters>() {
|
||||
|
||||
@@ -136,6 +138,19 @@ public class KotlinReferencesSearcher : QueryExecutorBase<PsiReference, Referenc
|
||||
}
|
||||
}
|
||||
|
||||
private fun findStaticMethodFromCompanionObject(function: JetFunction): PsiMethod? {
|
||||
val originObject = function.parents
|
||||
.dropWhile { it is JetClassBody }
|
||||
.firstOrNull() as? JetObjectDeclaration ?: return null
|
||||
if (originObject.isCompanion()) {
|
||||
val originClass = originObject.getStrictParentOfType<JetClass>()
|
||||
val originLightClass = LightClassUtil.getPsiClass(originClass)
|
||||
val allMethods = originLightClass?.allMethods
|
||||
return allMethods?.find { it is KotlinLightMethod && it.getOrigin() == function }
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
private fun searchLightElements(queryParameters: ReferencesSearch.SearchParameters, element: PsiElement) {
|
||||
when (element) {
|
||||
is JetClassOrObject -> processJetClassOrObject(element, queryParameters)
|
||||
@@ -146,6 +161,11 @@ public class KotlinReferencesSearcher : QueryExecutorBase<PsiReference, Referenc
|
||||
val method = runReadAction { LightClassUtil.getLightClassMethod(function) }
|
||||
searchNamedElement(queryParameters, method)
|
||||
}
|
||||
|
||||
val staticFromCompanionObject = findStaticMethodFromCompanionObject(element)
|
||||
if (staticFromCompanionObject != null) {
|
||||
searchNamedElement(queryParameters, staticFromCompanionObject)
|
||||
}
|
||||
}
|
||||
is JetProperty -> {
|
||||
val propertyMethods = runReadAction { LightClassUtil.getLightClassPropertyMethods(element) }
|
||||
@@ -160,6 +180,12 @@ public class KotlinReferencesSearcher : QueryExecutorBase<PsiReference, Referenc
|
||||
else if (declaration is JetPropertyAccessor) {
|
||||
searchNamedElement(queryParameters, PsiTreeUtil.getParentOfType<JetProperty>(declaration, javaClass<JetProperty>()))
|
||||
}
|
||||
else if (declaration is JetFunction) {
|
||||
val staticFromCompanionObject = findStaticMethodFromCompanionObject(declaration)
|
||||
if (staticFromCompanionObject != null) {
|
||||
searchNamedElement(queryParameters, staticFromCompanionObject)
|
||||
}
|
||||
}
|
||||
}
|
||||
is KotlinLightParameter -> {
|
||||
val componentFunctionDescriptor = element.getOrigin()?.dataClassComponentFunction()
|
||||
|
||||
Vendored
+7
@@ -0,0 +1,7 @@
|
||||
package testing.rename
|
||||
|
||||
public open class C {
|
||||
companion object {
|
||||
jvmStatic fun second() {}
|
||||
}
|
||||
}
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
package testing;
|
||||
|
||||
import testing.rename.C;
|
||||
|
||||
class JavaClient {
|
||||
public void foo() {
|
||||
C.second();
|
||||
}
|
||||
}
|
||||
Vendored
+7
@@ -0,0 +1,7 @@
|
||||
package testing.rename
|
||||
|
||||
public open class C {
|
||||
companion object {
|
||||
jvmStatic fun first() {}
|
||||
}
|
||||
}
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
package testing;
|
||||
|
||||
import testing.rename.C;
|
||||
|
||||
class JavaClient {
|
||||
public void foo() {
|
||||
C.first();
|
||||
}
|
||||
}
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
{
|
||||
"type": "KOTLIN_FUNCTION",
|
||||
"classId": "testing/rename/C.Companion",
|
||||
"oldName": "first",
|
||||
"newName": "second",
|
||||
"withRuntime": "true"
|
||||
}
|
||||
@@ -44,6 +44,7 @@ import org.jetbrains.kotlin.idea.jsonUtils.getNullableString
|
||||
import org.jetbrains.kotlin.idea.jsonUtils.getString
|
||||
import org.jetbrains.kotlin.idea.references.mainReference
|
||||
import org.jetbrains.kotlin.idea.search.allScope
|
||||
import org.jetbrains.kotlin.idea.test.ConfigLibraryUtil
|
||||
import org.jetbrains.kotlin.idea.test.DirectiveBasedActionUtils
|
||||
import org.jetbrains.kotlin.idea.test.KotlinMultiFileTestCase
|
||||
import org.jetbrains.kotlin.idea.test.PluginTestCaseBase
|
||||
@@ -83,6 +84,11 @@ public abstract class AbstractRenameTest : KotlinMultiFileTestCase() {
|
||||
|
||||
val hintDirective = renameObject.getNullableString("hint")
|
||||
|
||||
val withRuntime = renameObject.getNullableString("withRuntime")
|
||||
if (withRuntime != null) {
|
||||
ConfigLibraryUtil.configureKotlinRuntimeAndSdk(myModule, PluginTestCaseBase.mockJdk())
|
||||
}
|
||||
|
||||
try {
|
||||
val context = TestContext()
|
||||
|
||||
|
||||
@@ -311,6 +311,12 @@ public class RenameTestGenerated extends AbstractRenameTest {
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("renameKotlinStaticMethod/renameKotlinStaticMethod.test")
|
||||
public void testRenameKotlinStaticMethod_RenameKotlinStaticMethod() throws Exception {
|
||||
String fileName = JetTestUtils.navigationMetadata("idea/testData/refactoring/rename/renameKotlinStaticMethod/renameKotlinStaticMethod.test");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("renameKotlinValProperty/renameAsJavaGetterForExplicitGetter.test")
|
||||
public void testRenameKotlinValProperty_RenameAsJavaGetterForExplicitGetter() throws Exception {
|
||||
String fileName = JetTestUtils.navigationMetadata("idea/testData/refactoring/rename/renameKotlinValProperty/renameAsJavaGetterForExplicitGetter.test");
|
||||
|
||||
Reference in New Issue
Block a user