change AddExceptionToExistingCatch to be quickfix

This commit is contained in:
Roman.Ivanov
2018-03-29 11:24:55 +07:00
parent 5aa1899843
commit 21ddec7d1f
11 changed files with 23 additions and 40 deletions
@@ -91,6 +91,8 @@ public abstract class QuickFixFactory {
@NotNull public abstract IntentionAction createSurroundWithTryCatchFix(@NotNull PsiElement element);
@NotNull public abstract IntentionAction createAddExceptionToExistingCatch(@NotNull PsiElement element);
@NotNull public abstract IntentionAction createGeneralizeCatchFix(@NotNull PsiElement element, @NotNull PsiClassType type);
@NotNull public abstract IntentionAction createChangeToAppendFix(@NotNull IElementType sign, @NotNull PsiType type, @NotNull PsiAssignmentExpression assignment);
@@ -195,6 +195,7 @@ public class HighlightFixUtil {
QuickFixAction.registerQuickFixAction(errorResult, QUICK_FIX_FACTORY.createAddExceptionToThrowsFix(element));
QuickFixAction.registerQuickFixAction(errorResult, QUICK_FIX_FACTORY.createAddExceptionFromFieldInitializerToConstructorThrowsFix(element));
QuickFixAction.registerQuickFixAction(errorResult, QUICK_FIX_FACTORY.createSurroundWithTryCatchFix(element));
QuickFixAction.registerQuickFixAction(errorResult, QUICK_FIX_FACTORY.createAddExceptionToExistingCatch(element));
if (unhandled.size() == 1) {
QuickFixAction.registerQuickFixAction(errorResult, QUICK_FIX_FACTORY.createGeneralizeCatchFix(element, unhandled.get(0)));
}
@@ -185,6 +185,10 @@ public class EmptyQuickFixFactory extends QuickFixFactory {
return QuickFixes.EMPTY_FIX;
}
@NotNull public IntentionAction createAddExceptionToExistingCatch(@NotNull PsiElement element) {
return QuickFixes.EMPTY_FIX;
}
@NotNull
@Override
public IntentionAction createGeneralizeCatchFix(@NotNull PsiElement psiElement, @NotNull PsiClassType psiClassType) {
@@ -1156,8 +1156,5 @@
<className>com.intellij.codeInsight.intention.impl.SplitFilterAction</className>
<category>Java/Streams</category>
</intentionAction>
<intentionAction>
<className>com.intellij.codeInsight.intention.impl.AddExceptionToExistingCatchAction</className>
</intentionAction>
</extensions>
</idea-plugin>
@@ -1,5 +1,5 @@
// Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.intellij.codeInsight.intention.impl;
package com.intellij.codeInsight.daemon.impl.quickfix;
import com.intellij.codeInsight.ExceptionUtil;
import com.intellij.codeInsight.daemon.QuickFixBundle;
@@ -24,14 +24,18 @@ import java.util.Objects;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class AddExceptionToExistingCatchAction extends PsiElementBaseIntentionAction {
public class AddExceptionToExistingCatchFix extends PsiElementBaseIntentionAction {
private final PsiElement myErrorElement;
public AddExceptionToExistingCatchFix(PsiElement errorElement) {myErrorElement = errorElement;}
@Override
public void invoke(@NotNull Project project, Editor editor, @NotNull PsiElement element) throws IncorrectOperationException {
PsiTryStatement tryStatement = PsiTreeUtil.getParentOfType(element, PsiTryStatement.class);
if (tryStatement == null) return;
PsiCatchSection[] catchSections = tryStatement.getCatchSections();
if (catchSections.length == 0) return;
List<PsiClassType> unhandledExceptions = new ArrayList<>(ExceptionUtil.getOwnUnhandledExceptions(element));
List<PsiClassType> unhandledExceptions = new ArrayList<>(ExceptionUtil.getOwnUnhandledExceptions(myErrorElement));
if (unhandledExceptions.size() != 1) return;
List<String> catchTexts = getAvailableCatchSections(catchSections)
.map(s -> s.getCatchType())
@@ -96,8 +100,8 @@ public class AddExceptionToExistingCatchAction extends PsiElementBaseIntentionAc
if (notFinishedCatches(catchSections)) return false;
PsiElement parent = PsiTreeUtil.getParentOfType(element, PsiCallExpression.class, PsiThrowStatement.class);
if (parent == null) return false;
List<PsiClassType> unhandledExceptions = new ArrayList<>(ExceptionUtil.collectUnhandledExceptions(tryStatement.getParent(), parent));
return !unhandledExceptions.isEmpty();
List<PsiClassType> unhandledExceptions = new ArrayList<>(ExceptionUtil.getOwnUnhandledExceptions(myErrorElement));
return unhandledExceptions.size() == 1;
}
private static boolean notFinishedCatches(PsiCatchSection[] catchSections) {
@@ -16,6 +16,7 @@ import com.intellij.codeInsight.daemon.quickFix.CreateFieldOrPropertyFix;
import com.intellij.codeInsight.intention.IntentionAction;
import com.intellij.codeInsight.intention.IntentionManager;
import com.intellij.codeInsight.intention.QuickFixFactory;
import com.intellij.codeInsight.daemon.impl.quickfix.AddExceptionToExistingCatchFix;
import com.intellij.codeInsight.intention.impl.CreateClassInPackageInModuleFix;
import com.intellij.codeInsight.intention.impl.ReplaceAssignmentWithComparisonFix;
import com.intellij.codeInspection.*;
@@ -227,6 +228,12 @@ public class QuickFixFactoryImpl extends QuickFixFactory {
return new SurroundWithTryCatchFix(element);
}
@NotNull
@Override
public IntentionAction createAddExceptionToExistingCatch(@NotNull PsiElement element) {
return new AddExceptionToExistingCatchFix(element);
}
@NotNull
@Override
public IntentionAction createGeneralizeCatchFix(@NotNull PsiElement element, @NotNull PsiClassType type) {
@@ -1,13 +0,0 @@
class A extends Exception {}
class B extends A {}
class C extends A {}
class Test {
public static void main(String[] args) {
try {
throw new A();
} catch (<spot>A</spot> e) {
} catch (C e) {
}
}
}
@@ -1,13 +0,0 @@
class A extends Exception {}
class B extends A {}
class C extends A {}
class Test {
public static void main(String[] args) {
try {
throw new A();
} catch (B e) {
} catch (C e) {
}
}
}
@@ -1,6 +0,0 @@
<html>
<body>
<p>Intention to add exception to existing catch clause</p>
<!-- tooltip end -->
</body>
</html>