fix duplicate throws quickfix; add test

This commit is contained in:
Dmitry Batkovich
2018-07-02 12:46:14 +03:00
parent bda5493579
commit b6d217a8ee
8 changed files with 87 additions and 1 deletions
@@ -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);
@@ -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));
}
}
}
@@ -0,0 +1,9 @@
// "Remove 'IOException' from 'execute' throws list" "true"
import java.io.*;
class X {
void execute() throws XXX, YYY, IOException {
}
}
@@ -0,0 +1,9 @@
// "Remove 'IOException' from 'execute' throws list" "true"
import java.io.*;
class X {
void execute() throws IOException {
}
}
@@ -0,0 +1,9 @@
// "Remove 'IOException' from 'execute' throws list" "true"
import java.io.*;
class X {
void execute() throws XXX, IOE<caret>xception, YYY, IOException {
}
}
@@ -0,0 +1,9 @@
// "Remove 'IOException' from 'execute' throws list" "false"
import java.io.*;
class X {
void execute() throws XXX, IOException, YYY, IOEx<caret>ception {
}
}
@@ -0,0 +1,9 @@
// "Remove 'IOException' from 'execute' throws list" "true"
import java.io.*;
class X {
void execute() throws IOE<caret>xception, IOException {
}
}
@@ -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";
}
}