From cf11fdff3ce1224fe8a0b9b19c5799a45541f6f6 Mon Sep 17 00:00:00 2001 From: Bas Leijdekkers Date: Thu, 6 Oct 2016 12:22:54 +0200 Subject: [PATCH] IDEA-162158 (Inspection incorrectly suggests to collapse "catch" blocks) --- .../util/duplicates/DuplicatesFinder.java | 10 ++++------ .../TryIdenticalCatches.after.java | 17 +++++++++++++++++ .../TryIdenticalCatches.java | 17 +++++++++++++++++ 3 files changed, 38 insertions(+), 6 deletions(-) diff --git a/java/java-analysis-impl/src/com/intellij/refactoring/util/duplicates/DuplicatesFinder.java b/java/java-analysis-impl/src/com/intellij/refactoring/util/duplicates/DuplicatesFinder.java index 539075b5fd12..ded7d252ae48 100644 --- a/java/java-analysis-impl/src/com/intellij/refactoring/util/duplicates/DuplicatesFinder.java +++ b/java/java-analysis-impl/src/com/intellij/refactoring/util/duplicates/DuplicatesFinder.java @@ -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; diff --git a/plugins/InspectionGadgets/test/com/siyeh/igtest/errorhandling/try_identical_catches/TryIdenticalCatches.after.java b/plugins/InspectionGadgets/test/com/siyeh/igtest/errorhandling/try_identical_catches/TryIdenticalCatches.after.java index e86a19705306..87fdcfef51b3 100644 --- a/plugins/InspectionGadgets/test/com/siyeh/igtest/errorhandling/try_identical_catches/TryIdenticalCatches.after.java +++ b/plugins/InspectionGadgets/test/com/siyeh/igtest/errorhandling/try_identical_catches/TryIdenticalCatches.after.java @@ -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 diff --git a/plugins/InspectionGadgets/test/com/siyeh/igtest/errorhandling/try_identical_catches/TryIdenticalCatches.java b/plugins/InspectionGadgets/test/com/siyeh/igtest/errorhandling/try_identical_catches/TryIdenticalCatches.java index 05fce429d02e..ff838abeae7c 100644 --- a/plugins/InspectionGadgets/test/com/siyeh/igtest/errorhandling/try_identical_catches/TryIdenticalCatches.java +++ b/plugins/InspectionGadgets/test/com/siyeh/igtest/errorhandling/try_identical_catches/TryIdenticalCatches.java @@ -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 -- 2.32.0