Copy/Move: Fix processing of imported Java static members
#KT-18098 Fixed
This commit is contained in:
@@ -42,6 +42,7 @@ import org.jetbrains.kotlin.idea.refactoring.fqName.isImported
|
|||||||
import org.jetbrains.kotlin.idea.references.KtSimpleNameReference
|
import org.jetbrains.kotlin.idea.references.KtSimpleNameReference
|
||||||
import org.jetbrains.kotlin.idea.references.KtSimpleNameReference.ShorteningMode
|
import org.jetbrains.kotlin.idea.references.KtSimpleNameReference.ShorteningMode
|
||||||
import org.jetbrains.kotlin.idea.references.mainReference
|
import org.jetbrains.kotlin.idea.references.mainReference
|
||||||
|
import org.jetbrains.kotlin.load.java.descriptors.JavaCallableMemberDescriptor
|
||||||
import org.jetbrains.kotlin.name.FqName
|
import org.jetbrains.kotlin.name.FqName
|
||||||
import org.jetbrains.kotlin.psi.*
|
import org.jetbrains.kotlin.psi.*
|
||||||
import org.jetbrains.kotlin.psi.psiUtil.*
|
import org.jetbrains.kotlin.psi.psiUtil.*
|
||||||
@@ -123,6 +124,17 @@ fun KtElement.processInternalReferencesToUpdateOnPackageNameChange(
|
|||||||
val isExtension = isCallable && refExpr.isExtensionRef(bindingContext)
|
val isExtension = isCallable && refExpr.isExtensionRef(bindingContext)
|
||||||
val isCallableReference = isCallableReference(refExpr.mainReference)
|
val isCallableReference = isCallableReference(refExpr.mainReference)
|
||||||
|
|
||||||
|
val declaration by lazy {
|
||||||
|
var result = DescriptorToSourceUtilsIde.getAnyDeclaration(project, descriptor) ?: return@lazy null
|
||||||
|
|
||||||
|
if (descriptor.isCompanionObject()
|
||||||
|
&& bindingContext[BindingContext.SHORT_REFERENCE_TO_COMPANION_OBJECT, refExpr] != null) {
|
||||||
|
result = (result as KtObjectDeclaration).containingClassOrObject ?: result
|
||||||
|
}
|
||||||
|
|
||||||
|
result
|
||||||
|
}
|
||||||
|
|
||||||
if (isCallable) {
|
if (isCallable) {
|
||||||
if (!isCallableReference) {
|
if (!isCallableReference) {
|
||||||
if (isExtension && containingDescriptor is ClassDescriptor) {
|
if (isExtension && containingDescriptor is ClassDescriptor) {
|
||||||
@@ -137,17 +149,11 @@ fun KtElement.processInternalReferencesToUpdateOnPackageNameChange(
|
|||||||
|
|
||||||
if (!isExtension) {
|
if (!isExtension) {
|
||||||
if (!(containingDescriptor is PackageFragmentDescriptor
|
if (!(containingDescriptor is PackageFragmentDescriptor
|
||||||
|| containingDescriptor is ClassDescriptor && containingDescriptor.kind == ClassKind.OBJECT)) return null
|
|| containingDescriptor is ClassDescriptor && containingDescriptor.kind == ClassKind.OBJECT
|
||||||
|
|| descriptor is JavaCallableMemberDescriptor && ((declaration as? PsiMember)?.hasModifierProperty(PsiModifier.STATIC) ?: false))) return null
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var declaration = DescriptorToSourceUtilsIde.getAnyDeclaration(project, descriptor) ?: return null
|
|
||||||
|
|
||||||
if (descriptor.isCompanionObject()
|
|
||||||
&& bindingContext[BindingContext.SHORT_REFERENCE_TO_COMPANION_OBJECT, refExpr] != null) {
|
|
||||||
declaration = (declaration as KtObjectDeclaration).containingClassOrObject ?: declaration
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!DescriptorUtils.getFqName(descriptor).isSafe) return null
|
if (!DescriptorUtils.getFqName(descriptor).isSafe) return null
|
||||||
|
|
||||||
val (oldContainer, newContainer) = containerChangeInfo
|
val (oldContainer, newContainer) = containerChangeInfo
|
||||||
@@ -166,6 +172,8 @@ fun KtElement.processInternalReferencesToUpdateOnPackageNameChange(
|
|||||||
val isImported = isImported(descriptor)
|
val isImported = isImported(descriptor)
|
||||||
if (isImported && this is KtFile) return null
|
if (isImported && this is KtFile) return null
|
||||||
|
|
||||||
|
if (declaration == null) return null
|
||||||
|
|
||||||
if (isExtension || containerFqName != null || isImported) return {
|
if (isExtension || containerFqName != null || isImported) return {
|
||||||
createMoveUsageInfoIfPossible(it.mainReference, declaration, false, true)
|
createMoveUsageInfoIfPossible(it.mainReference, declaration, false, true)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,5 @@
|
|||||||
|
package foo;
|
||||||
|
|
||||||
|
public class J {
|
||||||
|
public static int JJJ = 1;
|
||||||
|
}
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
package foo
|
||||||
|
|
||||||
|
import foo.J.JJJ
|
||||||
|
|
||||||
|
fun test() {
|
||||||
|
val x = JJJ
|
||||||
|
}
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
package foo
|
||||||
|
|
||||||
|
fun test2() {
|
||||||
|
val x = J.JJJ
|
||||||
|
}
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
package foo;
|
||||||
|
|
||||||
|
public class J {
|
||||||
|
public static int JJJ = 1;
|
||||||
|
}
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
package foo
|
||||||
|
|
||||||
|
import foo.J.JJJ
|
||||||
|
|
||||||
|
fun <caret>test() {
|
||||||
|
val x = JJJ
|
||||||
|
}
|
||||||
Vendored
+5
@@ -0,0 +1,5 @@
|
|||||||
|
{
|
||||||
|
"mainFile": "foo/test.kt",
|
||||||
|
"targetPackage": "foo",
|
||||||
|
"newName": "test2"
|
||||||
|
}
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
package foo;
|
||||||
|
|
||||||
|
public class J {
|
||||||
|
public static void jjj() {
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
package foo
|
||||||
|
|
||||||
|
import foo.J.jjj
|
||||||
|
|
||||||
|
fun test() {
|
||||||
|
jjj()
|
||||||
|
}
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
package foo
|
||||||
|
|
||||||
|
fun test2() {
|
||||||
|
J.jjj()
|
||||||
|
}
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
package foo;
|
||||||
|
|
||||||
|
public class J {
|
||||||
|
public static void jjj() {
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
package foo
|
||||||
|
|
||||||
|
import foo.J.jjj
|
||||||
|
|
||||||
|
fun <caret>test() {
|
||||||
|
jjj()
|
||||||
|
}
|
||||||
Vendored
+5
@@ -0,0 +1,5 @@
|
|||||||
|
{
|
||||||
|
"mainFile": "foo/test.kt",
|
||||||
|
"targetPackage": "foo",
|
||||||
|
"newName": "test2"
|
||||||
|
}
|
||||||
@@ -167,4 +167,16 @@ public class CopyTestGenerated extends AbstractCopyTest {
|
|||||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/refactoring/copy/copyTopLevelPropertyWithRename/copyTopLevelPropertyWithRename.test");
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/refactoring/copy/copyTopLevelPropertyWithRename/copyTopLevelPropertyWithRename.test");
|
||||||
doTest(fileName);
|
doTest(fileName);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@TestMetadata("refToImportJavaStaticField/refToImportedJavaStaticField.test")
|
||||||
|
public void testRefToImportJavaStaticField_RefToImportedJavaStaticField() throws Exception {
|
||||||
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/refactoring/copy/refToImportJavaStaticField/refToImportedJavaStaticField.test");
|
||||||
|
doTest(fileName);
|
||||||
|
}
|
||||||
|
|
||||||
|
@TestMetadata("refToImportJavaStaticMethod/refToImportedJavaStaticMethod.test")
|
||||||
|
public void testRefToImportJavaStaticMethod_RefToImportedJavaStaticMethod() throws Exception {
|
||||||
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/refactoring/copy/refToImportJavaStaticMethod/refToImportedJavaStaticMethod.test");
|
||||||
|
doTest(fileName);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user