From b2123b322d4f872777241ceb82f8a601e98b29a5 Mon Sep 17 00:00:00 2001 From: peter Date: Tue, 23 Oct 2018 11:32:55 +0200 Subject: [PATCH] getUnhandledExceptions: make potentially expensive substitutor calculation a bit lazier --- .../intellij/codeInsight/ExceptionUtil.java | 22 +++++++++++-------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/java/java-psi-impl/src/com/intellij/codeInsight/ExceptionUtil.java b/java/java-psi-impl/src/com/intellij/codeInsight/ExceptionUtil.java index 9b4d02e50fd3..9a3a93a024ea 100644 --- a/java/java-psi-impl/src/com/intellij/codeInsight/ExceptionUtil.java +++ b/java/java-psi-impl/src/com/intellij/codeInsight/ExceptionUtil.java @@ -24,6 +24,7 @@ import org.jetbrains.annotations.Nullable; import java.util.*; import java.util.function.Predicate; +import java.util.function.Supplier; /** * @author mike @@ -352,7 +353,7 @@ public class ExceptionUtil { final PsiElement resolve = resolveResult.getElement(); if (resolve instanceof PsiMethod) { final PsiElement referenceNameElement = methodReferenceExpression.getReferenceNameElement(); - return getUnhandledExceptions((PsiMethod)resolve, referenceNameElement, topElement, resolveResult.getSubstitutor()); + return getUnhandledExceptions((PsiMethod)resolve, referenceNameElement, topElement, resolveResult::getSubstitutor); } return Collections.emptyList(); } @@ -409,7 +410,7 @@ public class ExceptionUtil { if (element instanceof PsiEnumConstant) { final PsiMethod method = ((PsiEnumConstant)element).resolveMethod(); if (method != null) { - return getUnhandledExceptions(method, element, null, PsiSubstitutor.EMPTY); + return getUnhandledExceptions(method, element, null, () -> PsiSubstitutor.EMPTY); } return Collections.emptyList(); } @@ -466,7 +467,6 @@ public class ExceptionUtil { return Collections.emptyList(); } - final PsiSubstitutor substitutor = result.getSubstitutor(); if (!isArrayClone(method, methodCall) && methodCall instanceof PsiMethodCallExpression) { PsiFile containingFile = methodCall.getContainingFile(); MethodResolverProcessor processor = new MethodResolverProcessor((PsiMethodCallExpression)methodCall, containingFile); @@ -486,11 +486,11 @@ public class ExceptionUtil { }); if (!candidates.isEmpty()) { GlobalSearchScope scope = methodCall.getResolveScope(); - final List ex = collectSubstituted(substitutor, thrownExceptions, scope); + List ex = collectSubstituted(result.getSubstitutor(), thrownExceptions, scope); for (Pair pair : candidates) { final PsiClassType[] exceptions = pair.first.getThrowsList().getReferencedTypes(); if (exceptions.length == 0) { - return getUnhandledExceptions(methodCall, topElement, PsiSubstitutor.EMPTY, PsiClassType.EMPTY_ARRAY); + return Collections.emptyList(); } retainExceptions(ex, collectSubstituted(pair.second, exceptions, scope)); } @@ -502,7 +502,7 @@ public class ExceptionUtil { } } - return getUnhandledExceptions(method, methodCall, topElement, substitutor); + return getUnhandledExceptions(method, methodCall, topElement, result::getSubstitutor); } public static void retainExceptions(List ex, List thrownEx) { @@ -646,15 +646,19 @@ public class ExceptionUtil { } @NotNull - public static List getUnhandledExceptions(@NotNull PsiMethod method, + private static List getUnhandledExceptions(@NotNull PsiMethod method, PsiElement element, PsiElement topElement, - @NotNull PsiSubstitutor substitutor) { + @NotNull Supplier substitutor) { if (isArrayClone(method, element)) { return Collections.emptyList(); } final PsiClassType[] referencedTypes = method.getThrowsList().getReferencedTypes(); - return getUnhandledExceptions(element, topElement, substitutor, referencedTypes); + if (referencedTypes.length == 0) { + return Collections.emptyList(); + } + + return getUnhandledExceptions(element, topElement, substitutor.get(), referencedTypes); } private static List getUnhandledExceptions(PsiElement element,