From 333c564c628e206d9bf8fc2b3ecb13681e521992 Mon Sep 17 00:00:00 2001 From: Bas Leijdekkers Date: Mon, 23 Aug 2021 09:43:33 +0200 Subject: [PATCH] Java: even more precise rethrow types (IJ-CR-13170) GitOrigin-RevId: 34d767e800b3dc24d11fa05e6d237b88b630cb0c --- .../source/tree/java/PsiCatchSectionImpl.java | 8 +++++--- .../advHighlighting7/PreciseRethrow.java | 18 ++++++++++++++++++ 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/java/java-psi-impl/src/com/intellij/psi/impl/source/tree/java/PsiCatchSectionImpl.java b/java/java-psi-impl/src/com/intellij/psi/impl/source/tree/java/PsiCatchSectionImpl.java index 91d780f11557..19838ed4fd34 100644 --- a/java/java-psi-impl/src/com/intellij/psi/impl/source/tree/java/PsiCatchSectionImpl.java +++ b/java/java-psi-impl/src/com/intellij/psi/impl/source/tree/java/PsiCatchSectionImpl.java @@ -131,7 +131,10 @@ public class PsiCatchSectionImpl extends CompositePsiElement implements PsiCatch private static List computePreciseCatchTypes(PsiType declaredType, List uncaughtTypes) { List types = new SmartList<>(); for (PsiType type : uncaughtTypes) { - if (declaredType.isAssignableFrom(type) || + if (type.isAssignableFrom(declaredType)) { + types.add(declaredType); + } + else if (declaredType.isAssignableFrom(type) || // JLS 11.2.3 "Exception Checking": // "It is a compile-time error if a catch clause can catch checked exception class E1 and it is not the case // that the try block corresponding to the catch clause can throw a checked exception class that is @@ -142,8 +145,7 @@ public class PsiCatchSectionImpl extends CompositePsiElement implements PsiCatch } } // ... the throw statement throws precisely the set of exception types T. - if (!types.isEmpty()) return Collections.unmodifiableList(types); - return Collections.singletonList(declaredType); + return types; } private static Collection getThrownTypes(@NotNull PsiTryStatement statement) { diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/advHighlighting7/PreciseRethrow.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/advHighlighting7/PreciseRethrow.java index 1e2f4ecfdd81..b37afd521049 100644 --- a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/advHighlighting7/PreciseRethrow.java +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/advHighlighting7/PreciseRethrow.java @@ -277,4 +277,22 @@ class C { } catch (E ignored) {} } + + private void m17() throws InterruptedException, java.io.FileNotFoundException { + try { + Thread.sleep(1); + throw new java.io.FileNotFoundException(); + } catch (InterruptedException | java.io.IOException ex) { + throw ex; + } + } + + private void m18() throws InterruptedException { + try { + Thread.sleep(1L); + // no IOException thrown + } catch (java.io.IOException ex) { + throw ex; + } + } } \ No newline at end of file