Optimizing imports doesn't remove references to java static fields when they are referred via short name in code
This commit is contained in:
@@ -224,29 +224,31 @@ public class JetImportOptimizer implements ImportOptimizer {
|
||||
return new FqName(qualifiedName);
|
||||
}
|
||||
}
|
||||
|
||||
if (element instanceof PsiField) {
|
||||
PsiField field = (PsiField) element;
|
||||
|
||||
FqName classFQN = getFqNameOfContainingClassForPsiMember(field);
|
||||
if (classFQN == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return combineClassFqNameWithMemberName(classFQN, field.getName());
|
||||
}
|
||||
|
||||
// TODO: Still problem with kotlin global properties imported from class files
|
||||
if (element instanceof PsiMethod) {
|
||||
PsiMethod method = (PsiMethod) element;
|
||||
|
||||
PsiClass containingClass = method.getContainingClass();
|
||||
|
||||
if (containingClass != null) {
|
||||
String classFQNStr = containingClass.getQualifiedName();
|
||||
if (classFQNStr != null) {
|
||||
if (method.isConstructor()) {
|
||||
return new FqName(classFQNStr);
|
||||
}
|
||||
|
||||
FqName classFQN = new FqName(classFQNStr);
|
||||
if (classFQN.shortName().getName().equals(JvmAbi.PACKAGE_CLASS)) {
|
||||
return QualifiedNamesUtil.combine(classFQN.parent(), Name.identifier(method.getName()));
|
||||
}
|
||||
else {
|
||||
return QualifiedNamesUtil.combine(classFQN, Name.identifier(method.getName()));
|
||||
}
|
||||
}
|
||||
FqName classFQN = getFqNameOfContainingClassForPsiMember(method);
|
||||
if (classFQN == null) {
|
||||
return null;
|
||||
}
|
||||
if (method.isConstructor()) {
|
||||
return classFQN;
|
||||
}
|
||||
|
||||
return combineClassFqNameWithMemberName(classFQN, method.getName());
|
||||
}
|
||||
|
||||
if (element instanceof PsiPackage) {
|
||||
@@ -256,6 +258,31 @@ public class JetImportOptimizer implements ImportOptimizer {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
private static FqName combineClassFqNameWithMemberName(FqName classFQN, String memberName) {
|
||||
if (memberName == null) {
|
||||
return null;
|
||||
}
|
||||
if (classFQN.shortName().getName().equals(JvmAbi.PACKAGE_CLASS)) {
|
||||
return QualifiedNamesUtil.combine(classFQN.parent(), Name.identifier(memberName));
|
||||
}
|
||||
else {
|
||||
return QualifiedNamesUtil.combine(classFQN, Name.identifier(memberName));
|
||||
}
|
||||
}
|
||||
|
||||
@Nullable
|
||||
private static FqName getFqNameOfContainingClassForPsiMember(PsiMember member) {
|
||||
PsiClass containingClass = member.getContainingClass();
|
||||
if (containingClass != null) {
|
||||
String classFQNStr = containingClass.getQualifiedName();
|
||||
if (classFQNStr != null) {
|
||||
return new FqName(classFQNStr);
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private static PsiElement getWithPreviousWhitespaces(PsiElement element) {
|
||||
PsiElement result = element;
|
||||
|
||||
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
import MyJavaClass.TEST
|
||||
|
||||
fun main() {
|
||||
TEST
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
public class MyJavaClass {
|
||||
public static String TEST = "test";
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
import MyJavaClass
|
||||
import MyJavaClass.TEST
|
||||
|
||||
fun main() {
|
||||
TEST
|
||||
}
|
||||
@@ -33,6 +33,10 @@ public class OptimizeImportsMultiFileTest extends CodeInsightTestCase {
|
||||
doTest(getTestName(false) + "/main.kt", getTestName(false) + "/kotlinClass.kt");
|
||||
}
|
||||
|
||||
public void testJavaStaticField() throws Exception {
|
||||
doTest(getTestName(false) + "/main.kt", getTestName(false) + "/MyJavaClass.java");
|
||||
}
|
||||
|
||||
public void doTest(String... fileNames) throws Exception {
|
||||
configureByFiles(null, fileNames);
|
||||
invokeFormatFile();
|
||||
|
||||
Reference in New Issue
Block a user