From b6d217a8eef58069b22f138eb3426bbebe4586d9 Mon Sep 17 00:00:00 2001 From: Dmitry Batkovich Date: Tue, 6 Feb 2018 16:36:03 +0300 Subject: [PATCH] fix duplicate throws quickfix; add test --- .../daemon/impl/quickfix/MethodThrowsFix.java | 18 +++++++++++++++ .../DuplicateThrowsInspection.java | 2 +- .../quickFix/afterRemoveUnderCaret.java | 9 ++++++++ .../duplicateThrows/quickFix/afterSimple.java | 9 ++++++++ .../quickFix/beforeRemoveUnderCaret.java | 9 ++++++++ .../quickFix/beforeRemoveUnderCaret2.java | 9 ++++++++ .../quickFix/beforeSimple.java | 9 ++++++++ .../DuplicateThrowsInspectionFixTest.java | 23 +++++++++++++++++++ 8 files changed, 87 insertions(+), 1 deletion(-) create mode 100644 java/java-tests/testData/inspection/duplicateThrows/quickFix/afterRemoveUnderCaret.java create mode 100644 java/java-tests/testData/inspection/duplicateThrows/quickFix/afterSimple.java create mode 100644 java/java-tests/testData/inspection/duplicateThrows/quickFix/beforeRemoveUnderCaret.java create mode 100644 java/java-tests/testData/inspection/duplicateThrows/quickFix/beforeRemoveUnderCaret2.java create mode 100644 java/java-tests/testData/inspection/duplicateThrows/quickFix/beforeSimple.java create mode 100644 java/java-tests/testSrc/com/intellij/java/codeInspection/DuplicateThrowsInspectionFixTest.java diff --git a/java/java-analysis-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/MethodThrowsFix.java b/java/java-analysis-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/MethodThrowsFix.java index d490c4d22289..d584322aa778 100644 --- a/java/java-analysis-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/MethodThrowsFix.java +++ b/java/java-analysis-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/MethodThrowsFix.java @@ -65,6 +65,24 @@ public abstract class MethodThrowsFix extends LocalQuickFixOnPsiElement { } } + public static class RemoveFirst extends MethodThrowsFix { + public RemoveFirst(@NotNull PsiMethod method, @NotNull PsiClassType exceptionType, boolean showClassName) { + super(method, exceptionType, showClassName); + } + + @NotNull + @Override + protected String getTextMessageKey() { + return "fix.throws.list.remove.exception"; + } + + @Override + public void invoke(@NotNull Project project, @NotNull PsiFile file, @NotNull PsiElement startElement, @NotNull PsiElement endElement) { + PsiJavaCodeReferenceElement[] referenceElements = ((PsiMethod) startElement).getThrowsList().getReferenceElements(); + Arrays.stream(referenceElements).filter(referenceElement -> referenceElement.getCanonicalText().equals(myThrowsCanonicalText)).findFirst().ifPresent(PsiElement::delete); + } + } + public static class Remove extends MethodThrowsFix { public Remove(@NotNull PsiMethod method, @NotNull PsiClassType exceptionType, boolean showClassName) { super(method, exceptionType, showClassName); diff --git a/java/java-analysis-impl/src/com/intellij/codeInspection/duplicateThrows/DuplicateThrowsInspection.java b/java/java-analysis-impl/src/com/intellij/codeInspection/duplicateThrows/DuplicateThrowsInspection.java index 13a1adf6bd68..4cbf63d685a0 100644 --- a/java/java-analysis-impl/src/com/intellij/codeInspection/duplicateThrows/DuplicateThrowsInspection.java +++ b/java/java-analysis-impl/src/com/intellij/codeInspection/duplicateThrows/DuplicateThrowsInspection.java @@ -83,7 +83,7 @@ public class DuplicateThrowsInspection extends AbstractBaseJavaLocalInspectionTo } } if (problem != null) { - holder.registerProblem(ref, problem, ProblemHighlightType.LIKE_UNUSED_SYMBOL, new MethodThrowsFix.Remove(method, type, false)); + holder.registerProblem(ref, problem, ProblemHighlightType.LIKE_UNUSED_SYMBOL, new MethodThrowsFix.RemoveFirst(method, type, false)); } } } diff --git a/java/java-tests/testData/inspection/duplicateThrows/quickFix/afterRemoveUnderCaret.java b/java/java-tests/testData/inspection/duplicateThrows/quickFix/afterRemoveUnderCaret.java new file mode 100644 index 000000000000..7a7eda83a183 --- /dev/null +++ b/java/java-tests/testData/inspection/duplicateThrows/quickFix/afterRemoveUnderCaret.java @@ -0,0 +1,9 @@ +// "Remove 'IOException' from 'execute' throws list" "true" +import java.io.*; + +class X { + + void execute() throws XXX, YYY, IOException { + + } +} \ No newline at end of file diff --git a/java/java-tests/testData/inspection/duplicateThrows/quickFix/afterSimple.java b/java/java-tests/testData/inspection/duplicateThrows/quickFix/afterSimple.java new file mode 100644 index 000000000000..89f096397a6b --- /dev/null +++ b/java/java-tests/testData/inspection/duplicateThrows/quickFix/afterSimple.java @@ -0,0 +1,9 @@ +// "Remove 'IOException' from 'execute' throws list" "true" +import java.io.*; + +class X { + + void execute() throws IOException { + + } +} \ No newline at end of file diff --git a/java/java-tests/testData/inspection/duplicateThrows/quickFix/beforeRemoveUnderCaret.java b/java/java-tests/testData/inspection/duplicateThrows/quickFix/beforeRemoveUnderCaret.java new file mode 100644 index 000000000000..54d9d350cf5e --- /dev/null +++ b/java/java-tests/testData/inspection/duplicateThrows/quickFix/beforeRemoveUnderCaret.java @@ -0,0 +1,9 @@ +// "Remove 'IOException' from 'execute' throws list" "true" +import java.io.*; + +class X { + + void execute() throws XXX, IOException, YYY, IOException { + + } +} \ No newline at end of file diff --git a/java/java-tests/testData/inspection/duplicateThrows/quickFix/beforeRemoveUnderCaret2.java b/java/java-tests/testData/inspection/duplicateThrows/quickFix/beforeRemoveUnderCaret2.java new file mode 100644 index 000000000000..f3333af33c44 --- /dev/null +++ b/java/java-tests/testData/inspection/duplicateThrows/quickFix/beforeRemoveUnderCaret2.java @@ -0,0 +1,9 @@ +// "Remove 'IOException' from 'execute' throws list" "false" +import java.io.*; + +class X { + + void execute() throws XXX, IOException, YYY, IOException { + + } +} \ No newline at end of file diff --git a/java/java-tests/testData/inspection/duplicateThrows/quickFix/beforeSimple.java b/java/java-tests/testData/inspection/duplicateThrows/quickFix/beforeSimple.java new file mode 100644 index 000000000000..f223654689c8 --- /dev/null +++ b/java/java-tests/testData/inspection/duplicateThrows/quickFix/beforeSimple.java @@ -0,0 +1,9 @@ +// "Remove 'IOException' from 'execute' throws list" "true" +import java.io.*; + +class X { + + void execute() throws IOException, IOException { + + } +} \ No newline at end of file diff --git a/java/java-tests/testSrc/com/intellij/java/codeInspection/DuplicateThrowsInspectionFixTest.java b/java/java-tests/testSrc/com/intellij/java/codeInspection/DuplicateThrowsInspectionFixTest.java new file mode 100644 index 000000000000..4a6aee8c1e58 --- /dev/null +++ b/java/java-tests/testSrc/com/intellij/java/codeInspection/DuplicateThrowsInspectionFixTest.java @@ -0,0 +1,23 @@ +// 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.java.codeInspection; + +import com.intellij.codeInsight.daemon.quickFix.LightQuickFixParameterizedTestCase; +import com.intellij.codeInspection.LocalInspectionTool; +import com.intellij.codeInspection.duplicateThrows.DuplicateThrowsInspection; +import org.jetbrains.annotations.NotNull; + +public class DuplicateThrowsInspectionFixTest extends LightQuickFixParameterizedTestCase { + + @NotNull + @Override + protected LocalInspectionTool[] configureLocalInspectionTools() { + return new LocalInspectionTool[]{new DuplicateThrowsInspection()}; + } + + public void test() { doAllTests(); } + + @Override + protected String getBasePath() { + return "/inspection/duplicateThrows/quickFix"; + } +}