exceptions collection: don't visit anonymous class body (IDEA-198967)

simple visit of expression list doesn't work as walking visitor starts to go down when it starts an element and element has no children
This commit is contained in:
Anna.Kozlova
2018-09-18 18:54:06 +02:00
parent a49f280b3f
commit 0acc44b025
3 changed files with 39 additions and 5 deletions
@@ -65,14 +65,13 @@ public class ExceptionUtil {
@NotNull
public static List<PsiClassType> getThrownExceptions(@NotNull PsiElement element) {
List<PsiClassType> result = new ArrayList<>();
element.accept(new JavaRecursiveElementWalkingVisitor() {
class Visitor extends JavaRecursiveElementWalkingVisitor {
@Override
public void visitAnonymousClass(PsiAnonymousClass aClass) {
final PsiExpressionList argumentList = aClass.getArgumentList();
if (argumentList != null){
super.visitExpressionList(argumentList);
if (argumentList != null && !argumentList.isEmpty()) {
argumentList.accept(new Visitor());
}
super.visitAnonymousClass(aClass);
}
@Override
@@ -128,7 +127,8 @@ public class ExceptionUtil {
addExceptions(result, getTryExceptions(statement));
// do not call super: try exception goes into try body recursively
}
});
}
element.accept(new Visitor());
return result;
}
@@ -0,0 +1,30 @@
import java.io.IOException;
import java.util.concurrent.Callable;
class MyTest {
public static void main() {
try {
new Callable() {
@Override
public Object call() throws IOException {
throw new IOException();
}
};
new Foo("") {
@Override
public Object call() throws IOException {
throw new IOException();
}
};
}
catch (Exception e) {
throw e;
}
}
static abstract class Foo implements Callable {
protected Foo(Object o) {
}
}
}
@@ -13,6 +13,10 @@ class UnhandledExceptionsHighlightingTest : LightDaemonAnalyzerTestCase() {
doTest(false)
}
fun testIgnoreExceptionThrownInAnonymous() {
doTest(false)
}
private fun doTest(warnings: Boolean) {
LanguageLevelProjectExtension.getInstance(getJavaFacade().project).languageLevel = LanguageLevel.JDK_1_8
IdeaTestUtil.setTestVersion(JavaSdkVersion.JDK_1_8, getModule(), testRootDisposable)