From 165455727fcb471a1c4b2babafa7b1314d2c7952 Mon Sep 17 00:00:00 2001 From: Eric Milles Date: Tue, 11 Jan 2022 12:32:55 -0600 Subject: [PATCH] include warnings in Groovy Event Console #1333 --- .../groovy/control/CompilationUnit.java | 11 ++--- .../control/messages/LocatedMessage.java | 2 +- .../transform/LogASTTransformation.java | 2 +- .../groovy/control/CompilationUnit.java | 8 ++-- .../transform/LogASTTransformation.java | 2 +- .../groovy/control/CompilationUnit.java | 8 ++-- .../transform/LogASTTransformation.java | 2 +- .../ast/GroovyErrorCollectorForJDT.java | 48 +++++++++++++------ 8 files changed, 51 insertions(+), 32 deletions(-) diff --git a/base/org.codehaus.groovy25/src/org/codehaus/groovy/control/CompilationUnit.java b/base/org.codehaus.groovy25/src/org/codehaus/groovy/control/CompilationUnit.java index df0518035a..a6be2c6db7 100644 --- a/base/org.codehaus.groovy25/src/org/codehaus/groovy/control/CompilationUnit.java +++ b/base/org.codehaus.groovy25/src/org/codehaus/groovy/control/CompilationUnit.java @@ -830,12 +830,16 @@ protected SourceUnit getSourceUnit() { // because the class may be generated even if a error was found // and that class may have an invalid format we fail here if needed getErrorCollector().failIfErrors(); + // GRECLIPSE add -- if there are errors, don't generate code + // codegen can fail unexpectedly if there was an earlier error + if (source != null && source.getErrorCollector().hasErrors()) return; + // GRECLIPSE end // // Prep the generator machinery // ClassVisitor classVisitor = createClassVisitor(); - + String sourceName = (source == null ? classNode.getModule().getDescription() : source.getName()); // only show the file name and its extension like javac does in its stacktraces rather than the full path // also takes care of both \ and / depending on the host compiling environment @@ -843,11 +847,6 @@ protected SourceUnit getSourceUnit() { sourceName = sourceName.substring(Math.max(sourceName.lastIndexOf('\\'), sourceName.lastIndexOf('/')) + 1); AsmClassGenerator generator = new AsmClassGenerator(source, context, classVisitor, sourceName); - // GRECLIPSE add -- if there are errors, don't generate code - // code gen can fail unexpectedly if there was an earlier error - if (source != null && source.getErrorCollector().hasErrors()) return; - // GRECLIPSE end - // // Run the generation and create the class (if required) // diff --git a/base/org.codehaus.groovy25/src/org/codehaus/groovy/control/messages/LocatedMessage.java b/base/org.codehaus.groovy25/src/org/codehaus/groovy/control/messages/LocatedMessage.java index 03a092846c..2ccd1ccaf7 100644 --- a/base/org.codehaus.groovy25/src/org/codehaus/groovy/control/messages/LocatedMessage.java +++ b/base/org.codehaus.groovy25/src/org/codehaus/groovy/control/messages/LocatedMessage.java @@ -56,7 +56,7 @@ public void write(PrintWriter writer, Janitor janitor) { String sample = source.getSample(line, column, janitor); if (sample != null) { - writer.println(source.getSample(line, column, janitor)); + writer.println(sample); } writer.println(name + ": " + line + ": " + this.message); diff --git a/base/org.codehaus.groovy25/src/org/codehaus/groovy/transform/LogASTTransformation.java b/base/org.codehaus.groovy25/src/org/codehaus/groovy/transform/LogASTTransformation.java index 57bdb365ed..0d406fc05b 100644 --- a/base/org.codehaus.groovy25/src/org/codehaus/groovy/transform/LogASTTransformation.java +++ b/base/org.codehaus.groovy25/src/org/codehaus/groovy/transform/LogASTTransformation.java @@ -113,7 +113,7 @@ public void visitClass(ClassNode node) { if (!logNode.getType().hasClass()) { String span = String.valueOf(new char[nodes[0].getLength()]); Token token = new Token(0, span, nodes[0].getLineNumber(), nodes[0].getColumnNumber()); - sourceUnit.getErrorCollector().addWarning(1, "Unable to resolve class: " + logNode.getType(), token, null); + sourceUnit.getErrorCollector().addWarning(1, "Unable to resolve class: " + logNode.getType(), token, sourceUnit); } // GRECLIPSE end super.visitClass(node); diff --git a/base/org.codehaus.groovy30/src/org/codehaus/groovy/control/CompilationUnit.java b/base/org.codehaus.groovy30/src/org/codehaus/groovy/control/CompilationUnit.java index fef34ffc44..6f0a46505d 100644 --- a/base/org.codehaus.groovy30/src/org/codehaus/groovy/control/CompilationUnit.java +++ b/base/org.codehaus.groovy30/src/org/codehaus/groovy/control/CompilationUnit.java @@ -791,6 +791,10 @@ protected SourceUnit getSourceUnit() { // because the class may be generated even if a error was found // and that class may have an invalid format we fail here if needed getErrorCollector().failIfErrors(); + // GRECLIPSE add -- if there are errors, don't generate code + // codegen can fail unexpectedly if there was an earlier error + if (source != null && source.getErrorCollector().hasErrors()) return; + // GRECLIPSE end // // Prep the generator machinery @@ -803,10 +807,6 @@ protected SourceUnit getSourceUnit() { if (sourceName != null) { sourceName = sourceName.substring(Math.max(sourceName.lastIndexOf('\\'), sourceName.lastIndexOf('/')) + 1); } - // GRECLIPSE add -- if there are errors, don't generate code - // code gen can fail unexpectedly if there was an earlier error - if (source != null && source.getErrorCollector().hasErrors()) return; - // GRECLIPSE end // // Run the generation and create the class (if required) diff --git a/base/org.codehaus.groovy30/src/org/codehaus/groovy/transform/LogASTTransformation.java b/base/org.codehaus.groovy30/src/org/codehaus/groovy/transform/LogASTTransformation.java index 33f8feaed2..1624925de1 100644 --- a/base/org.codehaus.groovy30/src/org/codehaus/groovy/transform/LogASTTransformation.java +++ b/base/org.codehaus.groovy30/src/org/codehaus/groovy/transform/LogASTTransformation.java @@ -126,7 +126,7 @@ public void visitClass(ClassNode node) { if (!logNode.getType().hasClass()) { String span = String.valueOf(new char[nodes[0].getLength()]); Token token = new Token(0, span, nodes[0].getLineNumber(), nodes[0].getColumnNumber()); - sourceUnit.getErrorCollector().addWarning(1, "Unable to resolve class: " + logNode.getType(), token, null); + sourceUnit.getErrorCollector().addWarning(1, "Unable to resolve class: " + logNode.getType(), token, sourceUnit); } // GRECLIPSE end } diff --git a/base/org.codehaus.groovy40/src/org/codehaus/groovy/control/CompilationUnit.java b/base/org.codehaus.groovy40/src/org/codehaus/groovy/control/CompilationUnit.java index 4364e1ae2f..0cc3790260 100644 --- a/base/org.codehaus.groovy40/src/org/codehaus/groovy/control/CompilationUnit.java +++ b/base/org.codehaus.groovy40/src/org/codehaus/groovy/control/CompilationUnit.java @@ -755,6 +755,10 @@ protected SourceUnit getSourceUnit() { // because the class may be generated even if a error was found // and that class may have an invalid format we fail here if needed getErrorCollector().failIfErrors(); + // GRECLIPSE add -- if there are errors, don't generate code + // codegen can fail unexpectedly if there was an earlier error + if (source != null && source.getErrorCollector().hasErrors()) return; + // GRECLIPSE end // // Prep the generator machinery @@ -767,10 +771,6 @@ protected SourceUnit getSourceUnit() { if (sourceName != null) { sourceName = sourceName.substring(Math.max(sourceName.lastIndexOf('\\'), sourceName.lastIndexOf('/')) + 1); } - // GRECLIPSE add -- if there are errors, don't generate code - // code gen can fail unexpectedly if there was an earlier error - if (source != null && source.getErrorCollector().hasErrors()) return; - // GRECLIPSE end // // Run the generation and create the class (if required) diff --git a/base/org.codehaus.groovy40/src/org/codehaus/groovy/transform/LogASTTransformation.java b/base/org.codehaus.groovy40/src/org/codehaus/groovy/transform/LogASTTransformation.java index 109be2f0c2..dc89c87171 100644 --- a/base/org.codehaus.groovy40/src/org/codehaus/groovy/transform/LogASTTransformation.java +++ b/base/org.codehaus.groovy40/src/org/codehaus/groovy/transform/LogASTTransformation.java @@ -137,7 +137,7 @@ public void visitClass(final ClassNode node) { if (!logNode.getType().hasClass()) { // GROOVY-5736 String span = String.valueOf(new char[nodes[0].getLength()]); Token token = new Token(0, span, nodes[0].getLineNumber(), nodes[0].getColumnNumber()); - sourceUnit.getErrorCollector().addWarning(1, "Unable to resolve class: " + logNode.getType(), token, null); + sourceUnit.getErrorCollector().addWarning(1, "Unable to resolve class: " + logNode.getType(), token, sourceUnit); } if (node.getName().endsWith("$Trait$Helper")) { // GROOVY-7439 addGeneratedMethod(node.getOuterClass(), "get" + logFieldName, ACC_PUBLIC, logNode.getType(), Parameter.EMPTY_ARRAY, null, diff --git a/base/org.eclipse.jdt.groovy.core/src/org/codehaus/jdt/groovy/internal/compiler/ast/GroovyErrorCollectorForJDT.java b/base/org.eclipse.jdt.groovy.core/src/org/codehaus/jdt/groovy/internal/compiler/ast/GroovyErrorCollectorForJDT.java index beb84d7802..3e81d6103c 100644 --- a/base/org.eclipse.jdt.groovy.core/src/org/codehaus/jdt/groovy/internal/compiler/ast/GroovyErrorCollectorForJDT.java +++ b/base/org.eclipse.jdt.groovy.core/src/org/codehaus/jdt/groovy/internal/compiler/ast/GroovyErrorCollectorForJDT.java @@ -1,11 +1,11 @@ /* - * Copyright 2009-2018 the original author or authors. + * Copyright 2009-2022 the original author or authors. * * 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 + * https://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, @@ -18,11 +18,11 @@ import java.io.PrintWriter; import java.io.StringWriter; -import org.codehaus.groovy.control.CompilationFailedException; import org.codehaus.groovy.control.CompilerConfiguration; import org.codehaus.groovy.control.ErrorCollector; import org.codehaus.groovy.control.messages.Message; import org.codehaus.groovy.control.messages.SimpleMessage; +import org.codehaus.groovy.control.messages.WarningMessage; import org.codehaus.groovy.eclipse.GroovyLogManager; import org.codehaus.groovy.eclipse.TraceCategory; import org.eclipse.jdt.internal.core.util.Util; @@ -36,22 +36,22 @@ public class GroovyErrorCollectorForJDT extends ErrorCollector { private static final long serialVersionUID = -5358192603029491124L; - public GroovyErrorCollectorForJDT(CompilerConfiguration configuration) { + public GroovyErrorCollectorForJDT(final CompilerConfiguration configuration) { super(configuration); } @Override - public void addErrorAndContinue(Message message) { + public void addErrorAndContinue(final Message message) { try { - String error; - if (message instanceof SimpleMessage) { - error = ((SimpleMessage) message).getMessage(); - } else { - StringWriter writer = new StringWriter(); - message.write(new PrintWriter(writer)); - error = writer.toString().trim(); - } if (GroovyLogManager.manager.hasLoggers()) { + String error; + if (message instanceof SimpleMessage) { + error = ((SimpleMessage) message).getMessage(); + } else { + StringWriter writer = new StringWriter(); + message.write(new PrintWriter(writer)); + error = writer.toString().trim(); + } GroovyLogManager.manager.log(TraceCategory.COMPILER, error); } } catch (Throwable t) { @@ -61,7 +61,27 @@ public void addErrorAndContinue(Message message) { } @Override - protected void failIfErrors() throws CompilationFailedException { + public void addWarning(final WarningMessage message) { + try { + if (GroovyLogManager.manager.hasLoggers()) { + StringWriter writer = new StringWriter(); + message.write(new PrintWriter(writer)); + // reformat to place message first followed by the source sample + String[] lines = writer.toString().substring(9).split(System.lineSeparator()); + if (lines.length < 3) lines = new String[]{"", "", ": "}; // prevent exception + String warning = String.format("Warning: %s%n%s%n%s%n%s", message.getMessage(), + lines[0], lines[1], lines[2].substring(0, lines[2].indexOf(": "))); + + GroovyLogManager.manager.log(TraceCategory.COMPILER, warning.trim()); + } + } catch (Throwable t) { + Util.log(t); + } + super.addWarning(message); + } + + @Override + protected void failIfErrors() { if (transformActive) { super.failIfErrors(); }