Skip to content

Commit

Permalink
include warnings in Groovy Event Console
Browse files Browse the repository at this point in the history
  • Loading branch information
eric-milles committed Jan 11, 2022
1 parent 4dacd66 commit 1654557
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -830,24 +830,23 @@ 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
if (sourceName != null)
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)
//
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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;
Expand All @@ -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) {
Expand All @@ -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();
}
Expand Down

0 comments on commit 1654557

Please sign in to comment.