From 28afb2c6243e500ba6ce7792f9fe3268b1ba0b86 Mon Sep 17 00:00:00 2001 From: Tagir Valeev Date: Wed, 16 Jan 2019 10:11:52 +0700 Subject: [PATCH] ExceptionFilter: refine position for throw statements; support "caused by:" and naked (w/o thread info) (IDEA-205404) --- .../execution/filters/ExceptionFilter.java | 37 +++++++++++++------ 1 file changed, 25 insertions(+), 12 deletions(-) diff --git a/java/execution/openapi/src/com/intellij/execution/filters/ExceptionFilter.java b/java/execution/openapi/src/com/intellij/execution/filters/ExceptionFilter.java index 23c1c15531ac..f49fa2c6a0ee 100644 --- a/java/execution/openapi/src/com/intellij/execution/filters/ExceptionFilter.java +++ b/java/execution/openapi/src/com/intellij/execution/filters/ExceptionFilter.java @@ -16,10 +16,12 @@ package com.intellij.execution.filters; import com.intellij.openapi.project.DumbAware; -import com.intellij.psi.PsiArrayAccessExpression; -import com.intellij.psi.PsiJavaToken; +import com.intellij.psi.*; import com.intellij.psi.search.GlobalSearchScope; +import com.intellij.psi.util.InheritanceUtil; import com.intellij.psi.util.PsiElementFilter; +import com.intellij.psi.util.PsiUtil; +import com.intellij.util.ObjectUtils; import org.jetbrains.annotations.NotNull; import java.util.regex.Matcher; @@ -28,8 +30,8 @@ import java.util.regex.Pattern; public class ExceptionFilter implements Filter, DumbAware { private final ExceptionInfoCache myCache; private PsiElementFilter myNextLineRefiner; - - private static final Pattern EXCEPTION_PATTERN = Pattern.compile("^Exception in thread \".+\" java\\.lang\\.(\\w+):"); + + private static final Pattern EXCEPTION_PATTERN = Pattern.compile("(Exception in thread \".+\" |Caused by: |)(\\w+\\.[\\w.]+)(:.*)?"); public ExceptionFilter(@NotNull final GlobalSearchScope scope) { myCache = new ExceptionInfoCache(scope); @@ -44,14 +46,25 @@ public class ExceptionFilter implements Filter, DumbAware { } private static PsiElementFilter getRefinerFromException(String line) { - Matcher matcher = EXCEPTION_PATTERN.matcher(line); - if(!matcher.find()) return null; - String exceptionName = matcher.group(1); - if("ArrayIndexOutOfBoundsException".equals(exceptionName)) { - return element -> element instanceof PsiJavaToken && - element.textMatches("[") && - element.getParent() instanceof PsiArrayAccessExpression; + Matcher matcher = EXCEPTION_PATTERN.matcher(line.trim()); + if(!matcher.matches()) return null; + String exceptionName = matcher.group(2); + PsiElementFilter throwFilter = e -> { + if (!(e instanceof PsiKeyword) || !(e.textMatches(PsiKeyword.THROW))) return false; + PsiThrowStatement parent = ObjectUtils.tryCast(e.getParent(), PsiThrowStatement.class); + if (parent == null) return false; + PsiExpression exception = PsiUtil.skipParenthesizedExprDown(parent.getException()); + if (exception == null) return false; + PsiType type = exception.getType(); + if (type == null) return false; + return exception instanceof PsiNewExpression ? type.equalsToText(exceptionName) : InheritanceUtil.isInheritor(type, exceptionName); + }; + if ("java.lang.ArrayIndexOutOfBoundsException".equals(exceptionName)) { + return element -> throwFilter.isAccepted(element) || + (element instanceof PsiJavaToken && + element.textMatches("[") && + element.getParent() instanceof PsiArrayAccessExpression); } - return null; + return throwFilter; } }