add option to ignore exceptions subclassing others

This commit is contained in:
Bas Leijdekkers
2012-09-19 12:20:22 +02:00
parent 8d47a41127
commit 4368276f55
3 changed files with 40 additions and 17 deletions
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2009 JetBrains s.r.o.
* Copyright 2000-2012 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -21,34 +21,52 @@ import com.intellij.codeInspection.InspectionsBundle;
import com.intellij.codeInspection.ProblemHighlightType;
import com.intellij.codeInspection.ProblemsHolder;
import com.intellij.codeInspection.ex.BaseLocalInspectionTool;
import com.intellij.codeInspection.ui.SingleCheckboxOptionsPanel;
import com.intellij.psi.*;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import javax.swing.*;
public class DuplicateThrowsInspection extends BaseLocalInspectionTool {
@SuppressWarnings("PublicField")
public boolean ignoreSubclassing = false;
@Override
@NotNull
public String getDisplayName() {
return InspectionsBundle.message("inspection.duplicate.throws.display.name");
}
@Override
@NotNull
public String getGroupDisplayName() {
return GroupNames.DECLARATION_REDUNDANCY;
}
@Override
@NotNull
public String getShortName() {
return "DuplicateThrows";
}
@Nullable
@Override
public JComponent createOptionsPanel() {
return new SingleCheckboxOptionsPanel(
InspectionsBundle.message("inspection.duplicate.throws.ignore.subclassing.option"), this, "ignoreSubclassing");
}
@Override
@NotNull
public PsiElementVisitor buildVisitor(@NotNull final ProblemsHolder holder, boolean isOnTheFly) {
return new JavaElementVisitor() {
@Override public void visitMethod(PsiMethod method) {
PsiReferenceList throwsList = method.getThrowsList();
PsiJavaCodeReferenceElement[] refs = throwsList.getReferenceElements();
PsiClassType[] types = throwsList.getReferencedTypes();
outer:
for (int i = 0; i < types.length; i++) {
PsiClassType type = types[i];
for (int j = i+1; j < types.length; j++) {
@@ -58,17 +76,18 @@ public class DuplicateThrowsInspection extends BaseLocalInspectionTool {
if (type.equals(otherType)) {
problem = InspectionsBundle.message("inspection.duplicate.throws.problem");
}
else if (otherType.isAssignableFrom(type)) {
problem = InspectionsBundle.message("inspection.duplicate.throws.more.general.problem", otherType.getCanonicalText());
}
else if (type.isAssignableFrom(otherType)) {
problem = InspectionsBundle.message("inspection.duplicate.throws.more.general.problem", type.getCanonicalText());
ref = refs[j];
type = otherType;
else if (!ignoreSubclassing) {
if (otherType.isAssignableFrom(type)) {
problem = InspectionsBundle.message("inspection.duplicate.throws.more.general.problem", otherType.getCanonicalText());
}
else if (type.isAssignableFrom(otherType)) {
problem = InspectionsBundle.message("inspection.duplicate.throws.more.general.problem", type.getCanonicalText());
ref = refs[j];
type = otherType;
}
}
if (problem != null) {
holder.registerProblem(ref, problem, ProblemHighlightType.LIKE_UNUSED_SYMBOL, new DeleteThrowsFix(method, type));
//break outer;
}
}
}
@@ -584,6 +584,7 @@ inspection.filter.show.diff.action.text=Show diff
inspection.duplicate.throws.display.name=Duplicate throws
inspection.duplicate.throws.problem=Duplicate throws
inspection.duplicate.throws.more.general.problem=There is a more general exception, ''{0}'', in the throws list already.
inspection.duplicate.throws.ignore.subclassing.option=Ignore exceptions subclassing others
inspection.filter.show.diff.only.action.text=Show diff only
inspection.new.profile.ide.to.project.warning.message=Do you want to save selected IDE profile as project profile?
inspection.new.profile.ide.to.project.warning.title=Unable to set up IDE profile for scope
@@ -1,18 +1,21 @@
<html>
<body>
<font face="verdana" size="-1">This inspection reports duplicate classes in the method throws list.
<br>For example:
This inspection reports duplicate exceptions in a method throws list.
For example:
<code><pre>
<b><font color="#000080">void</font></b> f() <b><font color="#000080">throws</font></b> Exception, <b><font color="#800000">Exception</font></b> {
<b>void</b> f() <b>throws</b> Exception, <b>Exception</b> {
}
</pre></code>
<br>
Also, inspection warns if you have declared two exceptions one of which subclasses another.
E.g.:
The inspection also warns if one exception subclasses another.
For example:
<code><pre>
<b><font color="#000080">void</font></b> f() <b><font color="#000080">throws</font></b> <b><font color="#800000">IOException</font></b>, Exception {
<b>void</b> f() <b>throws</b> <b>IOException</b>, Exception {
}
</pre></code>
</font>
<!-- tooltip end -->
<p>
Use the checkbox below to ignore exceptions subclassing others.
</p>
</body>
</html>