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 d1a11ea62db1..9e83ec57e0eb 100644 --- a/java/java-psi-impl/src/com/intellij/codeInsight/ExceptionUtil.java +++ b/java/java-psi-impl/src/com/intellij/codeInsight/ExceptionUtil.java @@ -759,6 +759,10 @@ public final class ExceptionUtil { @Nullable PsiElement topElement) { if (element == null || element.getParent() == topElement || element.getParent() == null) return HandlePlace.UNHANDLED; + for (CustomExceptionHandler exceptionHandler : CustomExceptionHandler.KEY.getExtensionList()) { + if (exceptionHandler.isHandled(element, exceptionType, topElement)) return HandlePlace.UNKNOWN; + } + final PsiElement parent = element.getParent(); if (parent instanceof PsiMethod) { @@ -823,10 +827,6 @@ public final class ExceptionUtil { // exceptions thrown in field initializers should be thrown in all class constructors return HandlePlace.fromBoolean(areAllConstructorsThrow(aClass, exceptionType)); } - } else { - for (CustomExceptionHandler exceptionHandler : CustomExceptionHandler.KEY.getExtensionList()) { - if (exceptionHandler.isHandled(element, exceptionType, topElement)) return HandlePlace.UNKNOWN; - } } return getHandlePlace(parent, exceptionType, topElement); } diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/exceptionHighlighting/CustomHandler.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/exceptionHighlighting/CustomHandler.java new file mode 100644 index 000000000000..6c9013ff8f69 --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/exceptionHighlighting/CustomHandler.java @@ -0,0 +1,15 @@ +import java.util.jar.JarFile; + +class MyTest { + + Object field = Class.forName(""); + Object o = Class.forName("").getName(); + + void test() { + Runnable r = () -> new JarFile(""); + r = () -> {new JarFile("");}; + Class.forName(""); + Object j = new JarFile(""); + } +} + diff --git a/java/java-tests/testSrc/com/intellij/java/codeInsight/daemon/lambda/UnhandledExceptionsHighlightingTest.kt b/java/java-tests/testSrc/com/intellij/java/codeInsight/daemon/lambda/UnhandledExceptionsHighlightingTest.kt index 50a737830e80..f51999fbd2e9 100644 --- a/java/java-tests/testSrc/com/intellij/java/codeInsight/daemon/lambda/UnhandledExceptionsHighlightingTest.kt +++ b/java/java-tests/testSrc/com/intellij/java/codeInsight/daemon/lambda/UnhandledExceptionsHighlightingTest.kt @@ -1,11 +1,16 @@ -// Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. +// Copyright 2000-2022 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license. package com.intellij.java.codeInsight.daemon.lambda +import com.intellij.codeInsight.CustomExceptionHandler import com.intellij.codeInsight.daemon.LightDaemonAnalyzerTestCase +import com.intellij.openapi.application.ApplicationManager import com.intellij.openapi.projectRoots.JavaSdkVersion import com.intellij.openapi.roots.LanguageLevelProjectExtension import com.intellij.pom.java.LanguageLevel +import com.intellij.psi.PsiClassType +import com.intellij.psi.PsiElement import com.intellij.testFramework.IdeaTestUtil +import com.intellij.testFramework.registerExtension class UnhandledExceptionsHighlightingTest : LightDaemonAnalyzerTestCase() { @@ -17,6 +22,15 @@ class UnhandledExceptionsHighlightingTest : LightDaemonAnalyzerTestCase() { doTest(false) } + fun testCustomHandler() { + val trueHandler = object : CustomExceptionHandler() { + override fun isHandled(element: PsiElement?, exceptionType: PsiClassType, topElement: PsiElement?): Boolean = true + } + ApplicationManager.getApplication() + .registerExtension(CustomExceptionHandler.KEY, trueHandler, testRootDisposable) + doTest(false) + } + private fun doTest(warnings: Boolean) { LanguageLevelProjectExtension.getInstance(getJavaFacade().project).languageLevel = LanguageLevel.JDK_1_8 IdeaTestUtil.setTestVersion(JavaSdkVersion.JDK_1_8, getModule(), testRootDisposable)