0

I have an annotation processor from which I want sometimes to log errors. These errors are logged as such:

processingEnv.getMessager().printMessage(kind, msg, element)

... where processingEnv is an instance of JavacProcessingEnvironment that is injected by the compiler over compilation on the abstract method init of AbstractProcessor.

The problem I'm having is that my message msg is a multi-line message and I want to print it all on the logs when the compilation fails.

However, this doesn't happen. I've debugged a bit, and realized that the concrete instance of JavacMessager behind the getMessager() has an instance of com.sun.tools.javac.util.Log, that is initialized with an instance of JCDiagnostic.Factory, that wraps my error in the following diagnostic:

public JCDiagnostic create(LintCategory lc, Set<DiagnosticFlag> flags, DiagnosticSource source, DiagnosticPosition pos, DiagnosticInfo diagnosticInfo) {
    return new JCDiagnostic(formatter, normalize(diagnosticInfo), lc, flags, source, pos);
}

My problem is that this new JCDiagnostic is by default a non-multiline diagnostic and so when the logger parses my message, it will only print the first line.

I've seen that the class JCDiagnostic is extended by MultilineDiagnostic which I guess is what I want, but I'm not managing to use it nor to find any information on the web about how to use it (even when searching the exact classes that I mentioned in this thread).

Anyone has any idea? I feel like I should configure the ProcessingEnvironment that I receive from the compiler at initialization of my annotation processor, but I don't find any method to do so.

Over simplified skeleton of my annotation processor:

public final class MyProcessor extends AbstractProcessor {

    @Override
    public synchronized void init(ProcessingEnvironment processingEnv) {
        super(processingEnv);
        //I guess I should do something here to configure the logger?
    }

    @Override
    public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
        //do stuff
        //and in case of log needed: 
        processingEnv.getMessager().printMessage(kind, msg, element);
    }

}

If more information is needed to answer the question don't hesitate to ask. Thanks in advance

Matteo NNZ
  • 11,930
  • 12
  • 52
  • 89

0 Answers0