diff --git a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/migration/TryWithIdenticalCatchesInspection.java b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/migration/TryWithIdenticalCatchesInspection.java index 89e2a02b86c4..a10aea4bd0d5 100644 --- a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/migration/TryWithIdenticalCatchesInspection.java +++ b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/migration/TryWithIdenticalCatchesInspection.java @@ -17,11 +17,12 @@ package com.siyeh.ig.migration; import com.intellij.codeInspection.ProblemDescriptor; import com.intellij.openapi.project.Project; +import com.intellij.openapi.util.text.StringUtil; import com.intellij.pom.java.JavaFeature; import com.intellij.psi.*; -import com.intellij.psi.codeStyle.CodeStyleManager; import com.intellij.psi.codeStyle.JavaCodeStyleManager; import com.intellij.psi.search.LocalSearchScope; +import com.intellij.psi.tree.IElementType; import com.intellij.refactoring.extractMethod.InputVariables; import com.intellij.refactoring.util.duplicates.DuplicatesFinder; import com.intellij.refactoring.util.duplicates.Match; @@ -233,6 +234,9 @@ public class TryWithIdenticalCatchesInspection extends BaseInspection { } boolean isDuplicate(@NotNull CatchSectionWrapper section) { + final Boolean sameComments = areSameComments(section); + if (sameComments != null) return sameComments; + final Match match = findDuplicate(section); if (match == null) { return false; @@ -253,6 +257,42 @@ public class TryWithIdenticalCatchesInspection extends BaseInspection { return myFinder.isDuplicate(section.myCodeBlock, true); } + @Nullable + private Boolean areSameComments(@NotNull CatchSectionWrapper section) { + if (!myCodeBlock.isEmpty()) { + return null; + } + if (!section.myCodeBlock.isEmpty()) { + return false; + } + final List comments = getCommentTexts(myCodeBlock); + final List otherComments = getCommentTexts(section.myCodeBlock); + return comments.equals(otherComments); + } + + @NotNull + private static List getCommentTexts(@NotNull PsiElement element) { + final List result = new ArrayList<>(); + for (PsiElement child = element.getFirstChild(); child != null; child = child.getNextSibling()) { + if (child instanceof PsiComment) { + final IElementType type = ((PsiComment)child).getTokenType(); + if (type == JavaTokenType.END_OF_LINE_COMMENT) { + final String text = StringUtil.trimStart(child.getText(), "//").trim(); + if (!text.isEmpty()) { + result.add(text); + } + } + else if (type == JavaTokenType.C_STYLE_COMMENT) { + final String text = StringUtil.trimStart(StringUtil.trimEnd(child.getText(), "*/"), "/*").trim(); + if (!text.isEmpty()) { + result.add(text); + } + } + } + } + return result; + } + boolean canSwapWith(@Nullable CatchSectionWrapper section) { if (section == null) return false; for (PsiClassType type : myTypes) { diff --git a/plugins/InspectionGadgets/test/com/siyeh/igtest/errorhandling/try_identical_catches/IdenticalCatchDifferentCommentStyle.after.java b/plugins/InspectionGadgets/test/com/siyeh/igtest/errorhandling/try_identical_catches/IdenticalCatchDifferentCommentStyle.after.java new file mode 100644 index 000000000000..21a853371693 --- /dev/null +++ b/plugins/InspectionGadgets/test/com/siyeh/igtest/errorhandling/try_identical_catches/IdenticalCatchDifferentCommentStyle.after.java @@ -0,0 +1,17 @@ +class C { + void foo() { + try { + bar(); + } catch (Ex1 e) { + // unique comment + } catch (Ex2 | Ex3 e) { /*same comment*/ + // duplicate comment + } + } + + void bar() throws Ex1, Ex2, Ex3 {} + + static class Ex1 extends Exception {} + static class Ex2 extends Exception {} + static class Ex3 extends Exception {} +} \ No newline at end of file diff --git a/plugins/InspectionGadgets/test/com/siyeh/igtest/errorhandling/try_identical_catches/IdenticalCatchDifferentCommentStyle.java b/plugins/InspectionGadgets/test/com/siyeh/igtest/errorhandling/try_identical_catches/IdenticalCatchDifferentCommentStyle.java new file mode 100644 index 000000000000..2d3f17f85172 --- /dev/null +++ b/plugins/InspectionGadgets/test/com/siyeh/igtest/errorhandling/try_identical_catches/IdenticalCatchDifferentCommentStyle.java @@ -0,0 +1,19 @@ +class C { + void foo() { + try { + bar(); + } catch (Ex1 e) { + // unique comment + } catch (Ex2 e) { /*same comment*/ + // duplicate comment + } catch (Ex3 e) { // same comment + /*duplicate comment*/ + } + } + + void bar() throws Ex1, Ex2, Ex3 {} + + static class Ex1 extends Exception {} + static class Ex2 extends Exception {} + static class Ex3 extends Exception {} +} \ No newline at end of file diff --git a/plugins/InspectionGadgets/test/com/siyeh/igtest/errorhandling/try_identical_catches/IdenticalCatchWithComments.after.java b/plugins/InspectionGadgets/test/com/siyeh/igtest/errorhandling/try_identical_catches/IdenticalCatchWithComments.after.java new file mode 100644 index 000000000000..3ce8d8b2235a --- /dev/null +++ b/plugins/InspectionGadgets/test/com/siyeh/igtest/errorhandling/try_identical_catches/IdenticalCatchWithComments.after.java @@ -0,0 +1,18 @@ +class C { + void foo() { + try { + bar(); + } catch (Ex1 e) { + // unique comment + /*some more*/ + } catch (Ex2 | Ex3 e) { /*same comment*/ + // duplicate comment + } + } + + void bar() throws Ex1, Ex2, Ex3 {} + + static class Ex1 extends Exception {} + static class Ex2 extends Exception {} + static class Ex3 extends Exception {} +} \ No newline at end of file diff --git a/plugins/InspectionGadgets/test/com/siyeh/igtest/errorhandling/try_identical_catches/IdenticalCatchWithComments.java b/plugins/InspectionGadgets/test/com/siyeh/igtest/errorhandling/try_identical_catches/IdenticalCatchWithComments.java new file mode 100644 index 000000000000..79d0583d2141 --- /dev/null +++ b/plugins/InspectionGadgets/test/com/siyeh/igtest/errorhandling/try_identical_catches/IdenticalCatchWithComments.java @@ -0,0 +1,20 @@ +class C { + void foo() { + try { + bar(); + } catch (Ex1 e) { + // unique comment + /*some more*/ + } catch (Ex2 e) { /*same comment*/ + // duplicate comment + } catch (Ex3 e) { /* same comment */ + //duplicate comment + } + } + + void bar() throws Ex1, Ex2, Ex3 {} + + static class Ex1 extends Exception {} + static class Ex2 extends Exception {} + static class Ex3 extends Exception {} +} \ No newline at end of file diff --git a/plugins/InspectionGadgets/test/com/siyeh/igtest/errorhandling/try_identical_catches/IdenticalCatchWithEmptyComments.after.java b/plugins/InspectionGadgets/test/com/siyeh/igtest/errorhandling/try_identical_catches/IdenticalCatchWithEmptyComments.after.java new file mode 100644 index 000000000000..439ea2d6b1a5 --- /dev/null +++ b/plugins/InspectionGadgets/test/com/siyeh/igtest/errorhandling/try_identical_catches/IdenticalCatchWithEmptyComments.after.java @@ -0,0 +1,16 @@ +class C { + void foo() { + try { + bar(); + } catch (Ex1 e) { + // unique comment + } catch (Ex2 | Ex3 e) { + } + } + + void bar() throws Ex1, Ex2, Ex3 {} + + static class Ex1 extends Exception {} + static class Ex2 extends Exception {} + static class Ex3 extends Exception {} +} \ No newline at end of file diff --git a/plugins/InspectionGadgets/test/com/siyeh/igtest/errorhandling/try_identical_catches/IdenticalCatchWithEmptyComments.java b/plugins/InspectionGadgets/test/com/siyeh/igtest/errorhandling/try_identical_catches/IdenticalCatchWithEmptyComments.java new file mode 100644 index 000000000000..b0ba0ad0f83a --- /dev/null +++ b/plugins/InspectionGadgets/test/com/siyeh/igtest/errorhandling/try_identical_catches/IdenticalCatchWithEmptyComments.java @@ -0,0 +1,20 @@ +class C { + void foo() { + try { + bar(); + } catch (Ex1 e) { + // unique comment + } catch (Ex2 e) { + } catch (Ex3 e) { + // + /* + */ + } + } + + void bar() throws Ex1, Ex2, Ex3 {} + + static class Ex1 extends Exception {} + static class Ex2 extends Exception {} + static class Ex3 extends Exception {} +} \ No newline at end of file diff --git a/plugins/InspectionGadgets/testsrc/com/siyeh/ig/migration/TryWithIdenticalCatchesTest.java b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/migration/TryWithIdenticalCatchesTest.java index 4fa3e9b1f4da..7106093a9fbf 100644 --- a/plugins/InspectionGadgets/testsrc/com/siyeh/ig/migration/TryWithIdenticalCatchesTest.java +++ b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/migration/TryWithIdenticalCatchesTest.java @@ -49,6 +49,18 @@ public class TryWithIdenticalCatchesTest extends LightCodeInsightFixtureTestCase doTest(true); } + public void testIdenticalCatchWithComments() { + doTest(); + } + + public void testIdenticalCatchWithEmptyComments() { + doTest(); + } + + public void testIdenticalCatchDifferentCommentStyle() { + doTest(); + } + public void doTest() { doTest(false); }