ExceptionFilter: refine position for throw statements; support "caused by:" and naked (w/o thread info) (IDEA-205404)

This commit is contained in:
Tagir Valeev
2019-01-16 10:13:58 +07:00
parent 2688ec2d75
commit 28afb2c624
@@ -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;
}
}