org.jetbrains.kotlin.diagnostics.rendering.DiagnosticFactoryToRendererMap Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of kotlin-compiler-embeddable Show documentation
Show all versions of kotlin-compiler-embeddable Show documentation
the Kotlin compiler embeddable
/*
* Copyright 2010-2015 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.kotlin.diagnostics.rendering;
import com.intellij.psi.PsiElement;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.kotlin.config.LanguageVersion;
import org.jetbrains.kotlin.diagnostics.*;
import java.util.HashMap;
import java.util.Map;
public final class DiagnosticFactoryToRendererMap {
private final Map, DiagnosticRenderer>> map = new HashMap<>();
private boolean immutable = false;
// TO catch EA-75872
private final String name;
public DiagnosticFactoryToRendererMap(String name) {
this.name = name;
}
public DiagnosticFactoryToRendererMap() {
this("");
}
@Override
public String toString() {
return "DiagnosticFactory#" + name;
}
private void checkMutability() {
if (immutable) {
throw new IllegalStateException("factory to renderer map is already immutable");
}
}
public void put(@NotNull DiagnosticFactory0 factory, @NotNull String message) {
checkMutability();
map.put(factory, new SimpleDiagnosticRenderer(message));
}
public void put(@NotNull DiagnosticFactory1 factory, @NotNull String message, @Nullable DiagnosticParameterRenderer super A> rendererA) {
checkMutability();
map.put(factory, new DiagnosticWithParameters1Renderer(message, rendererA));
}
public void put(@NotNull DiagnosticFactory1 factory, @NotNull String message, @NotNull MultiRenderer super A> rendererA) {
checkMutability();
map.put(factory, new DiagnosticWithParametersMultiRenderer(message, rendererA));
}
public void put(@NotNull DiagnosticFactory2 factory,
@NotNull String message,
@Nullable DiagnosticParameterRenderer super A> rendererA,
@Nullable DiagnosticParameterRenderer super B> rendererB) {
checkMutability();
map.put(factory, new DiagnosticWithParameters2Renderer(message, rendererA, rendererB));
}
public void put(@NotNull DiagnosticFactory3 factory,
@NotNull String message,
@Nullable DiagnosticParameterRenderer super A> rendererA,
@Nullable DiagnosticParameterRenderer super B> rendererB,
@Nullable DiagnosticParameterRenderer super C> rendererC) {
checkMutability();
map.put(factory, new DiagnosticWithParameters3Renderer(message, rendererA, rendererB, rendererC));
}
public void put(@NotNull DiagnosticFactory4 factory,
@NotNull String message,
@Nullable DiagnosticParameterRenderer super A> rendererA,
@Nullable DiagnosticParameterRenderer super B> rendererB,
@Nullable DiagnosticParameterRenderer super C> rendererC,
@Nullable DiagnosticParameterRenderer super D> rendererD) {
checkMutability();
map.put(factory, new DiagnosticWithParameters4Renderer(message, rendererA, rendererB, rendererC, rendererD));
}
public void put(@NotNull DiagnosticFactory> factory, @NotNull DiagnosticRenderer> renderer) {
checkMutability();
map.put(factory, renderer);
}
public void put(@NotNull DiagnosticFactoryForDeprecation0 factory, @NotNull String message) {
checkMutability();
map.put(factory.getErrorFactory(), new SimpleDiagnosticRenderer(message));
map.put(factory.getWarningFactory(), new SimpleDiagnosticRenderer(deprecationMessage(factory, message)));
}
public void put(@NotNull DiagnosticFactoryForDeprecation1 factory, @NotNull String message, @Nullable DiagnosticParameterRenderer super A> rendererA) {
checkMutability();
map.put(factory.getErrorFactory(), new DiagnosticWithParameters1Renderer(message, rendererA));
map.put(factory.getWarningFactory(), new DiagnosticWithParameters1Renderer(deprecationMessage(factory, message), rendererA));
}
public void put(@NotNull DiagnosticFactoryForDeprecation1 factory, @NotNull String message, @NotNull MultiRenderer super A> rendererA) {
checkMutability();
map.put(factory.getErrorFactory(), new DiagnosticWithParametersMultiRenderer(message, rendererA));
map.put(factory.getWarningFactory(), new DiagnosticWithParametersMultiRenderer(deprecationMessage(factory, message), rendererA));
}
public void put(@NotNull DiagnosticFactoryForDeprecation2 factory,
@NotNull String message,
@Nullable DiagnosticParameterRenderer super A> rendererA,
@Nullable DiagnosticParameterRenderer super B> rendererB) {
checkMutability();
map.put(factory.getErrorFactory(), new DiagnosticWithParameters2Renderer(message, rendererA, rendererB));
map.put(factory.getWarningFactory(), new DiagnosticWithParameters2Renderer(deprecationMessage(factory, message), rendererA, rendererB));
}
public void put(@NotNull DiagnosticFactoryForDeprecation3 factory,
@NotNull String message,
@Nullable DiagnosticParameterRenderer super A> rendererA,
@Nullable DiagnosticParameterRenderer super B> rendererB,
@Nullable DiagnosticParameterRenderer super C> rendererC) {
checkMutability();
map.put(factory.getErrorFactory(), new DiagnosticWithParameters3Renderer(message, rendererA, rendererB, rendererC));
map.put(factory.getWarningFactory(), new DiagnosticWithParameters3Renderer(deprecationMessage(factory, message), rendererA, rendererB, rendererC));
}
public void put(@NotNull DiagnosticFactoryForDeprecation4 factory,
@NotNull String message,
@Nullable DiagnosticParameterRenderer super A> rendererA,
@Nullable DiagnosticParameterRenderer super B> rendererB,
@Nullable DiagnosticParameterRenderer super C> rendererC,
@Nullable DiagnosticParameterRenderer super D> rendererD) {
checkMutability();
map.put(factory.getErrorFactory(), new DiagnosticWithParameters4Renderer(message, rendererA, rendererB, rendererC, rendererD));
map.put(factory.getWarningFactory(), new DiagnosticWithParameters4Renderer(deprecationMessage(factory, message), rendererA, rendererB, rendererC, rendererD));
}
private static String deprecationMessage(DiagnosticFactoryForDeprecation, ?, ?> factory, String errorMessage) {
LanguageVersion sinceVersion = factory.getDeprecatingFeature().getSinceVersion();
StringBuilder builder = new StringBuilder();
builder.append(errorMessage).append(". This will become an error");
if (sinceVersion != null) {
builder.append(" in Kotlin ").append(sinceVersion.getVersionString());
} else {
builder.append(" in a future release");
}
return builder.toString();
}
@Nullable
public DiagnosticRenderer> get(@NotNull DiagnosticFactory> factory) {
return map.get(factory);
}
public void setImmutable() {
immutable = false;
}
}