From d8b29b84016f4c3b6b582b2f58940dd7301aa240 Mon Sep 17 00:00:00 2001 From: Roman Shevchenko Date: Wed, 20 Jan 2016 12:43:42 +0100 Subject: [PATCH] [java] connect resolution of generics in resource closer method (IDEA-150607) --- .../intellij/codeInsight/ExceptionUtil.java | 45 +++++++++++-------- .../advHighlighting7/TryWithResources.java | 6 +++ 2 files changed, 33 insertions(+), 18 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 e5a2ee13331b..37b7be228ffc 100644 --- a/java/java-psi-impl/src/com/intellij/codeInsight/ExceptionUtil.java +++ b/java/java-psi-impl/src/com/intellij/codeInsight/ExceptionUtil.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2015 JetBrains s.r.o. + * Copyright 2000-2016 JetBrains s.r.o. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -41,6 +41,8 @@ import org.jetbrains.annotations.Nullable; import java.util.*; +import static com.intellij.openapi.util.Pair.pair; + /** * @author mike */ @@ -397,7 +399,7 @@ public class ExceptionUtil { @Override public void visitResourceVariable(@NotNull PsiResourceVariable resource) { - addExceptions(array, getUnhandledCloserExceptions((PsiResourceListElement)resource, null)); + addExceptions(array, getUnhandledCloserExceptions(resource, null)); visitElement(resource); } @@ -531,28 +533,35 @@ public class ExceptionUtil { @NotNull public static List getCloserExceptions(@NotNull PsiResourceListElement resource) { - PsiMethod method = PsiUtil.getResourceCloserMethod(resource); - PsiSubstitutor substitutor = PsiUtil.resolveGenericsClassInType(resource.getType()).getSubstitutor(); - return method != null ? getExceptionsByMethod(method, substitutor, resource) : Collections.emptyList(); - } - - /** @deprecated use {@link #getCloserExceptions(PsiResourceListElement)} (to be removed in IDEA 16) */ - @SuppressWarnings("unused") - public static List getCloserExceptions(@NotNull PsiResourceVariable resource) { - return getCloserExceptions((PsiResourceListElement)resource); + Pair closer = resolveCloser(resource); + return closer != null ? getExceptionsByMethod(closer.first, closer.second, resource) : Collections.emptyList(); } @NotNull public static List getUnhandledCloserExceptions(@NotNull PsiResourceListElement resource, @Nullable PsiElement topElement) { - PsiMethod method = PsiUtil.getResourceCloserMethod(resource); - PsiSubstitutor substitutor = PsiUtil.resolveGenericsClassInType(resource.getType()).getSubstitutor(); - return method != null ? getUnhandledExceptions(method, resource, topElement, substitutor) : Collections.emptyList(); + Pair closer = resolveCloser(resource); + return closer != null ? getUnhandledExceptions(closer.first, resource, topElement, closer.second) : Collections.emptyList(); } - /** @deprecated use {@link #getUnhandledCloserExceptions(PsiResourceListElement, PsiElement)} (to be removed in IDEA 16) */ - @SuppressWarnings("unused") - public static List getUnhandledCloserExceptions(@NotNull PsiResourceVariable resource, @Nullable PsiElement topElement) { - return getUnhandledCloserExceptions((PsiResourceListElement)resource, topElement); + private static Pair resolveCloser(PsiResourceListElement resource) { + PsiMethod method = PsiUtil.getResourceCloserMethod(resource); + if (method != null) { + PsiClass closerClass = method.getContainingClass(); + if (closerClass != null) { + PsiClassType.ClassResolveResult resourceType = PsiUtil.resolveGenericsClassInType(resource.getType()); + if (resourceType != null) { + PsiClass resourceClass = resourceType.getElement(); + if (resourceClass != null) { + PsiSubstitutor substitutor = TypeConversionUtil.getClassSubstitutor(closerClass, resourceClass, resourceType.getSubstitutor()); + if (substitutor != null) { + return pair(method, substitutor); + } + } + } + } + } + + return null; } @NotNull diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/advHighlighting7/TryWithResources.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/advHighlighting7/TryWithResources.java index 289dc6167f68..8ea2bce85e66 100644 --- a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/advHighlighting7/TryWithResources.java +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/advHighlighting7/TryWithResources.java @@ -20,6 +20,8 @@ class C { } } + interface GenRT extends Gen { } + void m1() { try (MyResource r = new MyResource()) { r.doSomething(); } catch (E1 | E2 | E3 ignore) { } @@ -87,4 +89,8 @@ class C { void m5() { try (Gen gen = new Gen.Impl()) { } } + + void m6() { + try (GenRT gen = null) { } + } } \ No newline at end of file