Java: Don't collapse 'catch' sections containing no code but with different comments...
authorPavel Dolgov <pavel.dolgov@jetbrains.com>
Fri, 20 Jul 2018 10:59:32 +0000 (13:59 +0300)
committerPavel Dolgov <pavel.dolgov@jetbrains.com>
Fri, 20 Jul 2018 11:09:19 +0000 (14:09 +0300)
plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/migration/TryWithIdenticalCatchesInspection.java
plugins/InspectionGadgets/test/com/siyeh/igtest/errorhandling/try_identical_catches/IdenticalCatchDifferentCommentStyle.after.java [new file with mode: 0644]
plugins/InspectionGadgets/test/com/siyeh/igtest/errorhandling/try_identical_catches/IdenticalCatchDifferentCommentStyle.java [new file with mode: 0644]
plugins/InspectionGadgets/test/com/siyeh/igtest/errorhandling/try_identical_catches/IdenticalCatchWithComments.after.java [new file with mode: 0644]
plugins/InspectionGadgets/test/com/siyeh/igtest/errorhandling/try_identical_catches/IdenticalCatchWithComments.java [new file with mode: 0644]
plugins/InspectionGadgets/test/com/siyeh/igtest/errorhandling/try_identical_catches/IdenticalCatchWithEmptyComments.after.java [new file with mode: 0644]
plugins/InspectionGadgets/test/com/siyeh/igtest/errorhandling/try_identical_catches/IdenticalCatchWithEmptyComments.java [new file with mode: 0644]
plugins/InspectionGadgets/testsrc/com/siyeh/ig/migration/TryWithIdenticalCatchesTest.java

index 89e2a02b86c4a30e9c2b0510c1385f7815c1f072..a10aea4bd0d58fb851d19b168da0e695755bb1de 100644 (file)
@@ -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<String> comments = getCommentTexts(myCodeBlock);
+      final List<String> otherComments = getCommentTexts(section.myCodeBlock);
+      return comments.equals(otherComments);
+    }
+
+    @NotNull
+    private static List<String> getCommentTexts(@NotNull PsiElement element) {
+      final List<String> 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 (file)
index 0000000..21a8533
--- /dev/null
@@ -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 (file)
index 0000000..2d3f17f
--- /dev/null
@@ -0,0 +1,19 @@
+class C {
+    void foo() {
+        try {
+            bar();
+        } catch (Ex1 e) {
+            // unique comment
+        } catch (Ex2 e) { /*same comment*/
+            // duplicate comment
+        } <warning descr="'catch' branch identical to 'Ex2' branch">catch (Ex3 <caret>e)</warning> { // 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 (file)
index 0000000..3ce8d8b
--- /dev/null
@@ -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 (file)
index 0000000..79d0583
--- /dev/null
@@ -0,0 +1,20 @@
+class C {
+    void foo() {
+        try {
+            bar();
+        } catch (Ex1 e) {
+            // unique comment
+            /*some more*/
+        } catch (Ex2 e) { /*same comment*/
+            // duplicate comment
+        } <warning descr="'catch' branch identical to 'Ex2' branch">catch (Ex3 <caret>e)</warning> { /* 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 (file)
index 0000000..439ea2d
--- /dev/null
@@ -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 (file)
index 0000000..b0ba0ad
--- /dev/null
@@ -0,0 +1,20 @@
+class C {
+    void foo() {
+        try {
+            bar();
+        } catch (Ex1 e) {
+            // unique comment
+        } catch (Ex2 e) {
+        } <warning descr="'catch' branch identical to 'Ex2' branch">catch (Ex3 <caret>e)</warning> {
+            //
+            /*
+            */
+        }
+    }
+
+    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
index 4fa3e9b1f4da7d085d5b803c5047c96e2a033674..7106093a9fbf2bef6cf69dd98fda6b4eb2740263 100644 (file)
@@ -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);
   }