Skip to content

Commit

Permalink
Remove logger usage to converge to the annotation processor messager
Browse files Browse the repository at this point in the history
This brings more consistent message and error reporting than what JDK logging does,
and it does not mess with build tools output.
  • Loading branch information
jponge committed Jan 18, 2025
1 parent a9c3969 commit 248bd84
Show file tree
Hide file tree
Showing 8 changed files with 19 additions and 12 deletions.
1 change: 1 addition & 0 deletions vertx-codegen-json/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
<execution>
<id>default-compile</id>
<configuration>
<showWarnings>true</showWarnings>
<annotationProcessorPaths>
<annotationProcessorPath>
<groupId>io.vertx</groupId>
Expand Down
1 change: 1 addition & 0 deletions vertx-codegen-processor/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ You can configure the `>io.vertx.codegen.processor.Processor` as any Java annota
<encoding>${project.build.sourceEncoding}</encoding>
<!-- Important: there are issues with apt and incremental compilation in the maven-compiler-plugin -->
<useIncrementalCompilation>false</useIncrementalCompilation>
<showWarnings>true</showWarnings>
</configuration>
<executions>
<execution>
Expand Down
1 change: 1 addition & 0 deletions vertx-codegen-processor/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@
<classpathElement>${project.build.directory}/test-classpath</classpathElement>
<classpathElement>${project.basedir}/src/tck/resources</classpathElement>
</classpathElements>
<outputDiagnostics>true</outputDiagnostics>
<processors>
<processor>io.vertx.codegen.processor.Processor</processor>
</processors>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
import javax.lang.model.util.Types;
import javax.tools.Diagnostic;
import java.util.*;
import java.util.logging.Logger;
import java.util.stream.Collectors;
import java.util.stream.Stream;

Expand All @@ -54,7 +53,6 @@ public class ClassModel implements Model {
public static final String ITERATOR = "java.util.Iterator";
public static final String FUNCTION = "java.util.function.Function";
public static final String SUPPLIER = "java.util.function.Supplier";
private static final Logger logger = Logger.getLogger(ClassModel.class.getName());

protected final ProcessingEnvironment env;
protected final AnnotationValueInfoFactory annotationValueInfoFactory;
Expand Down Expand Up @@ -697,7 +695,7 @@ private MethodInfo createMethod(ExecutableElement modelMethod, boolean allowAnyJ
String msg = "Interface " + modelElt + " does not redeclare the @Fluent return type " +
" of method " + modelMethod + " declared by " + declaringElt;
messager.printMessage(Diagnostic.Kind.WARNING, msg, modelElt, fluentAnnotation);
logger.warning(msg);
env.getMessager().printMessage(Diagnostic.Kind.WARNING, msg);
} else {
TypeMirror fluentType = modelMethod.getReturnType();
if (!typeUtils.isAssignable(fluentType, modelElt.asType())) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import java.util.*;
import java.util.function.Predicate;
import java.util.function.Supplier;
import java.util.logging.Logger;
import java.util.stream.Stream;

/**
Expand All @@ -46,7 +45,6 @@ public class CodeGen {
PROVIDERS = list;
}

private static final Logger logger = Logger.getLogger(CodeGen.class.getName());
final static Map<ProcessingEnvironment, ClassLoader> loaderMap = new WeakHashMap<>();

private final Map<String, Map<String, Map.Entry<TypeElement, Model>>> models = new HashMap<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@
import java.nio.file.Path;
import java.util.*;
import java.util.function.Predicate;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.Stream;
Expand All @@ -37,7 +35,6 @@
public class Processor extends AbstractProcessor {

private static final String JSON_MAPPERS_PROPERTIES_PATH = "META-INF/vertx/json-mappers.properties";
public static final Logger log = Logger.getLogger(Processor.class.getName());
private File outputDirectory;
private List<? extends Generator<?>> codeGenerators;
private Map<String, GeneratedFile> generatedResources = new HashMap<>();
Expand Down Expand Up @@ -155,6 +152,15 @@ private Path determineSourcePathInEclipse() {
return null;
}

private String throwableToMessageString(Throwable e) {
StringBuilder builder = new StringBuilder();
builder.append(e.getMessage()).append("\n");
for (StackTraceElement stackTraceElement : e.getStackTrace()) {
builder.append(" ").append(stackTraceElement.toString()).append("\n");
}
return builder.toString();
}

private List<CodeGen.Converter> loadJsonMappers() {
Exception exception = null;
List<CodeGen.Converter> merged = new ArrayList<>();
Expand Down Expand Up @@ -199,7 +205,7 @@ private List<CodeGen.Converter> loadJsonMappers() {
loadJsonMappers(merged, is);
processingEnv.getMessager().printMessage(Diagnostic.Kind.NOTE, "Loaded json-mappers.properties from '" + source + "'");
} catch (IOException e) {
log.log(Level.SEVERE, "Could not load json-mappers.properties", e);
processingEnv.getMessager().printMessage(Diagnostic.Kind.MANDATORY_WARNING, "Could not load json-mappers.properties: " + throwableToMessageString(e));
processingEnv.getMessager().printMessage(Diagnostic.Kind.WARNING, "Unable to open properties file at " + source);
}
}
Expand All @@ -220,7 +226,7 @@ private List<CodeGen.Converter> loadJsonMappers() {
loadJsonMappers(merged, is);
processingEnv.getMessager().printMessage(Diagnostic.Kind.NOTE, "Loaded json-mappers.properties from '" + source + "'");
} catch (IOException e) {
log.log(Level.SEVERE, "Could not load json-mappers.properties", e);
processingEnv.getMessager().printMessage(Diagnostic.Kind.MANDATORY_WARNING, "Could not load json-mappers.properties: " + throwableToMessageString(e));
processingEnv.getMessager().printMessage(Diagnostic.Kind.WARNING, "Unable to open properties file at " + source);
}
}
Expand Down Expand Up @@ -342,13 +348,13 @@ private void reportGenException(GenException e) {
name = e.element.getEnclosingElement() + "#" + name;
}
String msg = "Could not generate model for " + name + ": " + e.msg;
log.log(Level.SEVERE, msg, e);
processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, msg + "\nCaused by: " + throwableToMessageString(e));
processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, msg, e.element);
}

private void reportException(Exception e, Element elt) {
String msg = "Could not generate element for " + elt + ": " + e.getMessage();
log.log(Level.SEVERE, msg, e);
processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, msg + "\nCaused by: " + throwableToMessageString(e));
processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, msg, elt);
}

Expand Down
1 change: 1 addition & 0 deletions vertx-codegen-protobuf/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@
<classifier>processor</classifier>
</annotationProcessorPath>
</annotationProcessorPaths>
<showWarnings>true</showWarnings>
<compilerArgs>
<arg>-Adocgen.source=${asciidoc.dir}</arg>
<arg>-Adocgen.output=${project.build.directory}/asciidoc/java</arg>
Expand Down
1 change: 1 addition & 0 deletions vertx-codegen-protobuf/src/main/asciidoc/index.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ Lombok uses internal compiler API to update Abstract Syntax Tree of the compiler
<version>${vertx.version}</version>
</path>
</annotationProcessorPaths>
<showWarnings>true</showWarnings>
</configuration>
</plugin>
----
Expand Down

0 comments on commit 248bd84

Please sign in to comment.