[java] always give a chance to custom exception handlers (IDEA-288417)

GitOrigin-RevId: 8ba7d718b85b1488c70766cc0c2ef5b9534f7f57
This commit is contained in:
Anna Kozlova
2022-02-16 18:42:30 +00:00
committed by intellij-monorepo-bot
parent 391de12162
commit a64b6a83bc
3 changed files with 34 additions and 5 deletions
@@ -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);
}
@@ -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("");
}
}
@@ -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>(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)