Drop CompilerMessageLocation.NO_LOCATION, use null everywhere instead

This commit is contained in:
Alexander Udalov
2017-03-31 20:39:22 +03:00
parent 861d9a1620
commit cb4d2994a3
12 changed files with 31 additions and 47 deletions
@@ -19,7 +19,7 @@ package org.jetbrains.kotlin.cli.common.messages
import java.io.Serializable import java.io.Serializable
data class CompilerMessageLocation private constructor( data class CompilerMessageLocation private constructor(
val path: String?, val path: String,
val line: Int, val line: Int,
val column: Int, val column: Int,
val lineContent: String? val lineContent: String?
@@ -28,16 +28,13 @@ data class CompilerMessageLocation private constructor(
path + (if (line != -1 || column != -1) " ($line:$column)" else "") path + (if (line != -1 || column != -1) " ($line:$column)" else "")
companion object { companion object {
@JvmField @JvmStatic
val NO_LOCATION: CompilerMessageLocation = create(null) fun create(path: String?): CompilerMessageLocation? =
create(path, -1, -1, null)
@JvmStatic @JvmStatic
fun create(path: String?): CompilerMessageLocation = fun create(path: String?, line: Int, column: Int, lineContent: String?): CompilerMessageLocation? =
CompilerMessageLocation(path, -1, -1, null) if (path == null) null else CompilerMessageLocation(path, line, column, lineContent)
@JvmStatic
fun create(path: String?, line: Int, column: Int, lineContent: String?): CompilerMessageLocation =
if (path == null) NO_LOCATION else CompilerMessageLocation(path, line, column, lineContent)
private val serialVersionUID: Long = 8228357578L private val serialVersionUID: Long = 8228357578L
} }
@@ -16,7 +16,6 @@
package org.jetbrains.kotlin.cli.common.repl package org.jetbrains.kotlin.cli.common.repl
import org.jetbrains.kotlin.cli.common.messages.CompilerMessageLocation
import java.io.Reader import java.io.Reader
import java.util.concurrent.locks.ReentrantReadWriteLock import java.util.concurrent.locks.ReentrantReadWriteLock
import javax.script.* import javax.script.*
@@ -109,6 +108,5 @@ abstract class KotlinJsr223JvmScriptEngineBase(protected val myFactory: ScriptEn
} }
private fun ReplCompileResult.Error.locationString() = private fun ReplCompileResult.Error.locationString() =
if (location == CompilerMessageLocation.NO_LOCATION) "" if (location == null) ""
else " at ${location.line}:${location.column}" else " at ${location.line}:${location.column}"
@@ -62,9 +62,8 @@ sealed class ReplCheckResult : Serializable {
class Incomplete : ReplCheckResult() class Incomplete : ReplCheckResult()
class Error(val message: String, class Error(val message: String, val location: CompilerMessageLocation? = null) : ReplCheckResult() {
val location: CompilerMessageLocation = CompilerMessageLocation.NO_LOCATION) : ReplCheckResult() { override fun toString(): String = "Error(message = \"$message\")"
override fun toString(): String = "Error(message = \"$message\""
} }
companion object { companion object {
@@ -88,8 +87,7 @@ sealed class ReplCompileResult : Serializable {
class Incomplete : ReplCompileResult() class Incomplete : ReplCompileResult()
class Error(val message: String, class Error(val message: String, val location: CompilerMessageLocation? = null) : ReplCompileResult() {
val location: CompilerMessageLocation = CompilerMessageLocation.NO_LOCATION) : ReplCompileResult() {
override fun toString(): String = "Error(message = \"$message\"" override fun toString(): String = "Error(message = \"$message\""
} }
@@ -125,8 +123,7 @@ sealed class ReplEvalResult : Serializable {
sealed class Error(val message: String) : ReplEvalResult() { sealed class Error(val message: String) : ReplEvalResult() {
class Runtime(message: String, val cause: Exception? = null) : Error(message) class Runtime(message: String, val cause: Exception? = null) : Error(message)
class CompileTime(message: String, class CompileTime(message: String, val location: CompilerMessageLocation? = null) : Error(message)
val location: CompilerMessageLocation = CompilerMessageLocation.NO_LOCATION) : Error(message)
override fun toString(): String = "${this::class.simpleName}Error(message = \"$message\"" override fun toString(): String = "${this::class.simpleName}Error(message = \"$message\""
} }
@@ -183,4 +180,4 @@ enum class ReplRepeatingMode {
interface InvokeWrapper { interface InvokeWrapper {
operator fun <T> invoke(body: () -> T): T // e.g. for capturing io operator fun <T> invoke(body: () -> T): T // e.g. for capturing io
} }
@@ -77,7 +77,7 @@ public abstract class CLICompiler<A extends CommonCompilerArguments> {
Usage.print(errStream, createArguments(), false); Usage.print(errStream, createArguments(), false);
} }
catch (Throwable t) { catch (Throwable t) {
errStream.println(messageRenderer.render(EXCEPTION, OutputMessageUtil.renderException(t), CompilerMessageLocation.NO_LOCATION)); errStream.println(messageRenderer.render(EXCEPTION, OutputMessageUtil.renderException(t), null));
} }
return null; return null;
} }
@@ -16,7 +16,6 @@
package org.jetbrains.kotlin.cli.common.messages; package org.jetbrains.kotlin.cli.common.messages;
import kotlin.io.FilesKt;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import org.jetbrains.kotlin.utils.fileUtils.FileUtilsKt; import org.jetbrains.kotlin.utils.fileUtils.FileUtilsKt;
@@ -36,7 +35,7 @@ public interface MessageRenderer {
}; };
MessageRenderer PLAIN_FULL_PATHS = new PlainTextMessageRenderer() { MessageRenderer PLAIN_FULL_PATHS = new PlainTextMessageRenderer() {
@Nullable @NotNull
@Override @Override
protected String getPath(@NotNull CompilerMessageLocation location) { protected String getPath(@NotNull CompilerMessageLocation location) {
return location.getPath(); return location.getPath();
@@ -44,20 +43,18 @@ public interface MessageRenderer {
}; };
MessageRenderer PLAIN_RELATIVE_PATHS = new PlainTextMessageRenderer() { MessageRenderer PLAIN_RELATIVE_PATHS = new PlainTextMessageRenderer() {
@NotNull
private final File cwd = new File(".").getAbsoluteFile(); private final File cwd = new File(".").getAbsoluteFile();
@Nullable @NotNull
@Override @Override
protected String getPath(@NotNull CompilerMessageLocation location) { protected String getPath(@NotNull CompilerMessageLocation location) {
String path = location.getPath(); return FileUtilsKt.descendantRelativeTo(new File(location.getPath()), cwd).getPath();
return path == null ? path : FileUtilsKt.descendantRelativeTo(new File(path), cwd).getPath();
} }
}; };
String renderPreamble(); String renderPreamble();
String render(@NotNull CompilerMessageSeverity severity, @NotNull String message, @NotNull CompilerMessageLocation location); String render(@NotNull CompilerMessageSeverity severity, @NotNull String message, @Nullable CompilerMessageLocation location);
String renderConclusion(); String renderConclusion();
} }
@@ -37,7 +37,7 @@ public class MessageUtil {
return psiFileToMessageLocation(file, "<no path>", DiagnosticUtils.getLineAndColumnInPsiFile(file, element.getTextRange())); return psiFileToMessageLocation(file, "<no path>", DiagnosticUtils.getLineAndColumnInPsiFile(file, element.getTextRange()));
} }
@NotNull @Nullable
public static CompilerMessageLocation psiFileToMessageLocation( public static CompilerMessageLocation psiFileToMessageLocation(
@NotNull PsiFile file, @NotNull PsiFile file,
@Nullable String defaultValue, @Nullable String defaultValue,
@@ -57,16 +57,14 @@ public abstract class PlainTextMessageRenderer implements MessageRenderer {
} }
@Override @Override
public String render( public String render(@NotNull CompilerMessageSeverity severity, @NotNull String message, @Nullable CompilerMessageLocation location) {
@NotNull CompilerMessageSeverity severity, @NotNull String message, @NotNull CompilerMessageLocation location
) {
StringBuilder result = new StringBuilder(); StringBuilder result = new StringBuilder();
int line = location.getLine(); int line = location != null ? location.getLine() : -1;
int column = location.getColumn(); int column = location != null ? location.getColumn() : -1;
String lineContent = location.getLineContent(); String lineContent = location != null ? location.getLineContent() : null;
String path = getPath(location); String path = location != null ? getPath(location) : null;
if (path != null) { if (path != null) {
result.append(path); result.append(path);
result.append(":"); result.append(":");
@@ -48,7 +48,7 @@ public class PrintingMessageCollector implements MessageCollector {
hasErrors |= severity.isError(); hasErrors |= severity.isError();
errStream.println(messageRenderer.render(severity, message, location != null ? location : CompilerMessageLocation.NO_LOCATION)); errStream.println(messageRenderer.render(severity, message, location));
} }
@Override @Override
@@ -18,6 +18,7 @@ package org.jetbrains.kotlin.cli.common.messages;
import com.intellij.openapi.util.text.StringUtil; import com.intellij.openapi.util.text.StringUtil;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
public class XmlMessageRenderer implements MessageRenderer { public class XmlMessageRenderer implements MessageRenderer {
@Override @Override
@@ -26,11 +27,11 @@ public class XmlMessageRenderer implements MessageRenderer {
} }
@Override @Override
public String render(@NotNull CompilerMessageSeverity severity, @NotNull String message, @NotNull CompilerMessageLocation location) { public String render(@NotNull CompilerMessageSeverity severity, @NotNull String message, @Nullable CompilerMessageLocation location) {
StringBuilder out = new StringBuilder(); StringBuilder out = new StringBuilder();
String tagName = severity.getPresentableName(); String tagName = severity.getPresentableName();
out.append("<").append(tagName); out.append("<").append(tagName);
if (location.getPath() != null) { if (location != null) {
out.append(" path=\"").append(e(location.getPath())).append("\""); out.append(" path=\"").append(e(location.getPath())).append("\"");
out.append(" line=\"").append(location.getLine()).append("\""); out.append(" line=\"").append(location.getLine()).append("\"");
out.append(" column=\"").append(location.getColumn()).append("\""); out.append(" column=\"").append(location.getColumn()).append("\"");
@@ -98,8 +98,7 @@ open class KotlinJvmReplService(
override fun check(state: IReplStageState<*>, codeLine: ReplCodeLine): ReplCheckResult { override fun check(state: IReplStageState<*>, codeLine: ReplCodeLine): ReplCheckResult {
operationsTracer?.before("check") operationsTracer?.before("check")
try { try {
return replCompiler?.check(state, codeLine) return replCompiler?.check(state, codeLine) ?: ReplCheckResult.Error("Initialization error")
?: ReplCheckResult.Error("Initialization error", CompilerMessageLocation.NO_LOCATION)
} }
finally { finally {
operationsTracer?.after("check") operationsTracer?.after("check")
@@ -109,8 +108,7 @@ open class KotlinJvmReplService(
override fun compile(state: IReplStageState<*>, codeLine: ReplCodeLine): ReplCompileResult { override fun compile(state: IReplStageState<*>, codeLine: ReplCodeLine): ReplCompileResult {
operationsTracer?.before("compile") operationsTracer?.before("compile")
try { try {
return replCompiler?.compile(state, codeLine) return replCompiler?.compile(state, codeLine) ?: ReplCompileResult.Error("Initialization error")
?: ReplCompileResult.Error("Initialization error", CompilerMessageLocation.NO_LOCATION)
} }
finally { finally {
operationsTracer?.after("compile") operationsTracer?.after("compile")
@@ -35,7 +35,7 @@ internal class CompileServicesFacadeMessageCollector(
} }
override fun report(severity: CompilerMessageSeverity, message: String, location: CompilerMessageLocation?) { override fun report(severity: CompilerMessageSeverity, message: String, location: CompilerMessageLocation?) {
log.info("Message: " + MessageRenderer.WITHOUT_PATHS.render(severity, message, location ?: CompilerMessageLocation.NO_LOCATION)) log.info("Message: " + MessageRenderer.WITHOUT_PATHS.render(severity, message, location))
when (severity) { when (severity) {
CompilerMessageSeverity.OUTPUT -> { CompilerMessageSeverity.OUTPUT -> {
servicesFacade.report(ReportCategory.OUTPUT_MESSAGE, ReportSeverity.ERROR, message) servicesFacade.report(ReportCategory.OUTPUT_MESSAGE, ReportSeverity.ERROR, message)
@@ -42,9 +42,7 @@ public abstract class AbstractModuleXmlParserTest extends TestCase {
public void report( public void report(
@NotNull CompilerMessageSeverity severity, @NotNull String message, @Nullable CompilerMessageLocation location @NotNull CompilerMessageSeverity severity, @NotNull String message, @Nullable CompilerMessageLocation location
) { ) {
throw new AssertionError(MessageRenderer.PLAIN_FULL_PATHS.render( throw new AssertionError(MessageRenderer.PLAIN_FULL_PATHS.render(severity, message, location));
severity, message, location != null ? location : CompilerMessageLocation.NO_LOCATION
));
} }
@Override @Override