diff --git a/compiler/cli/cli-common/src/org/jetbrains/kotlin/cli/common/messages/CompilerMessageLocation.kt b/compiler/cli/cli-common/src/org/jetbrains/kotlin/cli/common/messages/CompilerMessageLocation.kt index fc0def65fc3..1f3a6d349c5 100644 --- a/compiler/cli/cli-common/src/org/jetbrains/kotlin/cli/common/messages/CompilerMessageLocation.kt +++ b/compiler/cli/cli-common/src/org/jetbrains/kotlin/cli/common/messages/CompilerMessageLocation.kt @@ -19,7 +19,7 @@ package org.jetbrains.kotlin.cli.common.messages import java.io.Serializable data class CompilerMessageLocation private constructor( - val path: String?, + val path: String, val line: Int, val column: Int, val lineContent: String? @@ -28,16 +28,13 @@ data class CompilerMessageLocation private constructor( path + (if (line != -1 || column != -1) " ($line:$column)" else "") companion object { - @JvmField - val NO_LOCATION: CompilerMessageLocation = create(null) + @JvmStatic + fun create(path: String?): CompilerMessageLocation? = + create(path, -1, -1, null) @JvmStatic - fun create(path: String?): CompilerMessageLocation = - CompilerMessageLocation(path, -1, -1, null) - - @JvmStatic - fun create(path: String?, line: Int, column: Int, lineContent: String?): CompilerMessageLocation = - if (path == null) NO_LOCATION else CompilerMessageLocation(path, line, column, lineContent) + fun create(path: String?, line: Int, column: Int, lineContent: String?): CompilerMessageLocation? = + if (path == null) null else CompilerMessageLocation(path, line, column, lineContent) private val serialVersionUID: Long = 8228357578L } diff --git a/compiler/cli/cli-common/src/org/jetbrains/kotlin/cli/common/repl/KotlinJsr223JvmScriptEngineBase.kt b/compiler/cli/cli-common/src/org/jetbrains/kotlin/cli/common/repl/KotlinJsr223JvmScriptEngineBase.kt index 02664d504e3..e0f1380c569 100644 --- a/compiler/cli/cli-common/src/org/jetbrains/kotlin/cli/common/repl/KotlinJsr223JvmScriptEngineBase.kt +++ b/compiler/cli/cli-common/src/org/jetbrains/kotlin/cli/common/repl/KotlinJsr223JvmScriptEngineBase.kt @@ -16,7 +16,6 @@ package org.jetbrains.kotlin.cli.common.repl -import org.jetbrains.kotlin.cli.common.messages.CompilerMessageLocation import java.io.Reader import java.util.concurrent.locks.ReentrantReadWriteLock import javax.script.* @@ -109,6 +108,5 @@ abstract class KotlinJsr223JvmScriptEngineBase(protected val myFactory: ScriptEn } private fun ReplCompileResult.Error.locationString() = - if (location == CompilerMessageLocation.NO_LOCATION) "" + if (location == null) "" else " at ${location.line}:${location.column}" - diff --git a/compiler/cli/cli-common/src/org/jetbrains/kotlin/cli/common/repl/ReplApi.kt b/compiler/cli/cli-common/src/org/jetbrains/kotlin/cli/common/repl/ReplApi.kt index c2d0514433f..8e0072be080 100644 --- a/compiler/cli/cli-common/src/org/jetbrains/kotlin/cli/common/repl/ReplApi.kt +++ b/compiler/cli/cli-common/src/org/jetbrains/kotlin/cli/common/repl/ReplApi.kt @@ -62,9 +62,8 @@ sealed class ReplCheckResult : Serializable { class Incomplete : ReplCheckResult() - class Error(val message: String, - val location: CompilerMessageLocation = CompilerMessageLocation.NO_LOCATION) : ReplCheckResult() { - override fun toString(): String = "Error(message = \"$message\"" + class Error(val message: String, val location: CompilerMessageLocation? = null) : ReplCheckResult() { + override fun toString(): String = "Error(message = \"$message\")" } companion object { @@ -88,8 +87,7 @@ sealed class ReplCompileResult : Serializable { class Incomplete : ReplCompileResult() - class Error(val message: String, - val location: CompilerMessageLocation = CompilerMessageLocation.NO_LOCATION) : ReplCompileResult() { + class Error(val message: String, val location: CompilerMessageLocation? = null) : ReplCompileResult() { override fun toString(): String = "Error(message = \"$message\"" } @@ -125,8 +123,7 @@ sealed class ReplEvalResult : Serializable { sealed class Error(val message: String) : ReplEvalResult() { class Runtime(message: String, val cause: Exception? = null) : Error(message) - class CompileTime(message: String, - val location: CompilerMessageLocation = CompilerMessageLocation.NO_LOCATION) : Error(message) + class CompileTime(message: String, val location: CompilerMessageLocation? = null) : Error(message) override fun toString(): String = "${this::class.simpleName}Error(message = \"$message\"" } @@ -183,4 +180,4 @@ enum class ReplRepeatingMode { interface InvokeWrapper { operator fun invoke(body: () -> T): T // e.g. for capturing io -} \ No newline at end of file +} diff --git a/compiler/cli/src/org/jetbrains/kotlin/cli/common/CLICompiler.java b/compiler/cli/src/org/jetbrains/kotlin/cli/common/CLICompiler.java index c6c02ccc09a..98ecb6693aa 100644 --- a/compiler/cli/src/org/jetbrains/kotlin/cli/common/CLICompiler.java +++ b/compiler/cli/src/org/jetbrains/kotlin/cli/common/CLICompiler.java @@ -77,7 +77,7 @@ public abstract class CLICompiler { Usage.print(errStream, createArguments(), false); } 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; } diff --git a/compiler/cli/src/org/jetbrains/kotlin/cli/common/messages/MessageRenderer.java b/compiler/cli/src/org/jetbrains/kotlin/cli/common/messages/MessageRenderer.java index 0e29862870c..4f00ba3e920 100644 --- a/compiler/cli/src/org/jetbrains/kotlin/cli/common/messages/MessageRenderer.java +++ b/compiler/cli/src/org/jetbrains/kotlin/cli/common/messages/MessageRenderer.java @@ -16,7 +16,6 @@ package org.jetbrains.kotlin.cli.common.messages; -import kotlin.io.FilesKt; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import org.jetbrains.kotlin.utils.fileUtils.FileUtilsKt; @@ -36,7 +35,7 @@ public interface MessageRenderer { }; MessageRenderer PLAIN_FULL_PATHS = new PlainTextMessageRenderer() { - @Nullable + @NotNull @Override protected String getPath(@NotNull CompilerMessageLocation location) { return location.getPath(); @@ -44,20 +43,18 @@ public interface MessageRenderer { }; MessageRenderer PLAIN_RELATIVE_PATHS = new PlainTextMessageRenderer() { - @NotNull private final File cwd = new File(".").getAbsoluteFile(); - @Nullable + @NotNull @Override protected String getPath(@NotNull CompilerMessageLocation location) { - String path = location.getPath(); - return path == null ? path : FileUtilsKt.descendantRelativeTo(new File(path), cwd).getPath(); + return FileUtilsKt.descendantRelativeTo(new File(location.getPath()), cwd).getPath(); } }; 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(); } diff --git a/compiler/cli/src/org/jetbrains/kotlin/cli/common/messages/MessageUtil.java b/compiler/cli/src/org/jetbrains/kotlin/cli/common/messages/MessageUtil.java index b3e653bf4df..9283f84dca4 100644 --- a/compiler/cli/src/org/jetbrains/kotlin/cli/common/messages/MessageUtil.java +++ b/compiler/cli/src/org/jetbrains/kotlin/cli/common/messages/MessageUtil.java @@ -37,7 +37,7 @@ public class MessageUtil { return psiFileToMessageLocation(file, "", DiagnosticUtils.getLineAndColumnInPsiFile(file, element.getTextRange())); } - @NotNull + @Nullable public static CompilerMessageLocation psiFileToMessageLocation( @NotNull PsiFile file, @Nullable String defaultValue, diff --git a/compiler/cli/src/org/jetbrains/kotlin/cli/common/messages/PlainTextMessageRenderer.java b/compiler/cli/src/org/jetbrains/kotlin/cli/common/messages/PlainTextMessageRenderer.java index 60df45690c4..9103f664c2d 100644 --- a/compiler/cli/src/org/jetbrains/kotlin/cli/common/messages/PlainTextMessageRenderer.java +++ b/compiler/cli/src/org/jetbrains/kotlin/cli/common/messages/PlainTextMessageRenderer.java @@ -57,16 +57,14 @@ public abstract class PlainTextMessageRenderer implements MessageRenderer { } @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 result = new StringBuilder(); - int line = location.getLine(); - int column = location.getColumn(); - String lineContent = location.getLineContent(); + int line = location != null ? location.getLine() : -1; + int column = location != null ? location.getColumn() : -1; + String lineContent = location != null ? location.getLineContent() : null; - String path = getPath(location); + String path = location != null ? getPath(location) : null; if (path != null) { result.append(path); result.append(":"); diff --git a/compiler/cli/src/org/jetbrains/kotlin/cli/common/messages/PrintingMessageCollector.java b/compiler/cli/src/org/jetbrains/kotlin/cli/common/messages/PrintingMessageCollector.java index 2826b6e6eab..c1ce58914c9 100644 --- a/compiler/cli/src/org/jetbrains/kotlin/cli/common/messages/PrintingMessageCollector.java +++ b/compiler/cli/src/org/jetbrains/kotlin/cli/common/messages/PrintingMessageCollector.java @@ -48,7 +48,7 @@ public class PrintingMessageCollector implements MessageCollector { hasErrors |= severity.isError(); - errStream.println(messageRenderer.render(severity, message, location != null ? location : CompilerMessageLocation.NO_LOCATION)); + errStream.println(messageRenderer.render(severity, message, location)); } @Override diff --git a/compiler/cli/src/org/jetbrains/kotlin/cli/common/messages/XmlMessageRenderer.java b/compiler/cli/src/org/jetbrains/kotlin/cli/common/messages/XmlMessageRenderer.java index e4251b1058d..fb8d07944c1 100644 --- a/compiler/cli/src/org/jetbrains/kotlin/cli/common/messages/XmlMessageRenderer.java +++ b/compiler/cli/src/org/jetbrains/kotlin/cli/common/messages/XmlMessageRenderer.java @@ -18,6 +18,7 @@ package org.jetbrains.kotlin.cli.common.messages; import com.intellij.openapi.util.text.StringUtil; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; public class XmlMessageRenderer implements MessageRenderer { @Override @@ -26,11 +27,11 @@ public class XmlMessageRenderer implements MessageRenderer { } @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(); String tagName = severity.getPresentableName(); out.append("<").append(tagName); - if (location.getPath() != null) { + if (location != null) { out.append(" path=\"").append(e(location.getPath())).append("\""); out.append(" line=\"").append(location.getLine()).append("\""); out.append(" column=\"").append(location.getColumn()).append("\""); diff --git a/compiler/daemon/src/org/jetbrains/kotlin/daemon/KotlinRemoteReplService.kt b/compiler/daemon/src/org/jetbrains/kotlin/daemon/KotlinRemoteReplService.kt index 53e32ad60b4..ab32141af01 100644 --- a/compiler/daemon/src/org/jetbrains/kotlin/daemon/KotlinRemoteReplService.kt +++ b/compiler/daemon/src/org/jetbrains/kotlin/daemon/KotlinRemoteReplService.kt @@ -98,8 +98,7 @@ open class KotlinJvmReplService( override fun check(state: IReplStageState<*>, codeLine: ReplCodeLine): ReplCheckResult { operationsTracer?.before("check") try { - return replCompiler?.check(state, codeLine) - ?: ReplCheckResult.Error("Initialization error", CompilerMessageLocation.NO_LOCATION) + return replCompiler?.check(state, codeLine) ?: ReplCheckResult.Error("Initialization error") } finally { operationsTracer?.after("check") @@ -109,8 +108,7 @@ open class KotlinJvmReplService( override fun compile(state: IReplStageState<*>, codeLine: ReplCodeLine): ReplCompileResult { operationsTracer?.before("compile") try { - return replCompiler?.compile(state, codeLine) - ?: ReplCompileResult.Error("Initialization error", CompilerMessageLocation.NO_LOCATION) + return replCompiler?.compile(state, codeLine) ?: ReplCompileResult.Error("Initialization error") } finally { operationsTracer?.after("compile") diff --git a/compiler/daemon/src/org/jetbrains/kotlin/daemon/report/CompileServicesFacadeMessageCollector.kt b/compiler/daemon/src/org/jetbrains/kotlin/daemon/report/CompileServicesFacadeMessageCollector.kt index b7f1dc5fb91..43101d8a4dd 100644 --- a/compiler/daemon/src/org/jetbrains/kotlin/daemon/report/CompileServicesFacadeMessageCollector.kt +++ b/compiler/daemon/src/org/jetbrains/kotlin/daemon/report/CompileServicesFacadeMessageCollector.kt @@ -35,7 +35,7 @@ internal class CompileServicesFacadeMessageCollector( } 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) { CompilerMessageSeverity.OUTPUT -> { servicesFacade.report(ReportCategory.OUTPUT_MESSAGE, ReportSeverity.ERROR, message) diff --git a/compiler/tests/org/jetbrains/kotlin/modules/xml/AbstractModuleXmlParserTest.java b/compiler/tests/org/jetbrains/kotlin/modules/xml/AbstractModuleXmlParserTest.java index 14927dfac62..5b4815e4409 100644 --- a/compiler/tests/org/jetbrains/kotlin/modules/xml/AbstractModuleXmlParserTest.java +++ b/compiler/tests/org/jetbrains/kotlin/modules/xml/AbstractModuleXmlParserTest.java @@ -42,9 +42,7 @@ public abstract class AbstractModuleXmlParserTest extends TestCase { public void report( @NotNull CompilerMessageSeverity severity, @NotNull String message, @Nullable CompilerMessageLocation location ) { - throw new AssertionError(MessageRenderer.PLAIN_FULL_PATHS.render( - severity, message, location != null ? location : CompilerMessageLocation.NO_LOCATION - )); + throw new AssertionError(MessageRenderer.PLAIN_FULL_PATHS.render(severity, message, location)); } @Override