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 22e98032eb8a..c9f49dbf0d19 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-2011 JetBrains s.r.o. + * Copyright 2000-2012 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. @@ -16,15 +16,15 @@ package com.intellij.codeInsight; import com.intellij.openapi.application.ApplicationManager; +import com.intellij.openapi.project.Project; import com.intellij.openapi.util.Computable; import com.intellij.openapi.util.NullableComputable; import com.intellij.psi.*; import com.intellij.psi.controlFlow.*; import com.intellij.psi.impl.PsiImplUtil; import com.intellij.psi.search.GlobalSearchScope; -import com.intellij.psi.util.InheritanceUtil; -import com.intellij.psi.util.PsiUtil; -import com.intellij.psi.util.TypeConversionUtil; +import com.intellij.psi.search.ProjectScope; +import com.intellij.psi.util.*; import com.intellij.util.NullableFunction; import com.intellij.util.containers.ContainerUtil; import gnu.trove.THashSet; @@ -347,18 +347,26 @@ public class ExceptionUtil { @NotNull public static List getUnhandledCloserExceptions(final PsiResourceVariable resource, @Nullable final PsiElement topElement) { - final PsiType resourceType = resource.getType(); - if (resourceType instanceof PsiClassType) { - final PsiClass resourceClass = ((PsiClassType)resourceType).resolve(); - if (resourceClass != null) { - final PsiMethod[] closers = resourceClass.findMethodsByName("close", false); - for (final PsiMethod method : closers) { - if (method.getParameterList().getParametersCount() == 0) { - return getUnhandledExceptions(method, resource, topElement, PsiSubstitutor.EMPTY); + final Project project = resource.getProject(); + final JavaPsiFacade facade = JavaPsiFacade.getInstance(project); + final PsiClass autoCloseable = facade.findClass(CommonClassNames.JAVA_LANG_AUTO_CLOSEABLE, ProjectScope.getLibrariesScope(project)); + if (autoCloseable != null) { + final PsiMethod[] methods = autoCloseable.findMethodsByName("close", false); + if (methods.length == 1) { + final MethodSignature signature = methods[0].getSignature(PsiSubstitutor.EMPTY); + final PsiType resourceType = resource.getType(); + if (resourceType instanceof PsiClassType) { + final PsiClass resourceClass = ((PsiClassType)resourceType).resolve(); + if (resourceClass != null) { + final PsiMethod method = MethodSignatureUtil.findMethodBySignature(resourceClass, signature, true); + if (method != null) { + return getUnhandledExceptions(method, resource, topElement, PsiSubstitutor.EMPTY); + } } } } } + return Collections.emptyList(); } diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/advHighlighting7/TryWithResources.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/advHighlighting7/TryWithResources.java index e8df782c83ac..c0f8eb7cc342 100644 --- a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/advHighlighting7/TryWithResources.java +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/advHighlighting7/TryWithResources.java @@ -1,3 +1,18 @@ +/* + * Copyright 2000-2012 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ class C { static class E extends Exception { } static class E1 extends E { } @@ -10,6 +25,8 @@ class C { @Override public void close() throws E3 { } } + static interface I extends AutoCloseable { } + void m1() { try (MyResource r = new MyResource()) { r.doSomething(); } catch (E1 | E2 | E3 ignore) { } @@ -24,6 +41,8 @@ class C { catch (E3 e) { } try (MyResource r = new MyResource()) { } + + try (I r = null) { System.out.println(r); } } void m2() throws Exception {