Skip to content

Commit

Permalink
Parse and forward JavaC 'Note:' level messages
Browse files Browse the repository at this point in the history
Theses messages can be generated by annotation processors using
`printMessage(Diagnostic.Kind.NOTE, "...")`.
  • Loading branch information
mensinda authored and gnodet committed Oct 27, 2023
1 parent 7c0d27d commit 1b578f4
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import org.apache.maven.artifact.versioning.VersionRange;
import org.apache.maven.settings.Settings;
import org.apache.maven.settings.io.xpp3.SettingsXpp3Reader;
import org.codehaus.plexus.compiler.CompilerMessage.Kind;
import org.codehaus.plexus.testing.PlexusTest;
import org.codehaus.plexus.util.FileUtils;
import org.codehaus.plexus.util.ReaderFactory;
Expand Down Expand Up @@ -144,25 +145,24 @@ public void testCompilingSources() throws Exception {

int numCompilerErrors = compilerErrorCount(messages);

int numCompilerWarnings = messages.size() - numCompilerErrors;
int numCompilerWarnings = compilerWarningCount(messages);

int expectedErrors = expectedErrors();
int numCompilerNotes = compilerNoteCount(messages);

int expectedErrors = expectedErrors();
if (expectedErrors != numCompilerErrors) {
System.out.println(numCompilerErrors + " error(s) found:");
List<String> errors = new ArrayList<>();
for (CompilerMessage error : messages) {
if (!error.isError()) {
for (CompilerMessage msg : messages) {
if (msg.getKind() != Kind.ERROR) {
continue;
}

System.out.println("----");
System.out.println(error.getFile());
System.out.println(error.getMessage());
System.out.println(msg.getFile());
System.out.println(msg.getMessage());
System.out.println("----");
errors.add(error.getMessage());
errors.add(msg.getMessage());
}

assertThat(
"Wrong number of compilation errors (" + numCompilerErrors + "/" + expectedErrors //
+ ") : " + displayLines(errors),
Expand All @@ -174,18 +174,16 @@ public void testCompilingSources() throws Exception {
if (expectedWarnings != numCompilerWarnings) {
List<String> warnings = new ArrayList<>();
System.out.println(numCompilerWarnings + " warning(s) found:");
for (CompilerMessage error : messages) {
if (error.isError()) {
for (CompilerMessage msg : messages) {
if (msg.getKind() != Kind.WARNING && msg.getKind() != Kind.MANDATORY_WARNING) {
continue;
}

System.out.println("----");
System.out.println(error.getFile());
System.out.println(error.getMessage());
System.out.println(msg.getFile());
System.out.println(msg.getMessage());
System.out.println("----");
warnings.add(error.getMessage());
warnings.add(msg.getMessage());
}

assertThat(
"Wrong number ("
+ numCompilerWarnings + "/" + expectedWarnings + ") of compilation warnings: "
Expand All @@ -194,6 +192,28 @@ public void testCompilingSources() throws Exception {
is(expectedWarnings));
}

int expectedNotes = expectedNotes();
if (expectedNotes != numCompilerNotes) {
List<String> notes = new ArrayList<>();
System.out.println(numCompilerWarnings + " notes(s) found:");
for (CompilerMessage msg : messages) {
if (msg.getKind() != Kind.NOTE) {
continue;
}
System.out.println("----");
System.out.println(msg.getFile());
System.out.println(msg.getMessage());
System.out.println("----");
notes.add(msg.getMessage());
}
assertThat(
"Wrong number ("
+ numCompilerNotes + "/" + expectedNotes + ") of compilation notes: "
+ displayLines(notes),
numCompilerNotes,
is(expectedNotes));
}

assertThat(
files, containsInAnyOrder(normalizePaths(expectedOutputFiles()).toArray(new String[0])));
}
Expand Down Expand Up @@ -269,14 +289,26 @@ private List<String> normalizePaths(Collection<String> relativePaths) {
.collect(Collectors.toList());
}

protected int compilerErrorCount(List<CompilerMessage> messages) {
int count = 0;
private int compilerErrorCount(List<CompilerMessage> messages) {
return countKind(messages, Kind.ERROR);
}

private int compilerWarningCount(List<CompilerMessage> messages) {
return messages.size() - (compilerErrorCount(messages) + compilerNoteCount(messages));
}

private int compilerNoteCount(List<CompilerMessage> messages) {
return countKind(messages, Kind.NOTE);
}

private int countKind(List<CompilerMessage> messages, Kind kind) {
int c = 0;
for (CompilerMessage message : messages) {
count += message.isError() ? 1 : 0;
if (message.getKind() == kind) {
c++;
}
}

return count;
return c;
}

protected int expectedErrors() {
Expand All @@ -287,6 +319,10 @@ protected int expectedWarnings() {
return 0;
}

protected int expectedNotes() {
return 0;
}

protected Collection<String> expectedOutputFiles() {
return Collections.emptyList();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@ protected int expectedWarnings() {
String javaVersion = getJavaVersion();
if (javaVersion.startsWith("1.8")) {
return 1;
} else if (javaVersion.contains("18") || javaVersion.contains("19") || javaVersion.contains("20")) {
} else if (javaVersion.contains("18")
|| javaVersion.contains("19")
|| javaVersion.contains("20")
|| javaVersion.contains("21")) {
return 5;
} else if (javaVersion.contains("21")) {
return 6;
}
return 2;
}
Expand All @@ -30,6 +31,15 @@ protected int expectedErrors() {
return 1;
}

@Override
protected int expectedNotes() {
String javaVersion = getJavaVersion();
if (javaVersion.contains("21")) {
return 1;
}
return 0;
}

@Override
public String getSourceVersion() {
return "1.8";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -697,7 +697,7 @@ static List<CompilerMessage> parseModernStream(int exitCode, BufferedReader inpu
} else if ((buffer.length() == 0) && line.startsWith("warning: ")) {
errors.add(new CompilerMessage(line, CompilerMessage.Kind.WARNING));
} else if ((buffer.length() == 0) && isNote(line)) {
// skip, JDK 1.5 telling us deprecated APIs are used but -Xlint:deprecation isn't set
errors.add(new CompilerMessage(line, CompilerMessage.Kind.NOTE));
} else if ((buffer.length() == 0) && isMisc(line)) {
// verbose output was set
errors.add(new CompilerMessage(line, CompilerMessage.Kind.OTHER));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,15 @@ protected int expectedWarnings() {
return 2;
}

@Override
protected int expectedNotes() {
String javaVersion = getJavaVersion();
if (javaVersion.contains("21")) {
return 7;
}
return 0;
}

@Override
public String getTargetVersion() {
String javaVersion = getJavaVersion();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,16 @@ public void testErrorMessage() {
assertThat(compilerError.getEndLine(), is(7));
}

@Test
public void testNoteMessage() throws IOException {
String error = "Note: My fancy annotation processor info" + EOL;
List<CompilerMessage> messages =
JavacCompiler.parseModernStream(0, new BufferedReader(new StringReader(error)));
assertThat(messages.size(), is(1));
assertThat(messages.get(0).isError(), is(false));
assertThat(messages.get(0).getMessage(), is("My fancy annotation processor info"));
}

@Test
public void testUnknownSymbolError() {
String error = "./org/codehaus/foo/UnknownSymbol.java:7: cannot find symbol" + EOL + "symbol : method foo()"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,4 @@
public class JavaxToolsCompilerTest extends AbstractJavacCompilerTest {
// no op default is to javax.tools if available

@Override
protected int expectedWarnings() {
String javaVersion = getJavaVersion();
if (javaVersion.contains("21")) {
return 8;
} else {
return super.expectedWarnings();
}
}
}

0 comments on commit 1b578f4

Please sign in to comment.