From 4368276f5585d2c0a014ecf41894e9208cf2597c Mon Sep 17 00:00:00 2001 From: Bas Leijdekkers Date: Wed, 19 Sep 2012 12:20:22 +0200 Subject: [PATCH] add option to ignore exceptions subclassing others --- .../DuplicateThrowsInspection.java | 39 ++++++++++++++----- .../src/messages/InspectionsBundle.properties | 1 + .../DuplicateThrows.html | 17 ++++---- 3 files changed, 40 insertions(+), 17 deletions(-) diff --git a/java/java-impl/src/com/intellij/codeInspection/duplicateThrows/DuplicateThrowsInspection.java b/java/java-impl/src/com/intellij/codeInspection/duplicateThrows/DuplicateThrowsInspection.java index b4898e658cc0..b01d1054159d 100644 --- a/java/java-impl/src/com/intellij/codeInspection/duplicateThrows/DuplicateThrowsInspection.java +++ b/java/java-impl/src/com/intellij/codeInspection/duplicateThrows/DuplicateThrowsInspection.java @@ -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; } } } diff --git a/platform/platform-resources-en/src/messages/InspectionsBundle.properties b/platform/platform-resources-en/src/messages/InspectionsBundle.properties index 4100c767f268..73b9b7cb659e 100644 --- a/platform/platform-resources-en/src/messages/InspectionsBundle.properties +++ b/platform/platform-resources-en/src/messages/InspectionsBundle.properties @@ -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 diff --git a/resources-en/src/inspectionDescriptions/DuplicateThrows.html b/resources-en/src/inspectionDescriptions/DuplicateThrows.html index b00906883ddd..06ec466a034b 100644 --- a/resources-en/src/inspectionDescriptions/DuplicateThrows.html +++ b/resources-en/src/inspectionDescriptions/DuplicateThrows.html @@ -1,18 +1,21 @@ -This inspection reports duplicate classes in the method throws list. -
For example: +This inspection reports duplicate exceptions in a method throws list. +For example:
-void f() throws Exception, Exception {
+void f() throws Exception, Exception {
 }
 

-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:
-void f() throws IOException, Exception {
+void f() throws IOException, Exception {
 }
 
-
+ +

+Use the checkbox below to ignore exceptions subclassing others. +