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;
public CompilationException(@NotNull String message, @Nullable Throwable cause, @NotNull PsiElement element) {
super(message, cause);
super(getMessage(message, cause, element), cause);
this.element = element;
}
@@ -37,33 +37,31 @@ public class CompilationException extends RuntimeException {
}
private String where() {
Throwable cause = getCause();
Throwable throwable = cause != null ? cause : this;
StackTraceElement[] stackTrace = throwable.getStackTrace();
private static String where(@NotNull Throwable cause) {
StackTraceElement[] stackTrace = cause.getStackTrace();
if (stackTrace != null && stackTrace.length > 0) {
return stackTrace[0].getFileName() + ":" + stackTrace[0].getLineNumber();
}
return "unknown";
}
@Override
public String getMessage() {
public static String getMessage(@NotNull final String message, @Nullable final Throwable cause, @NotNull final PsiElement element) {
return ApplicationManager.getApplication().runReadAction(new Computable<String>() {
@Override
public String compute() {
StringBuilder message =
new StringBuilder("Back-end (JVM) Internal error: ").append(CompilationException.super.getMessage()).append("\n");
Throwable cause = getCause();
StringBuilder result =
new StringBuilder("Back-end (JVM) Internal error: ").append(message).append("\n");
if (cause != null) {
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) {
int startOffset = node.getStartOffset();
PsiElement element = getClosestPsiElement(node);
if (element != null) {
if (element != null && element.isValid()) {
return atLocation(element.getContainingFile(), element.getTextRange());
}
return "' at offset " + startOffset + " (line and file unknown: no PSI element)";