Skip to content

Commit

Permalink
Issue codehaus-plexus#106: Retain error messages from annotation proc…
Browse files Browse the repository at this point in the history
…essors
  • Loading branch information
mthmulders committed Feb 8, 2022
1 parent 082f454 commit ea7e254
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -755,6 +755,10 @@ else if ( hasPointer )
errors.add( new CompilerMessage( buffer.toString(), CompilerMessage.Kind.ERROR ) );
return errors;
}
else if ( line.startsWith( "An annotation processor threw an uncaught exception." ) ) {
CompilerMessage annotationProcessingError = parseAnnotationProcessorStream( input );
errors.add( annotationProcessingError );
}

// new error block?
if ( !line.startsWith( " " ) && hasPointer )
Expand Down Expand Up @@ -800,6 +804,23 @@ else if ( ( buffer.length() == 0 ) && isMisc( line ) )
}
}

private static CompilerMessage parseAnnotationProcessorStream( final BufferedReader input )
throws IOException
{
String line = input.readLine();
final StringBuilder buffer = new StringBuilder();

while (line != null) {
if (!line.startsWith( "Consult the following stack trace for details." )) {
buffer.append(line);
buffer.append(EOL);
}
line = input.readLine();
}

return new CompilerMessage( buffer.toString(), CompilerMessage.Kind.ERROR );
}

private static boolean isMisc( String line )
{
return startsWithPrefix( line, MISC_PREFIXES );
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
package org.codehaus.plexus.compiler.javac;

import org.codehaus.plexus.compiler.CompilerMessage;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.StringReader;
import java.util.List;

import static org.assertj.core.api.Assertions.assertThat;

/*
* Licensed to the Apache Software Foundation (ASF) under one
Expand Down Expand Up @@ -33,4 +42,36 @@ public void setUp()
super.setUp();
setForceJavacCompilerUse( true );
}

@Test
void parseModernStream_withAnnotationProcessingErrors() throws IOException
{
String input =
"\n" +
"\n" +
"An annotation processor threw an uncaught exception.\n" +
"Consult the following stack trace for details.\n" +
"java.lang.IllegalAccessError: class lombok.javac.apt.LombokProcessor (in unnamed module @0x1da51a35) cannot access class com.sun.tools.javac.processing.JavacProcessingEnvironment (in module jdk.compiler) because module jdk.compiler does not export com.sun.tools.javac.processing to unnamed module @0x1da51a35\n" +
"\tat lombok.javac.apt.LombokProcessor.getJavacProcessingEnvironment(LombokProcessor.java:433)\n" +
"\tat lombok.javac.apt.LombokProcessor.init(LombokProcessor.java:92)\n" +
"\tat lombok.core.AnnotationProcessor$JavacDescriptor.want(AnnotationProcessor.java:160)\n" +
"\tat lombok.core.AnnotationProcessor.init(AnnotationProcessor.java:213)\n" +
"\tat lombok.launch.AnnotationProcessorHider$AnnotationProcessor.init(AnnotationProcessor.java:64)\n" +
"\tat jdk.compiler/com.sun.tools.javac.processing.JavacProcessingEnvironment$ProcessorState.<init>(JavacProcessingEnvironment.java:702)\n" +
"\tat jdk.compiler/com.sun.tools.javac.processing.JavacProcessingEnvironment$DiscoveredProcessors$ProcessorStateIterator.next(JavacProcessingEnvironment.java:829)\n" +
"\tat jdk.compiler/com.sun.tools.javac.processing.JavacProcessingEnvironment.discoverAndRunProcs(JavacProcessingEnvironment.java:925)\n" +
"\tat jdk.compiler/com.sun.tools.javac.processing.JavacProcessingEnvironment$Round.run(JavacProcessingEnvironment.java:1269)\n" +
"\tat jdk.compiler/com.sun.tools.javac.processing.JavacProcessingEnvironment.doProcessing(JavacProcessingEnvironment.java:1384)\n" +
"\tat jdk.compiler/com.sun.tools.javac.main.JavaCompiler.processAnnotations(JavaCompiler.java:1261)\n" +
"\tat jdk.compiler/com.sun.tools.javac.main.JavaCompiler.compile(JavaCompiler.java:935)\n" +
"\tat jdk.compiler/com.sun.tools.javac.main.Main.compile(Main.java:317)\n" +
"\tat jdk.compiler/com.sun.tools.javac.main.Main.compile(Main.java:176)\n" +
"\tat jdk.compiler/com.sun.tools.javac.Main.compile(Main.java:64)\n" +
"\tat jdk.compiler/com.sun.tools.javac.Main.main(Main.java:50)\n";

List<CompilerMessage> compilerMessages = JavacCompiler.parseModernStream( 1,
new BufferedReader( new StringReader( input ) ) );

assertThat( compilerMessages ).hasSize( 1 );
}
}

0 comments on commit ea7e254

Please sign in to comment.