diff --git a/compiler/backend/src/org/jetbrains/jet/codegen/CompilationException.java b/compiler/backend/src/org/jetbrains/jet/codegen/CompilationException.java index ecc7a32ec80..a85f98ddc83 100644 --- a/compiler/backend/src/org/jetbrains/jet/codegen/CompilationException.java +++ b/compiler/backend/src/org/jetbrains/jet/codegen/CompilationException.java @@ -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() { @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(); } }); } diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/diagnostics/DiagnosticUtils.java b/compiler/frontend/src/org/jetbrains/jet/lang/diagnostics/DiagnosticUtils.java index bac22f2857f..4529d93fea4 100644 --- a/compiler/frontend/src/org/jetbrains/jet/lang/diagnostics/DiagnosticUtils.java +++ b/compiler/frontend/src/org/jetbrains/jet/lang/diagnostics/DiagnosticUtils.java @@ -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)";