diff --git a/compiler/frontend/src/org/jetbrains/kotlin/diagnostics/DiagnosticFactory.kt b/compiler/frontend/src/org/jetbrains/kotlin/diagnostics/DiagnosticFactory.kt index cf727e2830f43..9ed41ca5589c2 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/diagnostics/DiagnosticFactory.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/diagnostics/DiagnosticFactory.kt @@ -13,82 +13,42 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +package org.jetbrains.kotlin.diagnostics -package org.jetbrains.kotlin.diagnostics; +import org.jetbrains.kotlin.diagnostics.rendering.DiagnosticRenderer +import java.lang.IllegalArgumentException +import java.lang.SafeVarargs -import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; -import org.jetbrains.kotlin.diagnostics.rendering.DiagnosticRenderer; +abstract class DiagnosticFactory protected constructor( + open var name: String?, + open val severity: Severity +) { -import java.util.Arrays; -import java.util.Collection; + open var defaultRenderer: DiagnosticRenderer? = null -public abstract class DiagnosticFactory { + protected constructor(severity: Severity) : this(null, severity) - private String name = null; - private final Severity severity; - - private DiagnosticRenderer defaultRenderer; - - protected DiagnosticFactory(@NotNull Severity severity) { - this.severity = severity; - } - - protected DiagnosticFactory(@NotNull String name, @NotNull Severity severity) { - this.name = name; - this.severity = severity; - } - - /*package*/ void setName(@NotNull String name) { - this.name = name; - } - - @NotNull - public String getName() { - return name; - } - - @NotNull - public Severity getSeverity() { - return severity; + fun cast(diagnostic: Diagnostic): D { + require(!(diagnostic.factory !== this)) { "Factory mismatch: expected " + this + " but was " + diagnostic.factory } + @Suppress("UNCHECKED_CAST") + return diagnostic as D } - @Nullable - public DiagnosticRenderer getDefaultRenderer() { - return defaultRenderer; + override fun toString(): String { + return name ?: "" } - void setDefaultRenderer(@Nullable DiagnosticRenderer defaultRenderer) { - this.defaultRenderer = defaultRenderer; - } - - @NotNull - @SuppressWarnings("unchecked") - public D cast(@NotNull Diagnostic diagnostic) { - if (diagnostic.getFactory() != this) { - throw new IllegalArgumentException("Factory mismatch: expected " + this + " but was " + diagnostic.getFactory()); + companion object { + @SafeVarargs + fun cast(diagnostic: Diagnostic, vararg factories: DiagnosticFactory): D { + return cast(diagnostic, listOf(*factories)) } - return (D) diagnostic; - } - - @NotNull - @SafeVarargs - public static D cast(@NotNull Diagnostic diagnostic, @NotNull DiagnosticFactory... factories) { - return cast(diagnostic, Arrays.asList(factories)); - } - - @NotNull - public static D cast(@NotNull Diagnostic diagnostic, @NotNull Collection> factories) { - for (DiagnosticFactory factory : factories) { - if (diagnostic.getFactory() == factory) return factory.cast(diagnostic); + fun cast(diagnostic: Diagnostic, factories: Collection>): D { + for (factory in factories) { + if (diagnostic.factory === factory) return factory.cast(diagnostic) + } + throw IllegalArgumentException("Factory mismatch: expected one of " + factories + " but was " + diagnostic.factory) } - - throw new IllegalArgumentException("Factory mismatch: expected one of " + factories + " but was " + diagnostic.getFactory()); - } - - @Override - public String toString() { - return getName(); } -} +} \ No newline at end of file