From 6a13510741dacb4f74a3a1d146a0a95a99de6989 Mon Sep 17 00:00:00 2001 From: Evgeny Gerashchenko Date: Thu, 12 Apr 2012 13:56:53 +0400 Subject: [PATCH] Replaced Diagnostic.getMessage() with DiagnosticRenderer.render() calls. Made Diagnostic.getMessage() deprecated, introduced DiagnosticRender interface and default implementation for it. --- .../jet/compiler/CompileSession.java | 7 +-- .../compiler/DefaultDiagnosticRenderer.java | 39 ++++++++++++++++ .../jet/lang/diagnostics/Diagnostic.java | 1 + .../lang/diagnostics/DiagnosticHolder.java | 3 +- .../lang/diagnostics/DiagnosticRenderer.java | 30 ++++++++++++ .../tests/org/jetbrains/jet/JetTestUtils.java | 6 ++- .../jet/plugin/highlighter/JetPsiChecker.java | 10 ++-- idea/testData/checker/infos/Autocasts.jet | 46 +++++++++---------- 8 files changed, 107 insertions(+), 35 deletions(-) create mode 100644 compiler/cli/src/org/jetbrains/jet/compiler/DefaultDiagnosticRenderer.java create mode 100644 compiler/frontend/src/org/jetbrains/jet/lang/diagnostics/DiagnosticRenderer.java diff --git a/compiler/cli/src/org/jetbrains/jet/compiler/CompileSession.java b/compiler/cli/src/org/jetbrains/jet/compiler/CompileSession.java index 952f4cdb2c7..1e26ccf2fa9 100644 --- a/compiler/cli/src/org/jetbrains/jet/compiler/CompileSession.java +++ b/compiler/cli/src/org/jetbrains/jet/compiler/CompileSession.java @@ -35,10 +35,7 @@ import org.jetbrains.jet.lang.descriptors.ClassDescriptor; import org.jetbrains.jet.lang.descriptors.ClassOrNamespaceDescriptor; import org.jetbrains.jet.lang.descriptors.DeclarationDescriptor; import org.jetbrains.jet.lang.descriptors.ModuleDescriptor; -import org.jetbrains.jet.lang.diagnostics.Diagnostic; -import org.jetbrains.jet.lang.diagnostics.SimpleDiagnosticFactory; -import org.jetbrains.jet.lang.diagnostics.DiagnosticUtils; -import org.jetbrains.jet.lang.diagnostics.Severity; +import org.jetbrains.jet.lang.diagnostics.*; import org.jetbrains.jet.lang.psi.JetFile; import org.jetbrains.jet.lang.resolve.BindingContext; import org.jetbrains.jet.lang.resolve.java.AnalyzerFacadeForJVM; @@ -150,7 +147,7 @@ public class CompileSession { DiagnosticUtils.LineAndColumn lineAndColumn = DiagnosticUtils.getLineAndColumn(diagnostic); VirtualFile virtualFile = diagnostic.getPsiFile().getVirtualFile(); String path = virtualFile == null ? null : virtualFile.getPath(); - collector.report(diagnostic.getSeverity(), diagnostic.getMessage(), path, lineAndColumn.getLine(), lineAndColumn.getColumn()); + collector.report(diagnostic.getSeverity(), DefaultDiagnosticRenderer.INSTANCE.render(diagnostic), path, lineAndColumn.getLine(), lineAndColumn.getColumn()); } @NotNull diff --git a/compiler/cli/src/org/jetbrains/jet/compiler/DefaultDiagnosticRenderer.java b/compiler/cli/src/org/jetbrains/jet/compiler/DefaultDiagnosticRenderer.java new file mode 100644 index 00000000000..55d4b2f9d8e --- /dev/null +++ b/compiler/cli/src/org/jetbrains/jet/compiler/DefaultDiagnosticRenderer.java @@ -0,0 +1,39 @@ +/* + * Copyright 2010-2012 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.jet.compiler; + +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; +import org.jetbrains.jet.lang.diagnostics.Diagnostic; +import org.jetbrains.jet.lang.diagnostics.DiagnosticRenderer; + +/** + * @author Evgeny Gerashchenko + * @since 4/12/12 + */ +public class DefaultDiagnosticRenderer implements DiagnosticRenderer { + public static final DefaultDiagnosticRenderer INSTANCE = new DefaultDiagnosticRenderer(); + + private DefaultDiagnosticRenderer() { + } + + @NotNull + @Override + public String render(@Nullable Diagnostic diagnostic) { + return diagnostic.getMessage(); + } +} diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/diagnostics/Diagnostic.java b/compiler/frontend/src/org/jetbrains/jet/lang/diagnostics/Diagnostic.java index d4369a0513d..58049c26521 100644 --- a/compiler/frontend/src/org/jetbrains/jet/lang/diagnostics/Diagnostic.java +++ b/compiler/frontend/src/org/jetbrains/jet/lang/diagnostics/Diagnostic.java @@ -31,6 +31,7 @@ public interface Diagnostic { @NotNull AbstractDiagnosticFactory getFactory(); + @Deprecated @NotNull String getMessage(); diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/diagnostics/DiagnosticHolder.java b/compiler/frontend/src/org/jetbrains/jet/lang/diagnostics/DiagnosticHolder.java index f0c110e14bd..c1f1472d2c5 100644 --- a/compiler/frontend/src/org/jetbrains/jet/lang/diagnostics/DiagnosticHolder.java +++ b/compiler/frontend/src/org/jetbrains/jet/lang/diagnostics/DiagnosticHolder.java @@ -17,7 +17,6 @@ package org.jetbrains.jet.lang.diagnostics; import com.intellij.openapi.util.TextRange; -import com.intellij.psi.PsiElement; import com.intellij.psi.PsiFile; import org.jetbrains.annotations.NotNull; @@ -38,7 +37,7 @@ public interface DiagnosticHolder { if (diagnostic.getSeverity() == Severity.ERROR) { PsiFile psiFile = diagnostic.getPsiFile(); List textRanges = diagnostic.getTextRanges(); - throw new IllegalStateException(diagnostic.getMessage() + DiagnosticUtils.atLocation(psiFile, textRanges.get(0))); + throw new IllegalStateException(diagnostic.getFactory().getName() + DiagnosticUtils.atLocation(psiFile, textRanges.get(0))); } } }; diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/diagnostics/DiagnosticRenderer.java b/compiler/frontend/src/org/jetbrains/jet/lang/diagnostics/DiagnosticRenderer.java new file mode 100644 index 00000000000..d820c6cdf11 --- /dev/null +++ b/compiler/frontend/src/org/jetbrains/jet/lang/diagnostics/DiagnosticRenderer.java @@ -0,0 +1,30 @@ +/* + * Copyright 2010-2012 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.jet.lang.diagnostics; + +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +/** + * @author Evgeny Gerashchenko + * @since 4/12/12 + */ +public interface DiagnosticRenderer extends Renderer { + @NotNull + @Override + String render(@Nullable Diagnostic diagnostic); +} diff --git a/compiler/tests/org/jetbrains/jet/JetTestUtils.java b/compiler/tests/org/jetbrains/jet/JetTestUtils.java index 7ce129cc6d8..8bcd54cc0c8 100644 --- a/compiler/tests/org/jetbrains/jet/JetTestUtils.java +++ b/compiler/tests/org/jetbrains/jet/JetTestUtils.java @@ -26,6 +26,7 @@ import org.jetbrains.annotations.NotNull; import org.jetbrains.jet.analyzer.AnalyzeExhaust; import org.jetbrains.jet.compiler.JetCoreEnvironment; import org.jetbrains.jet.lang.cfg.pseudocode.JetControlFlowDataTraceFactory; +import org.jetbrains.jet.compiler.DefaultDiagnosticRenderer; import org.jetbrains.jet.lang.diagnostics.Diagnostic; import org.jetbrains.jet.lang.diagnostics.Severity; import org.jetbrains.jet.lang.diagnostics.UnresolvedReferenceDiagnostic; @@ -153,11 +154,14 @@ public class JetTestUtils { @Override public void report(@NotNull Diagnostic diagnostic) { if (diagnostic.getSeverity() == Severity.ERROR) { - throw new IllegalStateException(diagnostic.getMessage()); + throw new IllegalStateException(DefaultDiagnosticRenderer.INSTANCE.render(diagnostic)); } } }; + private JetTestUtils() { + } + public static AnalyzeExhaust analyzeFile(@NotNull JetFile namespace, @NotNull JetControlFlowDataTraceFactory flowDataTraceFactory) { return AnalyzerFacadeForJVM.analyzeOneFileWithJavaIntegration(namespace, flowDataTraceFactory, CompileCompilerDependenciesTest.compilerDependenciesForTests(CompilerSpecialMode.REGULAR)); diff --git a/idea/src/org/jetbrains/jet/plugin/highlighter/JetPsiChecker.java b/idea/src/org/jetbrains/jet/plugin/highlighter/JetPsiChecker.java index 3fcbddeae37..4bea587e9cd 100644 --- a/idea/src/org/jetbrains/jet/plugin/highlighter/JetPsiChecker.java +++ b/idea/src/org/jetbrains/jet/plugin/highlighter/JetPsiChecker.java @@ -33,6 +33,7 @@ import com.intellij.psi.PsiReference; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.TestOnly; +import org.jetbrains.jet.compiler.DefaultDiagnosticRenderer; import org.jetbrains.jet.lang.diagnostics.*; import org.jetbrains.jet.lang.psi.JetFile; import org.jetbrains.jet.lang.psi.JetReferenceExpression; @@ -154,7 +155,7 @@ public class JetPsiChecker implements Annotator { for (TextRange range : mrr.getRanges()) { Annotation annotation = holder.createErrorAnnotation( range.shiftRight(referenceExpression.getTextOffset()), - diagnostic.getMessage()); + getMessage(diagnostic)); registerQuickFix(annotation, diagnostic); @@ -163,7 +164,7 @@ public class JetPsiChecker implements Annotator { } else { for (TextRange textRange : textRanges) { - Annotation annotation = holder.createErrorAnnotation(textRange, diagnostic.getMessage()); + Annotation annotation = holder.createErrorAnnotation(textRange, getMessage(diagnostic)); registerQuickFix(annotation, diagnostic); annotation.setHighlightType(ProblemHighlightType.LIKE_UNKNOWN_SYMBOL); } @@ -239,10 +240,11 @@ public class JetPsiChecker implements Annotator { @NotNull private static String getMessage(@NotNull Diagnostic diagnostic) { + String message = DefaultDiagnosticRenderer.INSTANCE.render(diagnostic); if (ApplicationManager.getApplication().isInternal() || ApplicationManager.getApplication().isUnitTestMode()) { - return "[" + diagnostic.getFactory().getName() + "] " + diagnostic.getMessage(); + return "[" + diagnostic.getFactory().getName() + "] " + message; } - return diagnostic.getMessage(); + return message; } @Nullable diff --git a/idea/testData/checker/infos/Autocasts.jet b/idea/testData/checker/infos/Autocasts.jet index 9bd17ee710a..b665d066470 100644 --- a/idea/testData/checker/infos/Autocasts.jet +++ b/idea/testData/checker/infos/Autocasts.jet @@ -8,19 +8,19 @@ class B() : A() { fun f9(a : A?) { a?.foo() - a?.bar() + a?.bar() if (a is B) { a.bar() a.foo() } a?.foo() - a?.bar() + a?.bar() if (!(a is B)) { - a?.bar() + a?.bar() a?.foo() } if (!(a is B) || a.bar() == #()) { - a?.bar() + a?.bar() } if (!(a is B)) { return; @@ -55,7 +55,7 @@ fun f11(a : A?) { is B -> a.bar() is A -> a.foo() is Any -> a.foo() - is Any? -> a.bar() + is Any? -> a.bar() else -> a?.foo() } } @@ -65,7 +65,7 @@ fun f12(a : A?) { is B -> a.bar() is A -> a.foo() is Any -> a.foo(); - is Any? -> a.bar() + is Any? -> a.bar() is val c : B -> c.foo() is val c is C -> c.bar() is val c is C -> a.bar() @@ -73,7 +73,7 @@ fun f12(a : A?) { } if (a is val b) { - a?.bar() + a?.bar() b?.foo() } if (a is val b is B) { @@ -90,17 +90,17 @@ fun f13(a : A?) { } else { a?.foo() - c.bar() + c.bar() } a?.foo() if (!(a is val c is B)) { a?.foo() - c.bar() + c.bar() } else { a.foo() - c.bar() + c.bar() } a?.foo() @@ -110,16 +110,16 @@ fun f13(a : A?) { } else { a?.foo() - c.bar() + c.bar() } if (!(a is val c is B) || !(a is val x is C)) { - x - c + x + c } else { - x - c + x + c } if (!(a is val c is B) || !(a is val c is C)) { @@ -127,21 +127,21 @@ fun f13(a : A?) { if (!(a is val c is B)) return a.bar() - c.foo() - c.bar() + c.foo() + c.bar() } fun f14(a : A?) { while (!(a is val c is B)) { } a.bar() - c.bar() + c.bar() } fun f15(a : A?) { do { } while (!(a is val c is B)) a.bar() - c.bar() + c.bar() } fun getStringLength(obj : Any) : Char? { @@ -217,20 +217,20 @@ fun declarationInsidePattern(x: #(Any, Any)): String = when(x) { is #(val a is S fun mergeAutocasts(a: Any?) { if (a is String || a is Int) { - a.compareTo("") + a.compareTo("") a.toString() } if (a is Int || a is String) { - a.compareTo("") + a.compareTo("") } when (a) { - is String, is Any -> a.compareTo("") + is String, is Any -> a.compareTo("") } if (a is String && a is Any) { val i: Int = a.compareTo("") } if (a is String && a.compareTo("") == 0) {} - if (a is String || a.compareTo("") == 0) {} + if (a is String || a.compareTo("") == 0) {} } //mutability