IDEA-162158 (Inspection incorrectly suggests to collapse "catch" blocks)
authorBas Leijdekkers <basleijdekkers@gmail.com>
Thu, 6 Oct 2016 10:22:54 +0000 (12:22 +0200)
committerBas Leijdekkers <basleijdekkers@gmail.com>
Thu, 6 Oct 2016 10:23:54 +0000 (12:23 +0200)
java/java-analysis-impl/src/com/intellij/refactoring/util/duplicates/DuplicatesFinder.java
plugins/InspectionGadgets/test/com/siyeh/igtest/errorhandling/try_identical_catches/TryIdenticalCatches.after.java
plugins/InspectionGadgets/test/com/siyeh/igtest/errorhandling/try_identical_catches/TryIdenticalCatches.java

index 539075b5fd12fc271192e71659584fc44c357ebe..ded7d252ae48c2486cbd90b43994ac6c0dc6068f 100644 (file)
@@ -401,12 +401,10 @@ public class DuplicatesFinder {
       final PsiType type1 = ((PsiNewExpression)pattern).getType();
       final PsiType type2 = ((PsiNewExpression)candidate).getType();
       if (type1 == null || type2 == null) return false;
-      final PsiJavaCodeReferenceElement classReference1 = ((PsiNewExpression)pattern).getClassReference();
-      final PsiJavaCodeReferenceElement classReference2 = ((PsiNewExpression)candidate).getClassReference();
-      if (classReference1 != null && classReference2 != null) {
-        final PsiElement resolved1 = classReference1.resolve();
-        final PsiElement resolved2 = classReference2.resolve();
-        if (!pattern.getManager().areElementsEquivalent(resolved1, resolved2)) return false;
+      final PsiMethod constructor1 = ((PsiNewExpression)pattern).resolveConstructor();
+      final PsiMethod constructor2 = ((PsiNewExpression)candidate).resolveConstructor();
+      if (constructor1 != null && constructor2 != null) {
+        if (!pattern.getManager().areElementsEquivalent(constructor1, constructor2)) return false;
       }
       else {
         if (!canTypesBeEquivalent(type1, type2)) return false;
index e86a1970530601f282348b7d0be0cf860aa9e5a5..87fdcfef51b3c1721f6342fa666fabf7821e4a0e 100644 (file)
@@ -185,4 +185,21 @@ class TryIdenticalCatches {
       System.out.println(e);
     }
   }
+}
+class TestInspection {
+  public void foo() throws MyException {
+    try {
+      toString();
+    } catch (IllegalArgumentException e) {
+      throw new MyException(e);
+    } catch (IllegalStateException e) {
+      throw new MyException(e);
+    }
+  }
+
+  private static class MyException extends Exception {
+    public MyException(IllegalArgumentException e) {}
+
+    public MyException(IllegalStateException e) {}
+  }
 }
\ No newline at end of file
index 05fce429d02e53515ec96b585baa2bf25bf96718..ff838abeae7c95663f562b82a85288590308f34a 100644 (file)
@@ -188,4 +188,21 @@ class TryIdenticalCatches {
       System.out.println(e);
     }
   }
+}
+class TestInspection {
+  public void foo() throws MyException {
+    try {
+      toString();
+    } catch (IllegalArgumentException e) {
+      throw new MyException(e);
+    } catch (IllegalStateException e) {
+      throw new MyException(e);
+    }
+  }
+
+  private static class MyException extends Exception {
+    public MyException(IllegalArgumentException e) {}
+
+    public MyException(IllegalStateException e) {}
+  }
 }
\ No newline at end of file