Exception fixed: com.intellij.psi.PsiInvalidElementAccessException

This commit is contained in:
Andrey Breslav
2013-09-29 15:19:16 +04:00
parent 1646b0875e
commit 3f00004255
2 changed files with 14 additions and 16 deletions
@@ -27,7 +27,7 @@ public class CompilationException extends RuntimeException {
private final PsiElement element; private final PsiElement element;
public CompilationException(@NotNull String message, @Nullable Throwable cause, @NotNull PsiElement element) { public CompilationException(@NotNull String message, @Nullable Throwable cause, @NotNull PsiElement element) {
super(message, cause); super(getMessage(message, cause, element), cause);
this.element = element; this.element = element;
} }
@@ -37,33 +37,31 @@ public class CompilationException extends RuntimeException {
} }
private String where() { private static String where(@NotNull Throwable cause) {
Throwable cause = getCause(); StackTraceElement[] stackTrace = cause.getStackTrace();
Throwable throwable = cause != null ? cause : this;
StackTraceElement[] stackTrace = throwable.getStackTrace();
if (stackTrace != null && stackTrace.length > 0) { if (stackTrace != null && stackTrace.length > 0) {
return stackTrace[0].getFileName() + ":" + stackTrace[0].getLineNumber(); return stackTrace[0].getFileName() + ":" + stackTrace[0].getLineNumber();
} }
return "unknown"; return "unknown";
} }
@Override public static String getMessage(@NotNull final String message, @Nullable final Throwable cause, @NotNull final PsiElement element) {
public String getMessage() {
return ApplicationManager.getApplication().runReadAction(new Computable<String>() { return ApplicationManager.getApplication().runReadAction(new Computable<String>() {
@Override @Override
public String compute() { public String compute() {
StringBuilder message = StringBuilder result =
new StringBuilder("Back-end (JVM) Internal error: ").append(CompilationException.super.getMessage()).append("\n"); new StringBuilder("Back-end (JVM) Internal error: ").append(message).append("\n");
Throwable cause = getCause();
if (cause != null) { if (cause != null) {
String causeMessage = cause.getMessage(); String causeMessage = cause.getMessage();
message.append("Cause: ").append(causeMessage == null ? cause.toString() : causeMessage).append("\n"); result.append("Cause: ").append(causeMessage == null ? cause.toString() : causeMessage).append("\n");
}
result.append("File being compiled and position: ").append(DiagnosticUtils.atLocation(element)).append("\n");
result.append("PsiElement: ").append(element.getText()).append("\n");
if (cause != null) {
result.append("The root cause was thrown at: ").append(where(cause));
} }
message.append("File being compiled and position: ").append(DiagnosticUtils.atLocation(element)).append("\n");
message.append("PsiElement: ").append(element.getText()).append("\n");
message.append("The root cause was thrown at: ").append(where());
return message.toString(); return result.toString();
} }
}); });
} }
@@ -77,7 +77,7 @@ public class DiagnosticUtils {
public static String atLocation(@NotNull ASTNode node) { public static String atLocation(@NotNull ASTNode node) {
int startOffset = node.getStartOffset(); int startOffset = node.getStartOffset();
PsiElement element = getClosestPsiElement(node); PsiElement element = getClosestPsiElement(node);
if (element != null) { if (element != null && element.isValid()) {
return atLocation(element.getContainingFile(), element.getTextRange()); return atLocation(element.getContainingFile(), element.getTextRange());
} }
return "' at offset " + startOffset + " (line and file unknown: no PSI element)"; return "' at offset " + startOffset + " (line and file unknown: no PSI element)";