mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
IDEA-97803 Add ignore options to Groovy empty catch block inspection
This commit is contained in:
+3
-1
@@ -107,4 +107,6 @@ target.0.does.not.exist=Target ''{0}'' does not exist
|
||||
target.annotation.is.unused=@Target is unused
|
||||
change.lvalue.type=Change variable ''{0}'' type to ''{1}''
|
||||
replace.qualified.name.with.import=Replace qualified name with import
|
||||
highlight.assignments.from.void=Highlight assignments from void type
|
||||
highlight.assignments.from.void=Highlight assignments from void type
|
||||
comments.count.as.content=Comments count as content
|
||||
ignore.when.catch.parameter.is.named.ignore.or.ignored=Ignore when catch parameter is named ignore or ignored
|
||||
+39
-5
@@ -18,16 +18,26 @@ package org.jetbrains.plugins.groovy.codeInspection.exception;
|
||||
import com.intellij.codeInsight.daemon.impl.quickfix.RenameElementFix;
|
||||
import com.intellij.codeInspection.LocalQuickFix;
|
||||
import com.intellij.codeInspection.ProblemHighlightType;
|
||||
import com.intellij.codeInspection.ui.MultipleCheckboxOptionsPanel;
|
||||
import com.intellij.psi.PsiComment;
|
||||
import com.intellij.psi.PsiElement;
|
||||
import org.jetbrains.annotations.Nls;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.jetbrains.plugins.groovy.codeInspection.BaseInspection;
|
||||
import org.jetbrains.plugins.groovy.codeInspection.BaseInspectionVisitor;
|
||||
import org.jetbrains.plugins.groovy.codeInspection.GroovyInspectionBundle;
|
||||
import org.jetbrains.plugins.groovy.lang.psi.api.statements.GrCatchClause;
|
||||
import org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement;
|
||||
import org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock;
|
||||
import org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter;
|
||||
import org.jetbrains.plugins.groovy.lang.psi.util.PsiUtil;
|
||||
|
||||
import javax.swing.*;
|
||||
|
||||
public class GroovyEmptyCatchBlockInspection extends BaseInspection {
|
||||
public boolean myIgnore = true;
|
||||
public boolean myCountCommentsAsContent = true;
|
||||
|
||||
@Nls
|
||||
@NotNull
|
||||
@@ -41,11 +51,21 @@ public class GroovyEmptyCatchBlockInspection extends BaseInspection {
|
||||
return "Empty 'catch' block";
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public BaseInspectionVisitor buildVisitor() {
|
||||
return new Visitor();
|
||||
}
|
||||
|
||||
private static class Visitor extends BaseInspectionVisitor {
|
||||
@Nullable
|
||||
@Override
|
||||
public JComponent createOptionsPanel() {
|
||||
MultipleCheckboxOptionsPanel panel = new MultipleCheckboxOptionsPanel(this);
|
||||
panel.addCheckbox(GroovyInspectionBundle.message("comments.count.as.content"), "myCountCommentsAsContent");
|
||||
panel.addCheckbox(GroovyInspectionBundle.message("ignore.when.catch.parameter.is.named.ignore.or.ignored"), "myIgnore");
|
||||
return panel;
|
||||
}
|
||||
|
||||
private class Visitor extends BaseInspectionVisitor {
|
||||
|
||||
public void visitCatchClause(GrCatchClause catchClause) {
|
||||
super.visitCatchClause(catchClause);
|
||||
@@ -56,15 +76,29 @@ public class GroovyEmptyCatchBlockInspection extends BaseInspection {
|
||||
|
||||
final GrParameter parameter = catchClause.getParameter();
|
||||
if (parameter == null) return;
|
||||
if (GrExceptionUtil.ignore(parameter)) return;
|
||||
if (myIgnore && GrExceptionUtil.ignore(parameter)) return;
|
||||
|
||||
final LocalQuickFix[] fixes = {new RenameElementFix(parameter, "ignored")};
|
||||
final LocalQuickFix[] fixes = myIgnore
|
||||
? new RenameElementFix[]{new RenameElementFix(parameter, "ignored")}
|
||||
: LocalQuickFix.EMPTY_ARRAY;
|
||||
registerError(catchClause.getFirstChild(), "Empty '#ref' block #loc", fixes, ProblemHighlightType.GENERIC_ERROR_OR_WARNING);
|
||||
}
|
||||
|
||||
private static boolean isEmpty(@NotNull GrOpenBlock body) {
|
||||
private boolean isEmpty(@NotNull GrOpenBlock body) {
|
||||
final GrStatement[] statements = body.getStatements();
|
||||
return statements.length == 0;
|
||||
if (statements.length != 0) return false;
|
||||
|
||||
if (myCountCommentsAsContent) {
|
||||
final PsiElement brace = body.getLBrace();
|
||||
if (brace != null) {
|
||||
final PsiElement next = PsiUtil.skipWhitespaces(brace.getNextSibling(), true);
|
||||
if (next instanceof PsiComment) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
+31
@@ -23,6 +23,7 @@ import org.jetbrains.plugins.groovy.codeInspection.control.GroovyTrivialIfInspec
|
||||
import org.jetbrains.plugins.groovy.codeInspection.control.GroovyUnnecessaryContinueInspection
|
||||
import org.jetbrains.plugins.groovy.codeInspection.control.GroovyUnnecessaryReturnInspection
|
||||
import org.jetbrains.plugins.groovy.codeInspection.declaration.GrMethodMayBeStaticInspection
|
||||
import org.jetbrains.plugins.groovy.codeInspection.exception.GroovyEmptyCatchBlockInspection
|
||||
import org.jetbrains.plugins.groovy.codeInspection.metrics.GroovyOverlyLongMethodInspection
|
||||
import org.jetbrains.plugins.groovy.codeInspection.noReturnMethod.MissingReturnInspection
|
||||
import org.jetbrains.plugins.groovy.codeInspection.threading.GroovyUnconditionalWaitInspection
|
||||
@@ -316,4 +317,34 @@ for(i in []) {
|
||||
''', GroovyUnnecessaryContinueInspection)
|
||||
}
|
||||
|
||||
void testEmptyCatchBlock1() {
|
||||
testHighlighting('''
|
||||
try{} <warning descr="Empty 'catch' block">catch</warning>(IOException e) {}
|
||||
try{} catch(IOException ignored) {}
|
||||
try{} catch(IOException ignore) {}
|
||||
try{} catch(IOException e) {/*comment*/}
|
||||
''', GroovyEmptyCatchBlockInspection)
|
||||
}
|
||||
|
||||
void testEmptyCatchBlock2() {
|
||||
GroovyEmptyCatchBlockInspection inspection = new GroovyEmptyCatchBlockInspection()
|
||||
inspection.myIgnore = false
|
||||
myFixture.enableInspections(inspection)
|
||||
testHighlighting('try{} <warning descr="Empty \'catch\' block">catch</warning>(IOException ignored) {}')
|
||||
}
|
||||
|
||||
void testEmptyCatchBlock3() {
|
||||
GroovyEmptyCatchBlockInspection inspection = new GroovyEmptyCatchBlockInspection()
|
||||
inspection.myIgnore = false
|
||||
myFixture.enableInspections(inspection)
|
||||
testHighlighting('try{} <warning descr="Empty \'catch\' block">catch</warning>(IOException ignored) {}')
|
||||
}
|
||||
|
||||
void testEmptyCatchBlock4() {
|
||||
GroovyEmptyCatchBlockInspection inspection = new GroovyEmptyCatchBlockInspection()
|
||||
inspection.myCountCommentsAsContent = false
|
||||
myFixture.enableInspections(inspection)
|
||||
testHighlighting('try{} <warning descr="Empty \'catch\' block">catch</warning>(IOException e) {/*comment*/}')
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user