mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-26 19:06:24 +07:00
PSI leaks via LocalQuickFix
This commit is contained in:
@@ -376,11 +376,11 @@ public class HighlightUtil {
|
||||
IElementType eqOpSign = operationSign.getTokenType();
|
||||
IElementType opSign = TypeConversionUtil.convertEQtoOperation(eqOpSign);
|
||||
if (opSign == null) return null;
|
||||
HighlightInfo errorResult = null;
|
||||
final PsiType lType = assignment.getLExpression().getType();
|
||||
final PsiExpression rExpression = assignment.getRExpression();
|
||||
if (rExpression == null) return null;
|
||||
final PsiType rType = rExpression.getType();
|
||||
HighlightInfo errorResult = null;
|
||||
if (!TypeConversionUtil.isBinaryOperatorApplicable(opSign, lType, rType, true) ||
|
||||
PsiType.getJavaLangObject(assignment.getManager(), assignment.getResolveScope()).equals(lType)) {
|
||||
String operatorText = operationSign.getText().substring(0, operationSign.getText().length() - 1);
|
||||
@@ -564,10 +564,10 @@ public class HighlightUtil {
|
||||
@Nullable
|
||||
static HighlightInfo checkVariableAlreadyDefined(PsiVariable variable) {
|
||||
if (variable instanceof ExternallyDefinedPsiElement) return null;
|
||||
boolean isIncorrect = false;
|
||||
PsiIdentifier identifier = variable.getNameIdentifier();
|
||||
assert identifier != null : variable;
|
||||
String name = variable.getName();
|
||||
boolean isIncorrect = false;
|
||||
if (variable instanceof PsiLocalVariable ||
|
||||
variable instanceof PsiParameter && ((PsiParameter)variable).getDeclarationScope() instanceof PsiCatchSection ||
|
||||
variable instanceof PsiParameter && ((PsiParameter)variable).getDeclarationScope() instanceof PsiForeachStatement) {
|
||||
|
||||
+35
-18
@@ -18,7 +18,7 @@ package com.intellij.codeInsight.daemon.impl.quickfix;
|
||||
import com.intellij.codeInsight.CodeInsightUtilBase;
|
||||
import com.intellij.codeInsight.daemon.QuickFixBundle;
|
||||
import com.intellij.codeInsight.generation.GenerateMembersUtil;
|
||||
import com.intellij.codeInspection.IntentionAndQuickFixAction;
|
||||
import com.intellij.codeInspection.LocalQuickFixAndIntentionActionOnPsiElement;
|
||||
import com.intellij.openapi.diagnostic.Logger;
|
||||
import com.intellij.openapi.editor.Editor;
|
||||
import com.intellij.openapi.project.Project;
|
||||
@@ -31,22 +31,22 @@ import com.intellij.util.IncorrectOperationException;
|
||||
import com.intellij.util.containers.ContainerUtil;
|
||||
import org.jetbrains.annotations.NonNls;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class AddMethodFix extends IntentionAndQuickFixAction {
|
||||
public class AddMethodFix extends LocalQuickFixAndIntentionActionOnPsiElement {
|
||||
private static final Logger LOG = Logger.getInstance("#com.intellij.codeInsight.daemon.impl.quickfix.AddMethodFix");
|
||||
|
||||
private final PsiClass myClass;
|
||||
private final PsiMethod myMethod;
|
||||
private final PsiMethod myMethodPrototype;
|
||||
private String myText;
|
||||
private final List<String> myExceptions = new ArrayList<String>();
|
||||
|
||||
public AddMethodFix(@NotNull PsiMethod method, @NotNull PsiClass implClass) {
|
||||
myMethod = method;
|
||||
myClass = implClass;
|
||||
setText(QuickFixBundle.message("add.method.text", method.getName(), implClass.getName()));
|
||||
public AddMethodFix(@NotNull PsiMethod methodPrototype, @NotNull PsiClass implClass) {
|
||||
super(implClass);
|
||||
myMethodPrototype = methodPrototype;
|
||||
setText(QuickFixBundle.message("add.method.text", methodPrototype.getName(), implClass.getName()));
|
||||
}
|
||||
|
||||
public AddMethodFix(@NonNls @NotNull String methodText, @NotNull PsiClass implClass, @NotNull String... exceptions) {
|
||||
@@ -78,7 +78,8 @@ public class AddMethodFix extends IntentionAndQuickFixAction {
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public String getName() {
|
||||
@Override
|
||||
public String getText() {
|
||||
return myText;
|
||||
}
|
||||
|
||||
@@ -87,28 +88,44 @@ public class AddMethodFix extends IntentionAndQuickFixAction {
|
||||
return QuickFixBundle.message("add.method.family");
|
||||
}
|
||||
|
||||
public boolean isAvailable(@NotNull final Project project, final Editor editor, final PsiFile file) {
|
||||
return myMethod != null
|
||||
&& myMethod.isValid()
|
||||
&& myClass != null
|
||||
@Override
|
||||
public boolean isAvailable(@NotNull Project project,
|
||||
@NotNull PsiFile file,
|
||||
@NotNull PsiElement startElement,
|
||||
@NotNull PsiElement endElement) {
|
||||
final PsiClass myClass = (PsiClass)startElement;
|
||||
|
||||
return myMethodPrototype != null
|
||||
&& myMethodPrototype.isValid()
|
||||
&& myClass.isValid()
|
||||
&& myClass.getManager().isInProject(myClass)
|
||||
&& myText != null
|
||||
&& MethodSignatureUtil.findMethodBySignature(myClass, myMethod, false) == null
|
||||
&& MethodSignatureUtil.findMethodBySignature(myClass, myMethodPrototype, false) == null
|
||||
;
|
||||
}
|
||||
|
||||
public void applyFix(final Project project, final PsiFile file, final Editor editor) {
|
||||
@Override
|
||||
public void invoke(@NotNull Project project,
|
||||
@NotNull PsiFile file,
|
||||
@Nullable("is null when called from inspection") Editor editor,
|
||||
@NotNull PsiElement startElement,
|
||||
@NotNull PsiElement endElement) {
|
||||
final PsiClass myClass = (PsiClass)startElement;
|
||||
if (!CodeInsightUtilBase.prepareFileForWrite(myClass.getContainingFile())) return;
|
||||
PsiCodeBlock body;
|
||||
if (myClass.isInterface() && (body = myMethod.getBody()) != null) body.delete();
|
||||
if (myClass.isInterface() && (body = myMethodPrototype.getBody()) != null) body.delete();
|
||||
for (String exception : myExceptions) {
|
||||
PsiUtil.addException(myMethod, exception);
|
||||
PsiUtil.addException(myMethodPrototype, exception);
|
||||
}
|
||||
PsiMethod method = (PsiMethod)myClass.add(myMethod);
|
||||
PsiMethod method = (PsiMethod)myClass.add(myMethodPrototype);
|
||||
method = (PsiMethod)method.replace(reformat(project, method));
|
||||
if (editor != null) {
|
||||
GenerateMembersUtil.positionCaret(editor, method, true);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean startInWriteAction() {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
+7
-4
@@ -29,15 +29,18 @@ import org.jetbrains.annotations.NotNull;
|
||||
* changes 'class a extends b' to 'class a implements b' or vice versa
|
||||
*/
|
||||
public class ChangeExtendsToImplementsFix extends ExtendsListFix {
|
||||
private final String myName;
|
||||
|
||||
public ChangeExtendsToImplementsFix(PsiClass aClass, PsiClassType classToExtendFrom) {
|
||||
super(aClass, classToExtendFrom, true);
|
||||
myName = QuickFixBundle.message("exchange.extends.implements.keyword",
|
||||
aClass.isInterface() == myClassToExtendFrom.isInterface() ? PsiKeyword.IMPLEMENTS : PsiKeyword.EXTENDS,
|
||||
aClass.isInterface() == myClassToExtendFrom.isInterface() ? PsiKeyword.EXTENDS : PsiKeyword.IMPLEMENTS,
|
||||
myClassToExtendFrom.getQualifiedName());
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public String getText() {
|
||||
return QuickFixBundle.message("exchange.extends.implements.keyword",
|
||||
myClass.isInterface() == myClassToExtendFrom.isInterface() ? PsiKeyword.IMPLEMENTS : PsiKeyword.EXTENDS,
|
||||
myClass.isInterface() == myClassToExtendFrom.isInterface() ? PsiKeyword.EXTENDS : PsiKeyword.IMPLEMENTS,
|
||||
myClassToExtendFrom.getQualifiedName());
|
||||
return myName;
|
||||
}
|
||||
}
|
||||
+17
-8
@@ -30,6 +30,7 @@ import com.intellij.codeInsight.daemon.impl.HighlightInfo;
|
||||
import com.intellij.codeInsight.generation.OverrideImplementUtil;
|
||||
import com.intellij.codeInsight.generation.PsiMethodMember;
|
||||
import com.intellij.openapi.application.ApplicationManager;
|
||||
import com.intellij.openapi.command.undo.UndoUtil;
|
||||
import com.intellij.openapi.editor.Editor;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.psi.*;
|
||||
@@ -39,6 +40,7 @@ import com.intellij.psi.util.TypeConversionUtil;
|
||||
import com.intellij.util.Function;
|
||||
import com.intellij.util.containers.ContainerUtil;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
@@ -52,25 +54,31 @@ public class ChangeParameterClassFix extends ExtendsListFix {
|
||||
return QuickFixBundle.message("change.parameter.class.family");
|
||||
}
|
||||
|
||||
public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile file) {
|
||||
@Override
|
||||
public boolean isAvailable(@NotNull Project project,
|
||||
@NotNull PsiFile file,
|
||||
@NotNull PsiElement startElement,
|
||||
@NotNull PsiElement endElement) {
|
||||
return
|
||||
super.isAvailable(project, editor, file)
|
||||
&& myClass != null
|
||||
&& myClass.isValid()
|
||||
&& myClass.getQualifiedName() != null
|
||||
super.isAvailable(project, file, startElement, endElement)
|
||||
&& myClassToExtendFrom != null
|
||||
&& myClassToExtendFrom.isValid()
|
||||
&& myClassToExtendFrom.getQualifiedName() != null
|
||||
&& myClass.getManager().isInProject(myClass)
|
||||
;
|
||||
}
|
||||
|
||||
public void invoke(@NotNull final Project project, Editor editor, PsiFile file) {
|
||||
@Override
|
||||
public void invoke(@NotNull Project project,
|
||||
@NotNull PsiFile file,
|
||||
@Nullable("is null when called from inspection") Editor editor,
|
||||
@NotNull PsiElement startElement,
|
||||
@NotNull PsiElement endElement) {
|
||||
final PsiClass myClass = (PsiClass)startElement;
|
||||
if (!CodeInsightUtilBase.prepareFileForWrite(file)) return;
|
||||
ApplicationManager.getApplication().runWriteAction(
|
||||
new Runnable() {
|
||||
public void run() {
|
||||
invokeImpl();
|
||||
invokeImpl(myClass);
|
||||
}
|
||||
}
|
||||
);
|
||||
@@ -99,6 +107,7 @@ public class ChangeParameterClassFix extends ExtendsListFix {
|
||||
OverrideImplementUtil.chooseAndImplementMethods(project, editor1, myClass);
|
||||
}
|
||||
}
|
||||
UndoUtil.markPsiFileForUndo(file);
|
||||
}
|
||||
|
||||
public static void registerQuickFixActions(PsiMethodCallExpression methodCall, PsiExpressionList list, HighlightInfo highlightInfo) {
|
||||
|
||||
+56
-49
@@ -17,9 +17,7 @@ package com.intellij.codeInsight.daemon.impl.quickfix;
|
||||
|
||||
import com.intellij.codeInsight.CodeInsightUtilBase;
|
||||
import com.intellij.codeInsight.daemon.QuickFixBundle;
|
||||
import com.intellij.codeInsight.intention.IntentionAction;
|
||||
import com.intellij.codeInspection.LocalQuickFix;
|
||||
import com.intellij.codeInspection.ProblemDescriptor;
|
||||
import com.intellij.codeInspection.LocalQuickFixAndIntentionActionOnPsiElement;
|
||||
import com.intellij.openapi.command.undo.UndoUtil;
|
||||
import com.intellij.openapi.diagnostic.Logger;
|
||||
import com.intellij.openapi.editor.Editor;
|
||||
@@ -28,45 +26,48 @@ import com.intellij.psi.*;
|
||||
import com.intellij.util.IncorrectOperationException;
|
||||
import org.jetbrains.annotations.NonNls;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
public class ExtendsListFix implements IntentionAction, LocalQuickFix {
|
||||
public class ExtendsListFix extends LocalQuickFixAndIntentionActionOnPsiElement {
|
||||
private static final Logger LOG = Logger.getInstance("#com.intellij.codeInsight.daemon.impl.quickfix.ExtendsListFix");
|
||||
|
||||
final PsiClass myClass;
|
||||
final PsiClass myClassToExtendFrom;
|
||||
private final boolean myToAdd;
|
||||
private final PsiClassType myTypeToExtendFrom;
|
||||
private final String myName;
|
||||
|
||||
public ExtendsListFix(@NotNull PsiClass aClass, @NotNull PsiClassType typeToExtendFrom, boolean toAdd) {
|
||||
myClass = aClass;
|
||||
myClassToExtendFrom = typeToExtendFrom.resolve();
|
||||
myTypeToExtendFrom = typeToExtendFrom;
|
||||
myToAdd = toAdd;
|
||||
this(aClass, typeToExtendFrom.resolve(), typeToExtendFrom, toAdd);
|
||||
}
|
||||
|
||||
public ExtendsListFix(@NotNull PsiClass aClass, @NotNull PsiClass classToExtendFrom, boolean toAdd) {
|
||||
myClass = aClass;
|
||||
myClassToExtendFrom = classToExtendFrom;
|
||||
myTypeToExtendFrom = JavaPsiFacade.getInstance(aClass.getProject()).getElementFactory().createType(classToExtendFrom);
|
||||
myToAdd = toAdd;
|
||||
this(aClass, classToExtendFrom, JavaPsiFacade.getInstance(aClass.getProject()).getElementFactory().createType(classToExtendFrom), toAdd);
|
||||
}
|
||||
|
||||
private ExtendsListFix(@NotNull PsiClass aClass,
|
||||
PsiClass classToExtendFrom,
|
||||
@NotNull PsiClassType typeToExtendFrom,
|
||||
boolean toAdd) {
|
||||
super(aClass);
|
||||
myClassToExtendFrom = classToExtendFrom;
|
||||
myToAdd = toAdd;
|
||||
myTypeToExtendFrom = typeToExtendFrom;
|
||||
|
||||
@NonNls final String messageKey;
|
||||
if (aClass.isInterface() == classToExtendFrom.isInterface()) {
|
||||
messageKey = toAdd ? "add.class.to.extends.list" : "remove.class.from.extends.list";
|
||||
}
|
||||
else {
|
||||
messageKey = toAdd ? "add.interface.to.implements.list" : "remove.interface.from.implements.list";
|
||||
}
|
||||
|
||||
myName = QuickFixBundle.message(messageKey, aClass.getName(), classToExtendFrom.getQualifiedName());
|
||||
}
|
||||
|
||||
|
||||
@NotNull
|
||||
public String getText() {
|
||||
@NonNls final String messageKey;
|
||||
if (myClass.isInterface() == myClassToExtendFrom.isInterface()) {
|
||||
messageKey = myToAdd ? "add.class.to.extends.list" : "remove.class.from.extends.list";
|
||||
}
|
||||
else {
|
||||
messageKey = myToAdd ? "add.interface.to.implements.list" : "remove.interface.from.implements.list";
|
||||
}
|
||||
|
||||
return QuickFixBundle.message(messageKey, myClass.getName(), myClassToExtendFrom.getQualifiedName());
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public String getName() {
|
||||
return getText();
|
||||
return myName;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@@ -74,27 +75,38 @@ public class ExtendsListFix implements IntentionAction, LocalQuickFix {
|
||||
return QuickFixBundle.message("change.extends.list.family");
|
||||
}
|
||||
|
||||
public void applyFix(@NotNull final Project project, @NotNull final ProblemDescriptor descriptor) {
|
||||
invoke(project, null, descriptor.getPsiElement().getContainingFile());
|
||||
}
|
||||
|
||||
public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile file) {
|
||||
@Override
|
||||
public boolean isAvailable(@NotNull Project project,
|
||||
@NotNull PsiFile file,
|
||||
@NotNull PsiElement startElement,
|
||||
@NotNull PsiElement endElement) {
|
||||
final PsiClass myClass = (PsiClass)startElement;
|
||||
return
|
||||
myClass != null
|
||||
&& myClass.isValid()
|
||||
&& myClass.getManager().isInProject(myClass)
|
||||
&& myClassToExtendFrom != null
|
||||
&& myClassToExtendFrom.isValid()
|
||||
&& !myClassToExtendFrom.hasModifierProperty(PsiModifier.FINAL)
|
||||
&& (myClassToExtendFrom.isInterface()
|
||||
|| (!myClass.isInterface()
|
||||
&& myClass.getExtendsList() != null
|
||||
&& myClass.getExtendsList().getReferencedTypes().length == 0 == myToAdd))
|
||||
myClass.isValid()
|
||||
&& myClass.getManager().isInProject(myClass)
|
||||
&& myClassToExtendFrom != null
|
||||
&& myClassToExtendFrom.isValid()
|
||||
&& !myClassToExtendFrom.hasModifierProperty(PsiModifier.FINAL)
|
||||
&& (myClassToExtendFrom.isInterface()
|
||||
|| !myClass.isInterface()
|
||||
&& myClass.getExtendsList() != null
|
||||
&& myClass.getExtendsList().getReferencedTypes().length == 0 == myToAdd)
|
||||
;
|
||||
|
||||
}
|
||||
|
||||
protected void invokeImpl () {
|
||||
@Override
|
||||
public void invoke(@NotNull Project project,
|
||||
@NotNull PsiFile file,
|
||||
@Nullable("is null when called from inspection") Editor editor,
|
||||
@NotNull PsiElement startElement,
|
||||
@NotNull PsiElement endElement) {
|
||||
final PsiClass myClass = (PsiClass)startElement;
|
||||
invokeImpl(myClass);
|
||||
UndoUtil.markPsiFileForUndo(file);
|
||||
}
|
||||
|
||||
protected void invokeImpl(PsiClass myClass) {
|
||||
if (!CodeInsightUtilBase.prepareFileForWrite(myClass.getContainingFile())) return;
|
||||
PsiReferenceList extendsList = !(myClass instanceof PsiTypeParameter) &&
|
||||
myClass.isInterface() != myClassToExtendFrom.isInterface() ?
|
||||
@@ -114,11 +126,6 @@ public class ExtendsListFix implements IntentionAction, LocalQuickFix {
|
||||
}
|
||||
}
|
||||
|
||||
public void invoke(@NotNull Project project, Editor editor, PsiFile file) {
|
||||
invokeImpl();
|
||||
UndoUtil.markPsiFileForUndo(file);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param position to add new class to or -1 if add to the end
|
||||
*/
|
||||
@@ -146,7 +153,7 @@ public class ExtendsListFix implements IntentionAction, LocalQuickFix {
|
||||
anchor = referenceElements[position - 1];
|
||||
}
|
||||
PsiJavaCodeReferenceElement classReferenceElement =
|
||||
JavaPsiFacade.getInstance(myClass.getProject()).getElementFactory().createReferenceElementByType(myTypeToExtendFrom);
|
||||
JavaPsiFacade.getInstance(extendsList.getProject()).getElementFactory().createReferenceElementByType(myTypeToExtendFrom);
|
||||
PsiElement element;
|
||||
if (anchor == null) {
|
||||
if (referenceElements.length == 0) {
|
||||
|
||||
+19
-7
@@ -20,7 +20,7 @@ import com.intellij.codeInsight.daemon.QuickFixBundle;
|
||||
import com.intellij.codeInsight.generation.OverrideImplementUtil;
|
||||
import com.intellij.codeInsight.generation.PsiMethodMember;
|
||||
import com.intellij.codeInsight.intention.impl.ImplementAbstractMethodHandler;
|
||||
import com.intellij.codeInspection.IntentionAndQuickFixAction;
|
||||
import com.intellij.codeInspection.LocalQuickFixAndIntentionActionOnPsiElement;
|
||||
import com.intellij.featureStatistics.FeatureUsageTracker;
|
||||
import com.intellij.featureStatistics.ProductivityFeatureNames;
|
||||
import com.intellij.ide.util.MemberChooser;
|
||||
@@ -39,14 +39,14 @@ import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.TreeMap;
|
||||
|
||||
public class ImplementMethodsFix extends IntentionAndQuickFixAction {
|
||||
private final PsiElement myPsiElement;
|
||||
public class ImplementMethodsFix extends LocalQuickFixAndIntentionActionOnPsiElement {
|
||||
public ImplementMethodsFix(PsiElement aClass) {
|
||||
myPsiElement = aClass;
|
||||
super(aClass);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public String getName() {
|
||||
@Override
|
||||
public String getText() {
|
||||
return QuickFixBundle.message("implement.methods.fix");
|
||||
}
|
||||
|
||||
@@ -55,11 +55,23 @@ public class ImplementMethodsFix extends IntentionAndQuickFixAction {
|
||||
return QuickFixBundle.message("implement.methods.fix");
|
||||
}
|
||||
|
||||
public boolean isAvailable(@NotNull final Project project, final Editor editor, final PsiFile file) {
|
||||
@Override
|
||||
public boolean isAvailable(@NotNull Project project,
|
||||
@NotNull PsiFile file,
|
||||
@NotNull PsiElement startElement,
|
||||
@NotNull PsiElement endElement) {
|
||||
PsiElement myPsiElement = startElement;
|
||||
return myPsiElement.isValid() && myPsiElement.getManager().isInProject(myPsiElement);
|
||||
}
|
||||
|
||||
public void applyFix(final Project project, final PsiFile file, @Nullable final Editor editor) {
|
||||
@Override
|
||||
public void invoke(@NotNull Project project,
|
||||
@NotNull PsiFile file,
|
||||
@Nullable("is null when called from inspection") final Editor editor,
|
||||
@NotNull PsiElement startElement,
|
||||
@NotNull PsiElement endElement) {
|
||||
final PsiElement myPsiElement = startElement;
|
||||
|
||||
if (editor == null || !CodeInsightUtilBase.prepareFileForWrite(myPsiElement.getContainingFile())) return;
|
||||
if (myPsiElement instanceof PsiEnumConstant) {
|
||||
FeatureUsageTracker.getInstance().triggerFeatureUsed(ProductivityFeatureNames.CODEASSISTS_OVERRIDE_IMPLEMENT);
|
||||
|
||||
+29
-10
@@ -17,7 +17,7 @@ package com.intellij.codeInsight.daemon.impl.quickfix;
|
||||
|
||||
import com.intellij.codeInsight.CodeInsightUtilBase;
|
||||
import com.intellij.codeInsight.daemon.QuickFixBundle;
|
||||
import com.intellij.codeInspection.IntentionAndQuickFixAction;
|
||||
import com.intellij.codeInspection.LocalQuickFixAndIntentionActionOnPsiElement;
|
||||
import com.intellij.openapi.command.undo.UndoUtil;
|
||||
import com.intellij.openapi.diagnostic.Logger;
|
||||
import com.intellij.openapi.editor.Editor;
|
||||
@@ -28,20 +28,22 @@ import com.intellij.util.IncorrectOperationException;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
public class MakeClassInterfaceFix extends IntentionAndQuickFixAction {
|
||||
public class MakeClassInterfaceFix extends LocalQuickFixAndIntentionActionOnPsiElement {
|
||||
private static final Logger LOG = Logger.getInstance("#com.intellij.codeInsight.daemon.impl.quickfix.MakeClassInterfaceFix");
|
||||
|
||||
private final PsiClass myClass;
|
||||
private final boolean myMakeInterface;
|
||||
private final String myName;
|
||||
|
||||
public MakeClassInterfaceFix(PsiClass aClass, final boolean makeInterface) {
|
||||
myClass = aClass;
|
||||
super(aClass);
|
||||
myMakeInterface = makeInterface;
|
||||
myName = aClass.getName();
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public String getName() {
|
||||
return QuickFixBundle.message(myMakeInterface? "make.class.an.interface.text":"make.interface.an.class.text", myClass.getName());
|
||||
@Override
|
||||
public String getText() {
|
||||
return QuickFixBundle.message(myMakeInterface? "make.class.an.interface.text":"make.interface.an.class.text", myName);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@@ -49,11 +51,23 @@ public class MakeClassInterfaceFix extends IntentionAndQuickFixAction {
|
||||
return QuickFixBundle.message("make.class.an.interface.family");
|
||||
}
|
||||
|
||||
public boolean isAvailable(@NotNull final Project project, final Editor editor, final PsiFile file) {
|
||||
@Override
|
||||
public boolean isAvailable(@NotNull Project project,
|
||||
@NotNull PsiFile file,
|
||||
@NotNull PsiElement startElement,
|
||||
@NotNull PsiElement endElement) {
|
||||
final PsiClass myClass = (PsiClass)startElement;
|
||||
|
||||
return myClass.isValid() && myClass.getManager().isInProject(myClass);
|
||||
}
|
||||
|
||||
public void applyFix(final Project project, final PsiFile file, @Nullable final Editor editor) {
|
||||
@Override
|
||||
public void invoke(@NotNull Project project,
|
||||
@NotNull PsiFile file,
|
||||
@Nullable("is null when called from inspection") Editor editor,
|
||||
@NotNull PsiElement startElement,
|
||||
@NotNull PsiElement endElement) {
|
||||
final PsiClass myClass = (PsiClass)startElement;
|
||||
if (!CodeInsightUtilBase.preparePsiElementForWrite(myClass)) return;
|
||||
try {
|
||||
final PsiReferenceList extendsList = myMakeInterface? myClass.getExtendsList() : myClass.getImplementsList();
|
||||
@@ -76,9 +90,9 @@ public class MakeClassInterfaceFix extends IntentionAndQuickFixAction {
|
||||
}
|
||||
}
|
||||
|
||||
private void convertPsiClass(PsiClass aClass, final boolean makeInterface) throws IncorrectOperationException {
|
||||
private static void convertPsiClass(PsiClass aClass, final boolean makeInterface) throws IncorrectOperationException {
|
||||
final IElementType lookFor = makeInterface? JavaTokenType.CLASS_KEYWORD : JavaTokenType.INTERFACE_KEYWORD;
|
||||
final PsiKeyword replaceWith = JavaPsiFacade.getInstance(myClass.getProject()).getElementFactory().createKeyword(makeInterface? PsiKeyword.INTERFACE : PsiKeyword.CLASS);
|
||||
final PsiKeyword replaceWith = JavaPsiFacade.getInstance(aClass.getProject()).getElementFactory().createKeyword(makeInterface? PsiKeyword.INTERFACE : PsiKeyword.CLASS);
|
||||
for (PsiElement psiElement : aClass.getChildren()) {
|
||||
if (psiElement instanceof PsiKeyword) {
|
||||
final PsiKeyword psiKeyword = (PsiKeyword)psiElement;
|
||||
@@ -89,4 +103,9 @@ public class MakeClassInterfaceFix extends IntentionAndQuickFixAction {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean startInWriteAction() {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
+29
-23
@@ -17,8 +17,7 @@ package com.intellij.codeInsight.daemon.impl.quickfix;
|
||||
|
||||
import com.intellij.codeInsight.CodeInsightUtilBase;
|
||||
import com.intellij.codeInsight.daemon.QuickFixBundle;
|
||||
import com.intellij.codeInspection.IntentionAndQuickFixAction;
|
||||
import com.intellij.openapi.application.ApplicationManager;
|
||||
import com.intellij.codeInspection.LocalQuickFixAndIntentionActionOnPsiElement;
|
||||
import com.intellij.openapi.command.undo.UndoUtil;
|
||||
import com.intellij.openapi.diagnostic.Logger;
|
||||
import com.intellij.openapi.editor.Editor;
|
||||
@@ -39,25 +38,27 @@ import org.jetbrains.annotations.Nullable;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class MethodParameterFix extends IntentionAndQuickFixAction {
|
||||
public class MethodParameterFix extends LocalQuickFixAndIntentionActionOnPsiElement {
|
||||
private static final Logger LOG = Logger.getInstance("#com.intellij.codeInsight.daemon.impl.quickfix.MethodReturnFix");
|
||||
|
||||
private final PsiMethod myMethod;
|
||||
private final PsiType myParameterType;
|
||||
private final int myIndex;
|
||||
private final boolean myFixWholeHierarchy;
|
||||
private final String myName;
|
||||
|
||||
public MethodParameterFix(PsiMethod method, PsiType type, int index, boolean fixWholeHierarchy) {
|
||||
myMethod = method;
|
||||
super(method);
|
||||
myParameterType = type;
|
||||
myIndex = index;
|
||||
myFixWholeHierarchy = fixWholeHierarchy;
|
||||
myName = method.getName();
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public String getName() {
|
||||
@Override
|
||||
public String getText() {
|
||||
return QuickFixBundle.message("fix.parameter.type.text",
|
||||
myMethod.getName(),
|
||||
myName,
|
||||
myParameterType.getCanonicalText() );
|
||||
}
|
||||
|
||||
@@ -66,9 +67,13 @@ public class MethodParameterFix extends IntentionAndQuickFixAction {
|
||||
return QuickFixBundle.message("fix.parameter.type.family");
|
||||
}
|
||||
|
||||
public boolean isAvailable(@NotNull final Project project, final Editor editor, final PsiFile file) {
|
||||
return myMethod != null
|
||||
&& myMethod.isValid()
|
||||
@Override
|
||||
public boolean isAvailable(@NotNull Project project,
|
||||
@NotNull PsiFile file,
|
||||
@NotNull PsiElement startElement,
|
||||
@NotNull PsiElement endElement) {
|
||||
final PsiMethod myMethod = (PsiMethod)startElement;
|
||||
return myMethod.isValid()
|
||||
&& myMethod.getManager().isInProject(myMethod)
|
||||
&& myParameterType != null
|
||||
&& !TypeConversionUtil.isNullType(myParameterType)
|
||||
@@ -76,7 +81,13 @@ public class MethodParameterFix extends IntentionAndQuickFixAction {
|
||||
&& !Comparing.equal(myParameterType, myMethod.getReturnType());
|
||||
}
|
||||
|
||||
public void applyFix(final Project project, final PsiFile file, @Nullable final Editor editor) {
|
||||
@Override
|
||||
public void invoke(@NotNull Project project,
|
||||
@NotNull PsiFile file,
|
||||
@Nullable("is null when called from inspection") Editor editor,
|
||||
@NotNull PsiElement startElement,
|
||||
@NotNull PsiElement endElement) {
|
||||
final PsiMethod myMethod = (PsiMethod)startElement;
|
||||
if (!CodeInsightUtilBase.prepareFileForWrite(myMethod.getContainingFile())) return;
|
||||
try {
|
||||
PsiMethod method = myMethod;
|
||||
@@ -90,14 +101,9 @@ public class MethodParameterFix extends IntentionAndQuickFixAction {
|
||||
false, null,
|
||||
method.getName(),
|
||||
method.getReturnType(),
|
||||
getNewParametersInfo());
|
||||
getNewParametersInfo(method));
|
||||
|
||||
if (ApplicationManager.getApplication().isUnitTestMode()) {
|
||||
processor.run();
|
||||
}
|
||||
else {
|
||||
processor.run();
|
||||
}
|
||||
processor.run();
|
||||
|
||||
|
||||
UndoUtil.markPsiFileForUndo(file);
|
||||
@@ -107,14 +113,14 @@ public class MethodParameterFix extends IntentionAndQuickFixAction {
|
||||
}
|
||||
}
|
||||
|
||||
private ParameterInfoImpl[] getNewParametersInfo() throws IncorrectOperationException {
|
||||
private ParameterInfoImpl[] getNewParametersInfo(PsiMethod method) throws IncorrectOperationException {
|
||||
List<ParameterInfoImpl> result = new ArrayList<ParameterInfoImpl>();
|
||||
PsiParameter[] parameters = myMethod.getParameterList().getParameters();
|
||||
PsiElementFactory factory = JavaPsiFacade.getInstance(myMethod.getProject()).getElementFactory();
|
||||
JavaCodeStyleManager codeStyleManager = JavaCodeStyleManager.getInstance(myMethod.getProject());
|
||||
PsiParameter[] parameters = method.getParameterList().getParameters();
|
||||
PsiElementFactory factory = JavaPsiFacade.getInstance(method.getProject()).getElementFactory();
|
||||
JavaCodeStyleManager codeStyleManager = JavaCodeStyleManager.getInstance(method.getProject());
|
||||
SuggestedNameInfo nameInfo = codeStyleManager.suggestVariableName(VariableKind.PARAMETER, null, null, myParameterType);
|
||||
PsiParameter newParameter = factory.createParameter(nameInfo.names[0], myParameterType);
|
||||
if (myMethod.getContainingClass().isInterface()) {
|
||||
if (method.getContainingClass().isInterface()) {
|
||||
PsiUtil.setModifierProperty(newParameter, PsiModifier.FINAL, false);
|
||||
}
|
||||
|
||||
|
||||
+35
-16
@@ -18,7 +18,7 @@ package com.intellij.codeInsight.daemon.impl.quickfix;
|
||||
import com.intellij.codeInsight.CodeInsightUtilBase;
|
||||
import com.intellij.codeInsight.daemon.QuickFixBundle;
|
||||
import com.intellij.codeInsight.daemon.impl.analysis.HighlightControlFlowUtil;
|
||||
import com.intellij.codeInspection.IntentionAndQuickFixAction;
|
||||
import com.intellij.codeInspection.LocalQuickFixAndIntentionActionOnPsiElement;
|
||||
import com.intellij.openapi.command.undo.UndoUtil;
|
||||
import com.intellij.openapi.diagnostic.Logger;
|
||||
import com.intellij.openapi.editor.Editor;
|
||||
@@ -52,22 +52,27 @@ import java.util.Collections;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
|
||||
public class MethodReturnTypeFix extends IntentionAndQuickFixAction {
|
||||
public class MethodReturnTypeFix extends LocalQuickFixAndIntentionActionOnPsiElement {
|
||||
private static final Logger LOG = Logger.getInstance("#com.intellij.codeInsight.daemon.impl.quickfix.MethodReturnBooleanFix");
|
||||
|
||||
private final PsiMethod myMethod;
|
||||
private final PsiType myReturnType;
|
||||
private final boolean myFixWholeHierarchy;
|
||||
private final String myName;
|
||||
private final String myCanonicalText;
|
||||
|
||||
public MethodReturnTypeFix(final PsiMethod method, final PsiType returnType, boolean fixWholeHierarchy) {
|
||||
myMethod = method;
|
||||
super(method);
|
||||
myReturnType = returnType;
|
||||
myFixWholeHierarchy = fixWholeHierarchy;
|
||||
myName = method.getName();
|
||||
myCanonicalText = returnType.getCanonicalText();
|
||||
}
|
||||
|
||||
|
||||
@NotNull
|
||||
public String getName() {
|
||||
return QuickFixBundle.message("fix.return.type.text", myMethod.getName(), myReturnType.getCanonicalText());
|
||||
@Override
|
||||
public String getText() {
|
||||
return QuickFixBundle.message("fix.return.type.text", myName, myCanonicalText);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@@ -75,9 +80,14 @@ public class MethodReturnTypeFix extends IntentionAndQuickFixAction {
|
||||
return QuickFixBundle.message("fix.return.type.family");
|
||||
}
|
||||
|
||||
public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile file) {
|
||||
return myMethod != null
|
||||
&& myMethod.isValid()
|
||||
@Override
|
||||
public boolean isAvailable(@NotNull Project project,
|
||||
@NotNull PsiFile file,
|
||||
@NotNull PsiElement startElement,
|
||||
@NotNull PsiElement endElement) {
|
||||
final PsiMethod myMethod = (PsiMethod)startElement;
|
||||
|
||||
return myMethod.isValid()
|
||||
&& myMethod.getManager().isInProject(myMethod)
|
||||
&& myReturnType != null
|
||||
&& myReturnType.isValid()
|
||||
@@ -91,14 +101,20 @@ public class MethodReturnTypeFix extends IntentionAndQuickFixAction {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void applyFix(Project project, PsiFile file, @Nullable Editor editor) {
|
||||
public void invoke(@NotNull Project project,
|
||||
@NotNull PsiFile file,
|
||||
Editor editor,
|
||||
@NotNull PsiElement startElement,
|
||||
@NotNull PsiElement endElement) {
|
||||
final PsiMethod myMethod = (PsiMethod)startElement;
|
||||
|
||||
if (!CodeInsightUtilBase.prepareFileForWrite(myMethod.getContainingFile())) return;
|
||||
if (myFixWholeHierarchy) {
|
||||
final PsiMethod superMethod = myMethod.findDeepestSuperMethod();
|
||||
final PsiType superReturnType = superMethod == null ? null : superMethod.getReturnType();
|
||||
if (superReturnType != null &&
|
||||
!Comparing.equal(myReturnType, superReturnType) &&
|
||||
!changeClassTypeArgument(project, superReturnType, superMethod.getContainingClass(), editor)) {
|
||||
!changeClassTypeArgument(myMethod, project, superReturnType, superMethod.getContainingClass(), editor)) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
@@ -117,8 +133,10 @@ public class MethodReturnTypeFix extends IntentionAndQuickFixAction {
|
||||
|
||||
final PsiReturnStatement latestReturn = returnSelector.getReturnStatement();
|
||||
if (latestReturn != null) {
|
||||
Editor editorForMethod = getEditorForMethod(project, editor, latestReturn.getContainingFile());
|
||||
selectReturnValueInEditor(latestReturn, editorForMethod);
|
||||
Editor editorForMethod = getEditorForMethod(myMethod, project, editor, latestReturn.getContainingFile());
|
||||
if (editorForMethod != null) {
|
||||
selectReturnValueInEditor(latestReturn, editorForMethod);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -189,7 +207,8 @@ public class MethodReturnTypeFix extends IntentionAndQuickFixAction {
|
||||
}
|
||||
}
|
||||
|
||||
private Editor getEditorForMethod(@NotNull final Project project, final Editor editor, final PsiFile file) {
|
||||
private static Editor getEditorForMethod(PsiMethod myMethod, @NotNull final Project project, final Editor editor, final PsiFile file) {
|
||||
|
||||
PsiFile containingFile = myMethod.getContainingFile();
|
||||
if (containingFile != file) {
|
||||
OpenFileDescriptor descriptor = new OpenFileDescriptor(project, containingFile.getVirtualFile());
|
||||
@@ -224,7 +243,7 @@ public class MethodReturnTypeFix extends IntentionAndQuickFixAction {
|
||||
methodSignatureChangeVisitor.addBase(targetMethod);
|
||||
ChangeSignatureProcessor processor = new UsagesAwareChangeSignatureProcessor(method.getProject(), targetMethod,
|
||||
false, null,
|
||||
method.getName(),
|
||||
myName,
|
||||
returnType,
|
||||
RemoveUnusedParameterFix.getNewParametersInfo(method, null),
|
||||
methodSignatureChangeVisitor);
|
||||
@@ -305,7 +324,7 @@ public class MethodReturnTypeFix extends IntentionAndQuickFixAction {
|
||||
editor.getSelectionModel().setSelection(range.getEndOffset(), range.getStartOffset());
|
||||
}
|
||||
|
||||
private boolean changeClassTypeArgument(Project project, PsiType superReturnType, PsiClass superClass, Editor editor) {
|
||||
private boolean changeClassTypeArgument(PsiMethod myMethod, Project project, PsiType superReturnType, PsiClass superClass, Editor editor) {
|
||||
if (superClass == null || !superClass.hasTypeParameters()) return true;
|
||||
final PsiClass superReturnTypeClass = PsiUtil.resolveClassInType(superReturnType);
|
||||
if (superReturnTypeClass == null || !(superReturnTypeClass instanceof PsiTypeParameter || superReturnTypeClass.hasTypeParameters())) return true;
|
||||
|
||||
+33
-17
@@ -17,41 +17,43 @@ package com.intellij.codeInsight.daemon.impl.quickfix;
|
||||
|
||||
import com.intellij.codeInsight.CodeInsightUtilBase;
|
||||
import com.intellij.codeInsight.daemon.QuickFixBundle;
|
||||
import com.intellij.codeInspection.IntentionAndQuickFixAction;
|
||||
import com.intellij.codeInspection.LocalQuickFixAndIntentionActionOnPsiElement;
|
||||
import com.intellij.openapi.command.undo.UndoUtil;
|
||||
import com.intellij.openapi.diagnostic.Logger;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.openapi.editor.Editor;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.psi.*;
|
||||
import com.intellij.psi.codeStyle.JavaCodeStyleManager;
|
||||
import com.intellij.psi.util.PsiFormatUtil;
|
||||
import com.intellij.psi.util.PsiFormatUtilBase;
|
||||
import com.intellij.util.IncorrectOperationException;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
public class MethodThrowsFix extends IntentionAndQuickFixAction {
|
||||
public class MethodThrowsFix extends LocalQuickFixAndIntentionActionOnPsiElement {
|
||||
private static final Logger LOG = Logger.getInstance("#com.intellij.codeInsight.daemon.impl.quickfix.MethodThrowsFix");
|
||||
|
||||
private final PsiMethod myMethod;
|
||||
private final String myThrowsCanonicalText;
|
||||
private final boolean myShouldThrow;
|
||||
private final boolean myShowContainingClass;
|
||||
private final String myMethodName;
|
||||
|
||||
public MethodThrowsFix(PsiMethod method, PsiClassType exceptionType, boolean shouldThrow, boolean showContainingClass) {
|
||||
myMethod = method;
|
||||
super(method);
|
||||
myThrowsCanonicalText = exceptionType.getCanonicalText();
|
||||
myShouldThrow = shouldThrow;
|
||||
myShowContainingClass = showContainingClass;
|
||||
myMethodName = PsiFormatUtil.formatMethod(method,
|
||||
PsiSubstitutor.EMPTY,
|
||||
PsiFormatUtilBase.SHOW_NAME | (showContainingClass ? PsiFormatUtilBase.SHOW_CONTAINING_CLASS
|
||||
: 0),
|
||||
0);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public String getName() {
|
||||
String methodName = PsiFormatUtil.formatMethod(myMethod,
|
||||
PsiSubstitutor.EMPTY,
|
||||
PsiFormatUtil.SHOW_NAME | (myShowContainingClass ? PsiFormatUtil.SHOW_CONTAINING_CLASS: 0),
|
||||
0);
|
||||
@Override
|
||||
public String getText() {
|
||||
return QuickFixBundle.message(myShouldThrow ? "fix.throws.list.add.exception" : "fix.throws.list.remove.exception",
|
||||
myThrowsCanonicalText,
|
||||
methodName);
|
||||
myMethodName);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@@ -59,13 +61,23 @@ public class MethodThrowsFix extends IntentionAndQuickFixAction {
|
||||
return QuickFixBundle.message("fix.throws.list.family");
|
||||
}
|
||||
|
||||
public boolean isAvailable(@NotNull final Project project, final Editor editor, final PsiFile file) {
|
||||
return myMethod != null
|
||||
&& myMethod.isValid()
|
||||
@Override
|
||||
public boolean isAvailable(@NotNull Project project,
|
||||
@NotNull PsiFile file,
|
||||
@NotNull PsiElement startElement,
|
||||
@NotNull PsiElement endElement) {
|
||||
final PsiMethod myMethod = (PsiMethod)startElement;
|
||||
return myMethod.isValid()
|
||||
&& myMethod.getManager().isInProject(myMethod);
|
||||
}
|
||||
|
||||
public void applyFix(final Project project, final PsiFile file, final Editor editor) {
|
||||
@Override
|
||||
public void invoke(@NotNull Project project,
|
||||
@NotNull PsiFile file,
|
||||
@Nullable("is null when called from inspection") Editor editor,
|
||||
@NotNull PsiElement startElement,
|
||||
@NotNull PsiElement endElement) {
|
||||
final PsiMethod myMethod = (PsiMethod)startElement;
|
||||
if (!CodeInsightUtilBase.prepareFileForWrite(myMethod.getContainingFile())) return;
|
||||
PsiJavaCodeReferenceElement[] referenceElements = myMethod.getThrowsList().getReferenceElements();
|
||||
try {
|
||||
@@ -92,4 +104,8 @@ public class MethodThrowsFix extends IntentionAndQuickFixAction {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean startInWriteAction() {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,7 +17,7 @@ package com.intellij.codeInsight.daemon.impl.quickfix;
|
||||
|
||||
import com.intellij.codeInsight.CodeInsightUtilBase;
|
||||
import com.intellij.codeInsight.daemon.QuickFixBundle;
|
||||
import com.intellij.codeInspection.IntentionAndQuickFixAction;
|
||||
import com.intellij.codeInspection.LocalQuickFixAndIntentionActionOnPsiElement;
|
||||
import com.intellij.openapi.application.ApplicationManager;
|
||||
import com.intellij.openapi.command.undo.UndoUtil;
|
||||
import com.intellij.openapi.diagnostic.Logger;
|
||||
@@ -32,41 +32,52 @@ import com.intellij.psi.search.searches.OverridingMethodsSearch;
|
||||
import com.intellij.psi.util.PsiFormatUtil;
|
||||
import com.intellij.psi.util.PsiFormatUtilBase;
|
||||
import com.intellij.psi.util.PsiUtil;
|
||||
import com.intellij.util.VisibilityUtil;
|
||||
import com.intellij.util.IncorrectOperationException;
|
||||
import com.intellij.util.VisibilityUtil;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class ModifierFix extends IntentionAndQuickFixAction {
|
||||
public class ModifierFix extends LocalQuickFixAndIntentionActionOnPsiElement {
|
||||
private static final Logger LOG = Logger.getInstance("#com.intellij.codeInsight.daemon.impl.quickfix.ModifierFix");
|
||||
|
||||
private final PsiModifierList myModifierList;
|
||||
@Modifier private final String myModifier;
|
||||
private final boolean myShouldHave;
|
||||
private final boolean myShowContainingClass;
|
||||
private PsiVariable myVariable;
|
||||
private final String myName;
|
||||
private final SmartPsiElementPointer<PsiVariable> myVariable;
|
||||
|
||||
public ModifierFix(PsiModifierList modifierList, @Modifier @NotNull String modifier, boolean shouldHave, boolean showContainingClass) {
|
||||
myModifierList = modifierList;
|
||||
super(modifierList);
|
||||
myModifier = modifier;
|
||||
myShouldHave = shouldHave;
|
||||
myShowContainingClass = showContainingClass;
|
||||
myName = format(null, modifierList);
|
||||
myVariable = null;
|
||||
}
|
||||
|
||||
public ModifierFix(@NotNull PsiModifierListOwner owner, @Modifier @NotNull String modifier, boolean shouldHave, boolean showContainingClass) {
|
||||
this(owner.getModifierList(), modifier, shouldHave, showContainingClass);
|
||||
if (owner instanceof PsiVariable) {
|
||||
myVariable = (PsiVariable)owner;
|
||||
}
|
||||
super(owner.getModifierList());
|
||||
myModifier = modifier;
|
||||
myShouldHave = shouldHave;
|
||||
myShowContainingClass = showContainingClass;
|
||||
PsiVariable variable = owner instanceof PsiVariable ? (PsiVariable)owner : null;
|
||||
myName = format(variable, owner.getModifierList());
|
||||
|
||||
myVariable = variable == null ? null : SmartPointerManager.getInstance(owner.getProject()).createSmartPsiElementPointer(variable);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public String getName() {
|
||||
@Override
|
||||
public String getText() {
|
||||
return myName;
|
||||
}
|
||||
|
||||
private String format(PsiVariable variable, PsiModifierList modifierList) {
|
||||
String name = null;
|
||||
PsiElement parent = myVariable == null ? myModifierList == null ? null : myModifierList.getParent() : myVariable;
|
||||
PsiElement parent = variable == null ? modifierList == null ? null : modifierList.getParent() : variable;
|
||||
if (parent instanceof PsiClass) {
|
||||
name = ((PsiClass)parent).getName();
|
||||
}
|
||||
@@ -98,12 +109,17 @@ public class ModifierFix extends IntentionAndQuickFixAction {
|
||||
return QuickFixBundle.message("fix.modifiers.family");
|
||||
}
|
||||
|
||||
public boolean isAvailable(@NotNull final Project project, final Editor editor, final PsiFile file) {
|
||||
return myModifierList != null &&
|
||||
myModifierList.isValid() &&
|
||||
@Override
|
||||
public boolean isAvailable(@NotNull Project project,
|
||||
@NotNull PsiFile file,
|
||||
@NotNull PsiElement startElement,
|
||||
@NotNull PsiElement endElement) {
|
||||
final PsiModifierList myModifierList = (PsiModifierList)startElement;
|
||||
PsiVariable variable = myVariable == null ? null : myVariable.getElement();
|
||||
return myModifierList.isValid() &&
|
||||
myModifierList.getManager().isInProject(myModifierList) &&
|
||||
myModifierList.hasExplicitModifier(myModifier) != myShouldHave &&
|
||||
(myVariable == null || myVariable.isValid());
|
||||
(variable == null || variable.isValid());
|
||||
}
|
||||
|
||||
private void changeModifierList (PsiModifierList modifierList) {
|
||||
@@ -115,23 +131,30 @@ public class ModifierFix extends IntentionAndQuickFixAction {
|
||||
}
|
||||
}
|
||||
|
||||
public void applyFix(final Project project, final PsiFile file, @Nullable final Editor editor) {
|
||||
@Override
|
||||
public void invoke(@NotNull Project project,
|
||||
@NotNull PsiFile file,
|
||||
@Nullable("is null when called from inspection") Editor editor,
|
||||
@NotNull PsiElement startElement,
|
||||
@NotNull PsiElement endElement) {
|
||||
final PsiModifierList myModifierList = (PsiModifierList)startElement;
|
||||
final PsiVariable variable = myVariable == null ? null : myVariable.getElement();
|
||||
if (!CodeInsightUtilBase.preparePsiElementForWrite(myModifierList)) return;
|
||||
final List<PsiModifierList> modifierLists = new ArrayList<PsiModifierList>();
|
||||
final PsiFile containingFile = myModifierList.getContainingFile();
|
||||
final PsiModifierList modifierList;
|
||||
if (myVariable != null && myVariable.isValid()) {
|
||||
if (variable != null && variable.isValid()) {
|
||||
ApplicationManager.getApplication().runWriteAction(new Runnable() {
|
||||
public void run() {
|
||||
try {
|
||||
myVariable.normalizeDeclaration();
|
||||
variable.normalizeDeclaration();
|
||||
}
|
||||
catch (IncorrectOperationException e) {
|
||||
LOG.error(e);
|
||||
}
|
||||
}
|
||||
});
|
||||
modifierList = myVariable.getModifierList();
|
||||
modifierList = variable.getModifierList();
|
||||
assert modifierList != null;
|
||||
}
|
||||
else {
|
||||
|
||||
+24
-14
@@ -22,25 +22,25 @@ import com.intellij.openapi.command.undo.UndoUtil;
|
||||
import com.intellij.openapi.diagnostic.Logger;
|
||||
import com.intellij.openapi.editor.Editor;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.psi.PsiClass;
|
||||
import com.intellij.psi.PsiClassType;
|
||||
import com.intellij.psi.PsiFile;
|
||||
import com.intellij.psi.PsiReferenceList;
|
||||
import com.intellij.psi.*;
|
||||
import com.intellij.util.IncorrectOperationException;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
public class MoveBoundClassToFrontFix extends ExtendsListFix {
|
||||
private static final Logger LOG = Logger.getInstance("#com.intellij.codeInsight.daemon.impl.quickfix.MoveBoundClassToFrontFix");
|
||||
private final String myName;
|
||||
|
||||
public MoveBoundClassToFrontFix(PsiClass aClass, PsiClassType classToExtendFrom) {
|
||||
super(aClass, classToExtendFrom, true);
|
||||
myName = QuickFixBundle.message("move.bound.class.to.front.fix.text",
|
||||
HighlightUtil.formatClass(myClassToExtendFrom),
|
||||
HighlightUtil.formatClass(aClass));
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public String getText() {
|
||||
return QuickFixBundle.message("move.bound.class.to.front.fix.text",
|
||||
HighlightUtil.formatClass(myClassToExtendFrom),
|
||||
HighlightUtil.formatClass(myClass));
|
||||
return myName;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@@ -48,7 +48,13 @@ public class MoveBoundClassToFrontFix extends ExtendsListFix {
|
||||
return QuickFixBundle.message("move.class.in.extend.list.family");
|
||||
}
|
||||
|
||||
public void invoke(@NotNull Project project, Editor editor, PsiFile file) {
|
||||
@Override
|
||||
public void invoke(@NotNull Project project,
|
||||
@NotNull PsiFile file,
|
||||
@Nullable("is null when called from inspection") Editor editor,
|
||||
@NotNull PsiElement startElement,
|
||||
@NotNull PsiElement endElement) {
|
||||
final PsiClass myClass = (PsiClass)startElement;
|
||||
if (!CodeInsightUtilBase.prepareFileForWrite(myClass.getContainingFile())) return;
|
||||
PsiReferenceList extendsList = myClass.getExtendsList();
|
||||
if (extendsList == null) return;
|
||||
@@ -62,13 +68,17 @@ public class MoveBoundClassToFrontFix extends ExtendsListFix {
|
||||
UndoUtil.markPsiFileForUndo(file);
|
||||
}
|
||||
|
||||
public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile file) {
|
||||
@Override
|
||||
public boolean isAvailable(@NotNull Project project,
|
||||
@NotNull PsiFile file,
|
||||
@NotNull PsiElement startElement,
|
||||
@NotNull PsiElement endElement) {
|
||||
final PsiClass myClass = (PsiClass)startElement;
|
||||
return
|
||||
myClass != null
|
||||
&& myClass.isValid()
|
||||
&& myClass.getManager().isInProject(myClass)
|
||||
&& myClassToExtendFrom != null
|
||||
&& myClassToExtendFrom.isValid()
|
||||
myClass.isValid()
|
||||
&& myClass.getManager().isInProject(myClass)
|
||||
&& myClassToExtendFrom != null
|
||||
&& myClassToExtendFrom.isValid()
|
||||
;
|
||||
}
|
||||
}
|
||||
|
||||
+28
-20
@@ -17,11 +17,11 @@ package com.intellij.codeInsight.daemon.impl.quickfix;
|
||||
|
||||
import com.intellij.codeInsight.CodeInsightUtilBase;
|
||||
import com.intellij.codeInsight.daemon.QuickFixBundle;
|
||||
import com.intellij.codeInspection.IntentionAndQuickFixAction;
|
||||
import com.intellij.openapi.application.ApplicationManager;
|
||||
import com.intellij.codeInspection.LocalQuickFixAndIntentionActionOnPsiElement;
|
||||
import com.intellij.openapi.editor.Editor;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.openapi.util.Comparing;
|
||||
import com.intellij.psi.PsiElement;
|
||||
import com.intellij.psi.PsiFile;
|
||||
import com.intellij.psi.PsiMethod;
|
||||
import com.intellij.psi.PsiParameter;
|
||||
@@ -33,16 +33,19 @@ import org.jetbrains.annotations.Nullable;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class RemoveUnusedParameterFix extends IntentionAndQuickFixAction {
|
||||
private final PsiParameter myParameter;
|
||||
public class RemoveUnusedParameterFix extends LocalQuickFixAndIntentionActionOnPsiElement {
|
||||
|
||||
private final String myName;
|
||||
|
||||
public RemoveUnusedParameterFix(PsiParameter parameter) {
|
||||
myParameter = parameter;
|
||||
super(parameter);
|
||||
myName = parameter.getName();
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public String getName() {
|
||||
return QuickFixBundle.message("remove.unused.parameter.text", myParameter.getName());
|
||||
@Override
|
||||
public String getText() {
|
||||
return QuickFixBundle.message("remove.unused.parameter.text", myName);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@@ -50,14 +53,25 @@ public class RemoveUnusedParameterFix extends IntentionAndQuickFixAction {
|
||||
return QuickFixBundle.message("remove.unused.parameter.family");
|
||||
}
|
||||
|
||||
public boolean isAvailable(@NotNull final Project project, final Editor editor, final PsiFile file) {
|
||||
@Override
|
||||
public boolean isAvailable(@NotNull Project project,
|
||||
@NotNull PsiFile file,
|
||||
@NotNull PsiElement startElement,
|
||||
@NotNull PsiElement endElement) {
|
||||
final PsiParameter myParameter = (PsiParameter)startElement;
|
||||
return
|
||||
myParameter.isValid()
|
||||
&& myParameter.getDeclarationScope() instanceof PsiMethod
|
||||
&& myParameter.getManager().isInProject(myParameter);
|
||||
}
|
||||
|
||||
public void applyFix(final Project project, final PsiFile file, @Nullable final Editor editor) {
|
||||
@Override
|
||||
public void invoke(@NotNull Project project,
|
||||
@NotNull PsiFile file,
|
||||
@Nullable("is null when called from inspection") Editor editor,
|
||||
@NotNull PsiElement startElement,
|
||||
@NotNull PsiElement endElement) {
|
||||
final PsiParameter myParameter = (PsiParameter)startElement;
|
||||
if (!CodeInsightUtilBase.prepareFileForWrite(myParameter.getContainingFile())) return;
|
||||
removeReferences(myParameter);
|
||||
}
|
||||
@@ -66,17 +80,11 @@ public class RemoveUnusedParameterFix extends IntentionAndQuickFixAction {
|
||||
PsiMethod method = (PsiMethod) parameter.getDeclarationScope();
|
||||
ChangeSignatureProcessor processor = new ChangeSignatureProcessor(parameter.getProject(),
|
||||
method,
|
||||
false, null,
|
||||
method.getName(),
|
||||
method.getReturnType(),
|
||||
getNewParametersInfo(method, parameter));
|
||||
|
||||
if (ApplicationManager.getApplication().isUnitTestMode()) {
|
||||
processor.run();
|
||||
}
|
||||
else {
|
||||
processor.run();
|
||||
}
|
||||
false, null,
|
||||
method.getName(),
|
||||
method.getReturnType(),
|
||||
getNewParametersInfo(method, parameter));
|
||||
processor.run();
|
||||
}
|
||||
|
||||
public static ParameterInfoImpl[] getNewParametersInfo(PsiMethod method, PsiParameter parameterToRemove) {
|
||||
|
||||
+103
-71
@@ -17,64 +17,86 @@ package com.intellij.codeInsight.daemon.impl.quickfix;
|
||||
|
||||
import com.intellij.codeInsight.CodeInsightUtilBase;
|
||||
import com.intellij.codeInsight.daemon.QuickFixBundle;
|
||||
import com.intellij.codeInsight.intention.IntentionAction;
|
||||
import com.intellij.codeInspection.IntentionAndQuickFixAction;
|
||||
import com.intellij.codeInspection.LocalQuickFixAndIntentionActionOnPsiElement;
|
||||
import com.intellij.openapi.command.undo.UndoUtil;
|
||||
import com.intellij.openapi.diagnostic.Logger;
|
||||
import com.intellij.openapi.editor.Editor;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.psi.*;
|
||||
import com.intellij.psi.codeStyle.JavaCodeStyleManager;
|
||||
import com.intellij.util.IncorrectOperationException;
|
||||
import org.jetbrains.annotations.NonNls;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
public class VariableArrayTypeFix extends IntentionAndQuickFixAction {
|
||||
private static final Logger LOG = Logger.getInstance("#com.intellij.codeInsight.daemon.impl.quickfix.VariableArrayTypeFix");
|
||||
public class VariableArrayTypeFix extends LocalQuickFixAndIntentionActionOnPsiElement {
|
||||
|
||||
private final PsiVariable myVariable;
|
||||
/**
|
||||
* only for the case when in same statement with initialization
|
||||
*/
|
||||
@Nullable
|
||||
private final PsiNewExpression myNewExpression;
|
||||
@NotNull
|
||||
private final PsiArrayInitializerExpression myInitializer;
|
||||
@NotNull
|
||||
private final PsiArrayType myTargetType;
|
||||
private final String myName;
|
||||
private final String myFamilyName;
|
||||
|
||||
public VariableArrayTypeFix(@NotNull PsiArrayInitializerExpression initializer, @NotNull PsiType componentType) {
|
||||
super(getInitializer(initializer));
|
||||
PsiArrayType arrayType = new PsiArrayType(componentType);
|
||||
PsiArrayInitializerExpression arrayInitializer = initializer;
|
||||
while (arrayInitializer.getParent() instanceof PsiArrayInitializerExpression) {
|
||||
arrayInitializer = (PsiArrayInitializerExpression) arrayInitializer.getParent();
|
||||
arrayInitializer = (PsiArrayInitializerExpression)arrayInitializer.getParent();
|
||||
arrayType = new PsiArrayType(arrayType);
|
||||
}
|
||||
|
||||
myInitializer = arrayInitializer;
|
||||
myTargetType = arrayType;
|
||||
|
||||
PsiNewExpression newExpressionLocal = null;
|
||||
PsiVariable variableLocal = null;
|
||||
PsiExpression myNewExpression = getNewExpressionLocal(arrayInitializer);
|
||||
PsiVariable myVariable = getVariableLocal(arrayInitializer);
|
||||
myName = myVariable == null ? null : myTargetType.equals(myVariable.getType()) && myNewExpression != null ?
|
||||
QuickFixBundle.message("change.new.operator.type.text", getNewText(myNewExpression,arrayInitializer), myTargetType.getCanonicalText(), "") :
|
||||
QuickFixBundle.message("fix.variable.type.text", myVariable.getName(), myTargetType.getCanonicalText());
|
||||
myFamilyName = myVariable == null ? null : myTargetType.equals(myVariable.getType()) && myNewExpression != null ?
|
||||
QuickFixBundle.message("change.new.operator.type.family") :
|
||||
QuickFixBundle.message("fix.variable.type.family");
|
||||
}
|
||||
|
||||
final PsiElement parent = myInitializer.getParent();
|
||||
if (parent instanceof PsiVariable) {
|
||||
variableLocal = (PsiVariable) parent;
|
||||
} else if (parent instanceof PsiNewExpression) {
|
||||
newExpressionLocal = (PsiNewExpression) parent;
|
||||
final PsiElement newParent = newExpressionLocal.getParent();
|
||||
if (newParent instanceof PsiAssignmentExpression) {
|
||||
variableLocal = getFromAssignment((PsiAssignmentExpression) newParent);
|
||||
} else if (newParent instanceof PsiVariable) {
|
||||
variableLocal = (PsiVariable) newParent;
|
||||
}
|
||||
} else if (parent instanceof PsiAssignmentExpression) {
|
||||
variableLocal = getFromAssignment((PsiAssignmentExpression)parent);
|
||||
private static PsiArrayInitializerExpression getInitializer(PsiArrayInitializerExpression initializer) {
|
||||
PsiArrayInitializerExpression arrayInitializer = initializer;
|
||||
while (arrayInitializer.getParent() instanceof PsiArrayInitializerExpression) {
|
||||
arrayInitializer = (PsiArrayInitializerExpression)arrayInitializer.getParent();
|
||||
}
|
||||
|
||||
myNewExpression = newExpressionLocal;
|
||||
myVariable = variableLocal;
|
||||
return arrayInitializer;
|
||||
}
|
||||
|
||||
private static PsiVariable getVariableLocal(PsiArrayInitializerExpression initializer) {
|
||||
PsiVariable variableLocal = null;
|
||||
|
||||
final PsiElement parent = initializer.getParent();
|
||||
if (parent instanceof PsiVariable) {
|
||||
variableLocal = (PsiVariable)parent;
|
||||
}
|
||||
else if (parent instanceof PsiNewExpression) {
|
||||
PsiNewExpression newExpressionLocal = (PsiNewExpression)parent;
|
||||
final PsiElement newParent = newExpressionLocal.getParent();
|
||||
if (newParent instanceof PsiAssignmentExpression) {
|
||||
variableLocal = getFromAssignment((PsiAssignmentExpression)newParent);
|
||||
}
|
||||
else if (newParent instanceof PsiVariable) {
|
||||
variableLocal = (PsiVariable)newParent;
|
||||
}
|
||||
}
|
||||
else if (parent instanceof PsiAssignmentExpression) {
|
||||
variableLocal = getFromAssignment((PsiAssignmentExpression)parent);
|
||||
}
|
||||
return variableLocal;
|
||||
}
|
||||
|
||||
private static PsiNewExpression getNewExpressionLocal(PsiArrayInitializerExpression initializer) {
|
||||
PsiNewExpression newExpressionLocal = null;
|
||||
|
||||
final PsiElement parent = initializer.getParent();
|
||||
if (parent instanceof PsiVariable) {
|
||||
|
||||
} else if (parent instanceof PsiNewExpression) {
|
||||
newExpressionLocal = (PsiNewExpression) parent;
|
||||
}
|
||||
|
||||
return newExpressionLocal;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@@ -84,7 +106,7 @@ public class VariableArrayTypeFix extends IntentionAndQuickFixAction {
|
||||
return referencedElement != null && referencedElement instanceof PsiVariable ? (PsiVariable)referencedElement : null;
|
||||
}
|
||||
|
||||
private String getNewText() {
|
||||
private static String getNewText(PsiElement myNewExpression, PsiArrayInitializerExpression myInitializer) {
|
||||
final String newText = myNewExpression.getText();
|
||||
final int initializerIdx = newText.indexOf(myInitializer.getText());
|
||||
if (initializerIdx != -1) {
|
||||
@@ -95,55 +117,65 @@ public class VariableArrayTypeFix extends IntentionAndQuickFixAction {
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public String getName() {
|
||||
return myTargetType.equals(myVariable.getType()) && myNewExpression != null ?
|
||||
QuickFixBundle.message("change.new.operator.type.text", getNewText(), myTargetType.getCanonicalText(), "") :
|
||||
QuickFixBundle.message("fix.variable.type.text", myVariable.getName(), myTargetType.getCanonicalText());
|
||||
public String getText() {
|
||||
return myName;
|
||||
}
|
||||
|
||||
|
||||
|
||||
@NotNull
|
||||
public String getFamilyName() {
|
||||
return myTargetType.equals(myVariable.getType()) && myNewExpression != null ?
|
||||
QuickFixBundle.message("change.new.operator.type.family") :
|
||||
QuickFixBundle.message("fix.variable.type.family");
|
||||
}
|
||||
|
||||
public boolean isAvailable(@NotNull final Project project, final Editor editor, final PsiFile file) {
|
||||
return myVariable != null && myVariable.isValid()
|
||||
&& myVariable.getManager().isInProject(myVariable)
|
||||
&& myTargetType.isValid()
|
||||
&& myInitializer.isValid();
|
||||
return myFamilyName;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void applyFix(Project project, PsiFile file, @Nullable Editor editor) {
|
||||
public boolean isAvailable(@NotNull Project project,
|
||||
@NotNull PsiFile file,
|
||||
@NotNull PsiElement startElement,
|
||||
@NotNull PsiElement endElement) {
|
||||
final PsiArrayInitializerExpression myInitializer = (PsiArrayInitializerExpression)startElement;
|
||||
final PsiVariable myVariable = getVariableLocal(myInitializer);
|
||||
|
||||
return myVariable != null
|
||||
&& myVariable.isValid()
|
||||
&& myVariable.getManager().isInProject(myVariable)
|
||||
&& myTargetType.isValid()
|
||||
&& myInitializer.isValid();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void invoke(@NotNull Project project,
|
||||
@NotNull PsiFile file,
|
||||
@Nullable("is null when called from inspection") Editor editor,
|
||||
@NotNull PsiElement startElement,
|
||||
@NotNull PsiElement endElement) {
|
||||
final PsiArrayInitializerExpression myInitializer = (PsiArrayInitializerExpression)startElement;
|
||||
final PsiVariable myVariable = getVariableLocal(myInitializer);
|
||||
if (myVariable == null) return;
|
||||
/**
|
||||
* only for the case when in same statement with initialization
|
||||
*/
|
||||
final PsiNewExpression myNewExpression = getNewExpressionLocal(myInitializer);
|
||||
|
||||
if (!CodeInsightUtilBase.prepareFileForWrite(myVariable.getContainingFile())) return;
|
||||
try {
|
||||
final PsiElementFactory factory = JavaPsiFacade.getInstance(file.getProject()).getElementFactory();
|
||||
final PsiElementFactory factory = JavaPsiFacade.getInstance(file.getProject()).getElementFactory();
|
||||
|
||||
if (! myTargetType.equals(myVariable.getType())) {
|
||||
myVariable.normalizeDeclaration();
|
||||
myVariable.getTypeElement().replace(factory.createTypeElement(myTargetType));
|
||||
JavaCodeStyleManager.getInstance(project).shortenClassReferences(myVariable);
|
||||
if (! myTargetType.equals(myVariable.getType())) {
|
||||
myVariable.normalizeDeclaration();
|
||||
myVariable.getTypeElement().replace(factory.createTypeElement(myTargetType));
|
||||
JavaCodeStyleManager.getInstance(project).shortenClassReferences(myVariable);
|
||||
|
||||
if (! myVariable.getContainingFile().equals(file)) {
|
||||
UndoUtil.markPsiFileForUndo(myVariable.getContainingFile());
|
||||
}
|
||||
if (! myVariable.getContainingFile().equals(file)) {
|
||||
UndoUtil.markPsiFileForUndo(myVariable.getContainingFile());
|
||||
}
|
||||
}
|
||||
|
||||
if (myNewExpression != null) {
|
||||
if (!CodeInsightUtilBase.prepareFileForWrite(file)) return;
|
||||
if (myNewExpression != null) {
|
||||
if (!CodeInsightUtilBase.prepareFileForWrite(file)) return;
|
||||
|
||||
@NonNls String text = "new " + myTargetType.getCanonicalText() + "{}";
|
||||
final PsiNewExpression newExpression = (PsiNewExpression) factory.createExpressionFromText(text, myNewExpression.getParent());
|
||||
final PsiElement[] children = newExpression.getChildren();
|
||||
children[children.length - 1].replace(myInitializer);
|
||||
myNewExpression.replace(newExpression);
|
||||
}
|
||||
} catch (IncorrectOperationException e) {
|
||||
LOG.error(e);
|
||||
@NonNls String text = "new " + myTargetType.getCanonicalText() + "{}";
|
||||
final PsiNewExpression newExpression = (PsiNewExpression) factory.createExpressionFromText(text, myNewExpression.getParent());
|
||||
final PsiElement[] children = newExpression.getChildren();
|
||||
children[children.length - 1].replace(myInitializer);
|
||||
myNewExpression.replace(newExpression);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+26
-22
@@ -17,8 +17,7 @@ package com.intellij.codeInsight.daemon.impl.quickfix;
|
||||
|
||||
import com.intellij.codeInsight.CodeInsightUtilBase;
|
||||
import com.intellij.codeInsight.daemon.QuickFixBundle;
|
||||
import com.intellij.codeInsight.intention.IntentionAction;
|
||||
import com.intellij.codeInspection.IntentionAndQuickFixAction;
|
||||
import com.intellij.codeInspection.LocalQuickFixAndIntentionActionOnPsiElement;
|
||||
import com.intellij.openapi.command.undo.UndoUtil;
|
||||
import com.intellij.openapi.diagnostic.Logger;
|
||||
import com.intellij.openapi.editor.Editor;
|
||||
@@ -30,23 +29,23 @@ import com.intellij.util.IncorrectOperationException;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
public class VariableTypeFix extends IntentionAndQuickFixAction {
|
||||
public class VariableTypeFix extends LocalQuickFixAndIntentionActionOnPsiElement {
|
||||
static final Logger LOG = Logger.getInstance("#com.intellij.codeInsight.daemon.impl.quickfix.VariableTypeFix");
|
||||
|
||||
private final PsiVariable myVariable;
|
||||
private final PsiType myReturnType;
|
||||
protected final String myName;
|
||||
|
||||
public VariableTypeFix(PsiVariable variable, PsiType toReturn) {
|
||||
myVariable = variable;
|
||||
public VariableTypeFix(@NotNull PsiVariable variable, PsiType toReturn) {
|
||||
super(variable);
|
||||
myReturnType = toReturn != null ? GenericsUtil.getVariableTypeByExpressionType(toReturn) : null;
|
||||
myName = variable.getName();
|
||||
}
|
||||
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public String getName() {
|
||||
public String getText() {
|
||||
return QuickFixBundle.message("fix.variable.type.text",
|
||||
getVariable().getName(),
|
||||
myName,
|
||||
getReturnType().getCanonicalText());
|
||||
}
|
||||
|
||||
@@ -55,10 +54,14 @@ public class VariableTypeFix extends IntentionAndQuickFixAction {
|
||||
return QuickFixBundle.message("fix.variable.type.family");
|
||||
}
|
||||
|
||||
public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile file) {
|
||||
return getVariable() != null
|
||||
&& getVariable().isValid()
|
||||
&& getVariable().getManager().isInProject(getVariable())
|
||||
@Override
|
||||
public boolean isAvailable(@NotNull Project project,
|
||||
@NotNull PsiFile file,
|
||||
@NotNull PsiElement startElement,
|
||||
@NotNull PsiElement endElement) {
|
||||
final PsiVariable myVariable = (PsiVariable)startElement;
|
||||
return myVariable.isValid()
|
||||
&& myVariable.getManager().isInProject(myVariable)
|
||||
&& getReturnType() != null
|
||||
&& getReturnType().isValid()
|
||||
&& !TypeConversionUtil.isNullType(getReturnType())
|
||||
@@ -66,13 +69,18 @@ public class VariableTypeFix extends IntentionAndQuickFixAction {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void applyFix(Project project, PsiFile file, @Nullable Editor editor) {
|
||||
if (!CodeInsightUtilBase.prepareFileForWrite(getVariable().getContainingFile())) return;
|
||||
public void invoke(@NotNull Project project,
|
||||
@NotNull PsiFile file,
|
||||
@Nullable("is null when called from inspection") Editor editor,
|
||||
@NotNull PsiElement startElement,
|
||||
@NotNull PsiElement endElement) {
|
||||
final PsiVariable myVariable = (PsiVariable)startElement;
|
||||
if (!CodeInsightUtilBase.prepareFileForWrite(myVariable.getContainingFile())) return;
|
||||
try {
|
||||
getVariable().normalizeDeclaration();
|
||||
getVariable().getTypeElement().replace(JavaPsiFacade.getInstance(file.getProject()).getElementFactory().createTypeElement(
|
||||
myVariable.normalizeDeclaration();
|
||||
myVariable.getTypeElement().replace(JavaPsiFacade.getInstance(file.getProject()).getElementFactory().createTypeElement(
|
||||
getReturnType()));
|
||||
JavaCodeStyleManager.getInstance(project).shortenClassReferences(getVariable());
|
||||
JavaCodeStyleManager.getInstance(project).shortenClassReferences(myVariable);
|
||||
UndoUtil.markPsiFileForUndo(file);
|
||||
} catch (IncorrectOperationException e) {
|
||||
LOG.error(e);
|
||||
@@ -83,10 +91,6 @@ public class VariableTypeFix extends IntentionAndQuickFixAction {
|
||||
return true;
|
||||
}
|
||||
|
||||
protected PsiVariable getVariable() {
|
||||
return myVariable;
|
||||
}
|
||||
|
||||
protected PsiType getReturnType() {
|
||||
return myReturnType;
|
||||
}
|
||||
|
||||
+33
-26
@@ -20,6 +20,7 @@ import com.intellij.codeInsight.daemon.quickFix.CreateClassOrPackageFix;
|
||||
import com.intellij.codeInsight.daemon.quickFix.CreateFieldOrPropertyFix;
|
||||
import com.intellij.codeInsight.intention.IntentionAction;
|
||||
import com.intellij.codeInsight.intention.QuickFixFactory;
|
||||
import com.intellij.codeInspection.LocalQuickFixAndIntentionActionOnPsiElement;
|
||||
import com.intellij.psi.*;
|
||||
import com.intellij.psi.util.PropertyMemberType;
|
||||
import com.intellij.psi.util.ClassKind;
|
||||
@@ -30,71 +31,77 @@ import org.jetbrains.annotations.Nullable;
|
||||
* @author cdr
|
||||
*/
|
||||
public class QuickFixFactoryImpl extends QuickFixFactory {
|
||||
public IntentionAction createModifierListFix(@NotNull PsiModifierList modifierList,
|
||||
@NotNull String modifier,
|
||||
boolean shouldHave,
|
||||
boolean showContainingClass) {
|
||||
public LocalQuickFixAndIntentionActionOnPsiElement createModifierListFix(@NotNull PsiModifierList modifierList,
|
||||
@NotNull String modifier,
|
||||
boolean shouldHave,
|
||||
boolean showContainingClass) {
|
||||
return new ModifierFix(modifierList, modifier, shouldHave,showContainingClass);
|
||||
}
|
||||
|
||||
public IntentionAction createModifierListFix(@NotNull PsiModifierListOwner owner,
|
||||
@NotNull final String modifier,
|
||||
final boolean shouldHave,
|
||||
final boolean showContainingClass) {
|
||||
public LocalQuickFixAndIntentionActionOnPsiElement createModifierListFix(@NotNull PsiModifierListOwner owner,
|
||||
@NotNull final String modifier,
|
||||
final boolean shouldHave,
|
||||
final boolean showContainingClass) {
|
||||
return new ModifierFix(owner, modifier, shouldHave, showContainingClass);
|
||||
}
|
||||
|
||||
public IntentionAction createMethodReturnFix(@NotNull PsiMethod method, @NotNull PsiType toReturn, boolean fixWholeHierarchy) {
|
||||
public LocalQuickFixAndIntentionActionOnPsiElement createMethodReturnFix(@NotNull PsiMethod method,
|
||||
@NotNull PsiType toReturn,
|
||||
boolean fixWholeHierarchy) {
|
||||
return new MethodReturnTypeFix(method, toReturn, fixWholeHierarchy);
|
||||
}
|
||||
|
||||
public IntentionAction createAddMethodFix(@NotNull PsiMethod method, @NotNull PsiClass toClass) {
|
||||
public LocalQuickFixAndIntentionActionOnPsiElement createAddMethodFix(@NotNull PsiMethod method, @NotNull PsiClass toClass) {
|
||||
return new AddMethodFix(method, toClass);
|
||||
}
|
||||
|
||||
public IntentionAction createAddMethodFix(@NotNull String methodText, @NotNull PsiClass toClass, String... exceptions) {
|
||||
public LocalQuickFixAndIntentionActionOnPsiElement createAddMethodFix(@NotNull String methodText,
|
||||
@NotNull PsiClass toClass,
|
||||
String... exceptions) {
|
||||
return new AddMethodFix(methodText, toClass, exceptions);
|
||||
}
|
||||
|
||||
public IntentionAction createImplementMethodsFix(@NotNull PsiClass aClass) {
|
||||
public LocalQuickFixAndIntentionActionOnPsiElement createImplementMethodsFix(@NotNull PsiClass aClass) {
|
||||
return new ImplementMethodsFix(aClass);
|
||||
}
|
||||
|
||||
public IntentionAction createImplementMethodsFix(@NotNull PsiElement psiElement) {
|
||||
public LocalQuickFixAndIntentionActionOnPsiElement createImplementMethodsFix(@NotNull PsiElement psiElement) {
|
||||
return new ImplementMethodsFix(psiElement);
|
||||
}
|
||||
|
||||
public IntentionAction createMethodThrowsFix(@NotNull PsiMethod method,
|
||||
@NotNull PsiClassType exceptionClass,
|
||||
boolean shouldThrow,
|
||||
boolean showContainingClass) {
|
||||
public LocalQuickFixAndIntentionActionOnPsiElement createMethodThrowsFix(@NotNull PsiMethod method,
|
||||
@NotNull PsiClassType exceptionClass,
|
||||
boolean shouldThrow,
|
||||
boolean showContainingClass) {
|
||||
return new MethodThrowsFix(method, exceptionClass, shouldThrow, showContainingClass);
|
||||
}
|
||||
|
||||
public IntentionAction createAddDefaultConstructorFix(@NotNull PsiClass aClass) {
|
||||
public LocalQuickFixAndIntentionActionOnPsiElement createAddDefaultConstructorFix(@NotNull PsiClass aClass) {
|
||||
return new AddDefaultConstructorFix(aClass);
|
||||
}
|
||||
|
||||
public IntentionAction createMethodParameterTypeFix(@NotNull PsiMethod method,
|
||||
int index,
|
||||
@NotNull PsiType newType,
|
||||
boolean fixWholeHierarchy) {
|
||||
public LocalQuickFixAndIntentionActionOnPsiElement createMethodParameterTypeFix(@NotNull PsiMethod method,
|
||||
int index,
|
||||
@NotNull PsiType newType,
|
||||
boolean fixWholeHierarchy) {
|
||||
return new MethodParameterFix(method, newType, index, fixWholeHierarchy);
|
||||
}
|
||||
|
||||
public IntentionAction createMakeClassInterfaceFix(@NotNull PsiClass aClass) {
|
||||
public LocalQuickFixAndIntentionActionOnPsiElement createMakeClassInterfaceFix(@NotNull PsiClass aClass) {
|
||||
return new MakeClassInterfaceFix(aClass, true);
|
||||
}
|
||||
|
||||
public IntentionAction createMakeClassInterfaceFix(@NotNull PsiClass aClass, final boolean makeInterface) {
|
||||
public LocalQuickFixAndIntentionActionOnPsiElement createMakeClassInterfaceFix(@NotNull PsiClass aClass, final boolean makeInterface) {
|
||||
return new MakeClassInterfaceFix(aClass, makeInterface);
|
||||
}
|
||||
|
||||
public IntentionAction createExtendsListFix(@NotNull PsiClass aClass, @NotNull PsiClassType typeToExtendFrom, boolean toAdd) {
|
||||
public LocalQuickFixAndIntentionActionOnPsiElement createExtendsListFix(@NotNull PsiClass aClass,
|
||||
@NotNull PsiClassType typeToExtendFrom,
|
||||
boolean toAdd) {
|
||||
return new ExtendsListFix(aClass, typeToExtendFrom, toAdd);
|
||||
}
|
||||
|
||||
public IntentionAction createRemoveUnusedParameterFix(@NotNull PsiParameter parameter) {
|
||||
public LocalQuickFixAndIntentionActionOnPsiElement createRemoveUnusedParameterFix(@NotNull PsiParameter parameter) {
|
||||
return new RemoveUnusedParameterFix(parameter);
|
||||
}
|
||||
|
||||
|
||||
@@ -30,19 +30,23 @@ import org.jetbrains.annotations.NotNull;
|
||||
*/
|
||||
public class AddAssertStatementFix implements LocalQuickFix {
|
||||
private static final Logger LOG = Logger.getInstance("#com.intellij.codeInspection.AddAssertStatementFix");
|
||||
private final PsiExpression myExpressionToAssert;
|
||||
private final SmartPsiElementPointer<PsiExpression> myExpressionToAssert;
|
||||
private final String myText;
|
||||
|
||||
public AddAssertStatementFix(@NotNull PsiExpression expressionToAssert) {
|
||||
myExpressionToAssert = SmartPointerManager.getInstance(expressionToAssert.getProject()).createSmartPsiElementPointer(expressionToAssert);
|
||||
LOG.assertTrue(PsiType.BOOLEAN.equals(expressionToAssert.getType()));
|
||||
myText = expressionToAssert.getText();
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public String getName() {
|
||||
return InspectionsBundle.message("inspection.assert.quickfix", myExpressionToAssert.getText());
|
||||
}
|
||||
|
||||
public AddAssertStatementFix(PsiExpression expressionToAssert) {
|
||||
myExpressionToAssert = expressionToAssert;
|
||||
LOG.assertTrue(PsiType.BOOLEAN.equals(myExpressionToAssert.getType()));
|
||||
return InspectionsBundle.message("inspection.assert.quickfix", myText);
|
||||
}
|
||||
|
||||
public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
|
||||
PsiExpression expressionToAssert = myExpressionToAssert.getElement();
|
||||
if (expressionToAssert == null) return;
|
||||
if (!CodeInsightUtilBase.preparePsiElementForWrite(descriptor.getPsiElement())) return;
|
||||
PsiElement element = descriptor.getPsiElement();
|
||||
PsiElement anchorElement = PsiTreeUtil.getParentOfType(element, PsiStatement.class);
|
||||
@@ -58,7 +62,8 @@ public class AddAssertStatementFix implements LocalQuickFix {
|
||||
PsiAssertStatement assertStatement = (PsiAssertStatement)factory.createStatementFromText(text, null);
|
||||
final PsiExpression assertCondition = assertStatement.getAssertCondition();
|
||||
assert assertCondition != null;
|
||||
assertCondition.replace(myExpressionToAssert);
|
||||
|
||||
assertCondition.replace(expressionToAssert);
|
||||
final PsiElement parent = anchorElement.getParent();
|
||||
if (parent instanceof PsiCodeBlock) {
|
||||
parent.addBefore(assertStatement, anchorElement);
|
||||
|
||||
@@ -32,17 +32,15 @@ import org.jetbrains.annotations.NotNull;
|
||||
|
||||
public class MoveToPackageFix implements LocalQuickFix {
|
||||
private static final Logger LOG = Logger.getInstance("#com.intellij.codeInspection.MoveToPackageFix");
|
||||
private final PsiFile myFile;
|
||||
private final PsiPackage myTargetPackage;
|
||||
private final String myTargetPackage;
|
||||
|
||||
public MoveToPackageFix(PsiFile file, PsiPackage targetPackage) {
|
||||
myFile = file;
|
||||
public MoveToPackageFix(String targetPackage) {
|
||||
myTargetPackage = targetPackage;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public String getName() {
|
||||
return QuickFixBundle.message("move.class.to.package.text", myTargetPackage.getQualifiedName());
|
||||
return QuickFixBundle.message("move.class.to.package.text", myTargetPackage);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@@ -50,22 +48,26 @@ public class MoveToPackageFix implements LocalQuickFix {
|
||||
return QuickFixBundle.message("move.class.to.package.family");
|
||||
}
|
||||
|
||||
public boolean isAvailable() {
|
||||
public boolean isAvailable(PsiFile myFile) {
|
||||
return myFile != null
|
||||
&& myFile.isValid()
|
||||
&& myFile.getManager().isInProject(myFile)
|
||||
&& myFile instanceof PsiJavaFile
|
||||
&& ((PsiJavaFile) myFile).getClasses().length != 0
|
||||
&& myTargetPackage != null
|
||||
&& myTargetPackage.isValid()
|
||||
&& JavaPsiFacade.getInstance(myFile.getProject()).findPackage(myTargetPackage) != null
|
||||
;
|
||||
}
|
||||
|
||||
public void applyFix(@NotNull final Project project, @NotNull final ProblemDescriptor descriptor) {
|
||||
PsiElement element = descriptor.getPsiElement();
|
||||
if (element == null) return;
|
||||
final PsiFile myFile = element.getContainingFile();
|
||||
|
||||
if (!CodeInsightUtilBase.prepareFileForWrite(myFile)) return;
|
||||
|
||||
try {
|
||||
String packageName = myTargetPackage.getQualifiedName();
|
||||
String packageName = myTargetPackage;
|
||||
PsiDirectory directory = PackageUtil.findOrCreateDirectoryForPackage(project, packageName, null, true);
|
||||
|
||||
if (directory == null) {
|
||||
|
||||
+1
-1
@@ -398,7 +398,7 @@ public class UncheckedWarningLocalInspection extends BaseJavaLocalInspectionTool
|
||||
if (valueType != null) {
|
||||
checkRawToGenericsAssignment(returnValue, returnType, valueType,
|
||||
false,
|
||||
(LocalQuickFix)QuickFixFactory.getInstance().createMethodReturnFix(method, valueType, true));
|
||||
QuickFixFactory.getInstance().createMethodReturnFix(method, valueType, true));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+7
-3
@@ -68,8 +68,8 @@ public class WrongPackageStatementInspection extends BaseJavaLocalInspectionTool
|
||||
}
|
||||
else if (!Comparing.equal(dirPackage.getQualifiedName(), packageReference.getText(), true)) {
|
||||
availableFixes.add(new AdjustPackageNameFix(javaFile, packageStatement, dirPackage));
|
||||
MoveToPackageFix moveToPackageFix = new MoveToPackageFix(file, classPackage);
|
||||
if (moveToPackageFix.isAvailable()) {
|
||||
MoveToPackageFix moveToPackageFix = new MoveToPackageFix(classPackage.getQualifiedName());
|
||||
if (moveToPackageFix.isAvailable(file)) {
|
||||
availableFixes.add(moveToPackageFix);
|
||||
}
|
||||
}
|
||||
@@ -77,7 +77,11 @@ public class WrongPackageStatementInspection extends BaseJavaLocalInspectionTool
|
||||
String description = JavaErrorMessages.message("package.name.file.path.mismatch",
|
||||
packageReference.getText(),
|
||||
dirPackage.getQualifiedName());
|
||||
return new ProblemDescriptor[]{manager.createProblemDescriptor(packageStatement.getPackageReference(), description, isOnTheFly, availableFixes.toArray(new LocalQuickFix[availableFixes.size()]), ProblemHighlightType.GENERIC_ERROR_OR_WARNING)};
|
||||
LocalQuickFix[] fixes = availableFixes.toArray(new LocalQuickFix[availableFixes.size()]);
|
||||
ProblemDescriptor descriptor =
|
||||
manager.createProblemDescriptor(packageStatement.getPackageReference(), description, isOnTheFly,
|
||||
fixes, ProblemHighlightType.GENERIC_ERROR_OR_WARNING);
|
||||
return new ProblemDescriptor[]{descriptor};
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
+1
-1
@@ -78,7 +78,7 @@ public class TypeMigrationProcessor extends BaseRefactoringProcessor {
|
||||
result.add(element);
|
||||
}
|
||||
}
|
||||
RefactoringUtil.highlightAllOccurences(project, PsiUtilBase.toPsiElementArray(result), editor);
|
||||
if (editor != null) RefactoringUtil.highlightAllOccurences(project, PsiUtilBase.toPsiElementArray(result), editor);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user