mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
add type cast as fix for unresolved calls (IDEA-185569)
This commit is contained in:
+35
-1
@@ -16,11 +16,14 @@
|
||||
|
||||
package com.intellij.codeInsight.daemon.impl.quickfix;
|
||||
|
||||
import com.intellij.codeInsight.daemon.QuickFixActionRegistrar;
|
||||
import com.intellij.codeInsight.daemon.QuickFixBundle;
|
||||
import com.intellij.codeInsight.guess.GuessManager;
|
||||
import com.intellij.codeInsight.intention.HighPriorityAction;
|
||||
import com.intellij.codeInspection.LocalQuickFixAndIntentionActionOnPsiElement;
|
||||
import com.intellij.openapi.editor.Editor;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.openapi.util.TextRange;
|
||||
import com.intellij.psi.*;
|
||||
import com.intellij.psi.codeStyle.CodeStyleManager;
|
||||
import com.intellij.psi.codeStyle.JavaCodeStyleManager;
|
||||
@@ -30,20 +33,28 @@ import com.intellij.psi.util.TypeConversionUtil;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static com.intellij.util.ObjectUtils.assertNotNull;
|
||||
|
||||
public class AddTypeCastFix extends LocalQuickFixAndIntentionActionOnPsiElement implements HighPriorityAction {
|
||||
private final PsiType myType;
|
||||
private final String myName;
|
||||
|
||||
public AddTypeCastFix(@NotNull PsiType type, @NotNull PsiExpression expression) {
|
||||
this(type, expression, "add.typecast.text");
|
||||
}
|
||||
|
||||
public AddTypeCastFix(@NotNull PsiType type, @NotNull PsiExpression expression, String messageKey) {
|
||||
super(expression);
|
||||
myType = type;
|
||||
myName = QuickFixBundle.message(messageKey, type.isValid() ? type.getCanonicalText() : "");
|
||||
}
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
public String getText() {
|
||||
return QuickFixBundle.message("add.typecast.text", myType.isValid() ? myType.getCanonicalText() : "");
|
||||
return myName;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -118,4 +129,27 @@ public class AddTypeCastFix extends LocalQuickFixAndIntentionActionOnPsiElement
|
||||
|
||||
return typeCast;
|
||||
}
|
||||
|
||||
public static void registerFix(QuickFixActionRegistrar registrar,
|
||||
PsiExpression qualifier,
|
||||
PsiJavaCodeReferenceElement ref,
|
||||
TextRange fixRange) {
|
||||
String referenceName = ref.getReferenceName();
|
||||
if (referenceName == null) return;
|
||||
PsiElement gParent = ref.getParent();
|
||||
List<PsiType> conjuncts = GuessManager.getInstance(qualifier.getProject()).getControlFlowExpressionTypeConjuncts(qualifier);
|
||||
for (PsiType conjunct : conjuncts) {
|
||||
PsiClass psiClass = PsiUtil.resolveClassInType(conjunct);
|
||||
if (psiClass == null) continue;
|
||||
if (gParent instanceof PsiMethodCallExpression) {
|
||||
if (psiClass.findMethodsByName(referenceName).length == 0) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
else if (psiClass.findFieldByName(referenceName, true) == null) {
|
||||
continue;
|
||||
}
|
||||
registrar.register(fixRange, new AddTypeCastFix(conjunct, qualifier, "add.qualifier.typecast.text"), null);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -29,6 +29,7 @@ add.runtime.exception.to.throws.text=Add runtime exception(s) to method signatur
|
||||
add.runtime.exception.to.throws.family=Add Runtime Exception to Method Signature
|
||||
add.typecast.family=Add TypeCast
|
||||
add.typecast.text=Cast to ''{0}''
|
||||
add.qualifier.typecast.text=Cast qualifier to ''{0}''
|
||||
add.docTag.to.custom.tags=Add {0} to custom tags
|
||||
fix.javadoc.family=Fix Javadoc
|
||||
adjust.package.family=Adjust Package Name
|
||||
|
||||
+5
-1
@@ -49,9 +49,13 @@ public class DefaultQuickFixProvider extends UnresolvedReferenceQuickFixProvider
|
||||
PsiReferenceExpression refExpr = (PsiReferenceExpression)ref;
|
||||
|
||||
registrar.register(new RenameWrongRefFix(refExpr));
|
||||
if (!ref.isQualified()) {
|
||||
PsiExpression qualifier = ((PsiReferenceExpression)ref).getQualifierExpression();
|
||||
if (qualifier == null) {
|
||||
registrar.register(fixRange, new BringVariableIntoScopeFix(refExpr), null);
|
||||
}
|
||||
else {
|
||||
AddTypeCastFix.registerFix(registrar, qualifier, ref, fixRange);
|
||||
}
|
||||
|
||||
for (IntentionAction action : createVariableActions(refExpr)) {
|
||||
registrar.register(fixRange, action, null);
|
||||
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
// "Cast qualifier to 'java.lang.String'" "true"
|
||||
class Test {
|
||||
void m(Object o) {
|
||||
if (o instanceof String) {
|
||||
System.out.println(((String) o).length());
|
||||
}
|
||||
}
|
||||
}
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
// "Cast qualifier to 'java.lang.String'" "true"
|
||||
class Test {
|
||||
void m(Object o) {
|
||||
if (o instanceof String) {
|
||||
System.out.println(<caret>o.length());
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user