From 1b578f496fcbac6b0960b062806dff03e75a295c Mon Sep 17 00:00:00 2001 From: Daniel Mensinger Date: Tue, 30 May 2023 10:58:24 +0200 Subject: [PATCH] Parse and forward JavaC 'Note:' level messages Theses messages can be generated by annotation processors using `printMessage(Diagnostic.Kind.NOTE, "...")`. --- .../plexus/compiler/AbstractCompilerTest.java | 78 ++++++++++++++----- .../javac/JavacErrorProneCompilerTest.java | 16 +++- .../plexus/compiler/javac/JavacCompiler.java | 2 +- .../javac/AbstractJavacCompilerTest.java | 9 +++ .../javac/ErrorMessageParserTest.java | 10 +++ .../javac/JavaxToolsCompilerTest.java | 9 --- 6 files changed, 90 insertions(+), 34 deletions(-) diff --git a/plexus-compiler-test/src/main/java/org/codehaus/plexus/compiler/AbstractCompilerTest.java b/plexus-compiler-test/src/main/java/org/codehaus/plexus/compiler/AbstractCompilerTest.java index 977ffbd1..1c42dc70 100644 --- a/plexus-compiler-test/src/main/java/org/codehaus/plexus/compiler/AbstractCompilerTest.java +++ b/plexus-compiler-test/src/main/java/org/codehaus/plexus/compiler/AbstractCompilerTest.java @@ -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; @@ -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 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), @@ -174,18 +174,16 @@ public void testCompilingSources() throws Exception { if (expectedWarnings != numCompilerWarnings) { List 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: " @@ -194,6 +192,28 @@ public void testCompilingSources() throws Exception { is(expectedWarnings)); } + int expectedNotes = expectedNotes(); + if (expectedNotes != numCompilerNotes) { + List 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]))); } @@ -269,14 +289,26 @@ private List normalizePaths(Collection relativePaths) { .collect(Collectors.toList()); } - protected int compilerErrorCount(List messages) { - int count = 0; + private int compilerErrorCount(List messages) { + return countKind(messages, Kind.ERROR); + } + + private int compilerWarningCount(List messages) { + return messages.size() - (compilerErrorCount(messages) + compilerNoteCount(messages)); + } + + private int compilerNoteCount(List messages) { + return countKind(messages, Kind.NOTE); + } + private int countKind(List 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() { @@ -287,6 +319,10 @@ protected int expectedWarnings() { return 0; } + protected int expectedNotes() { + return 0; + } + protected Collection expectedOutputFiles() { return Collections.emptyList(); } diff --git a/plexus-compilers/plexus-compiler-javac-errorprone/src/test/java/org/codehaus/plexus/compiler/javac/JavacErrorProneCompilerTest.java b/plexus-compilers/plexus-compiler-javac-errorprone/src/test/java/org/codehaus/plexus/compiler/javac/JavacErrorProneCompilerTest.java index 416b0310..16e014d3 100644 --- a/plexus-compilers/plexus-compiler-javac-errorprone/src/test/java/org/codehaus/plexus/compiler/javac/JavacErrorProneCompilerTest.java +++ b/plexus-compilers/plexus-compiler-javac-errorprone/src/test/java/org/codehaus/plexus/compiler/javac/JavacErrorProneCompilerTest.java @@ -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; } @@ -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"; diff --git a/plexus-compilers/plexus-compiler-javac/src/main/java/org/codehaus/plexus/compiler/javac/JavacCompiler.java b/plexus-compilers/plexus-compiler-javac/src/main/java/org/codehaus/plexus/compiler/javac/JavacCompiler.java index dbee1de9..23ff8987 100644 --- a/plexus-compilers/plexus-compiler-javac/src/main/java/org/codehaus/plexus/compiler/javac/JavacCompiler.java +++ b/plexus-compilers/plexus-compiler-javac/src/main/java/org/codehaus/plexus/compiler/javac/JavacCompiler.java @@ -697,7 +697,7 @@ static List 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)); diff --git a/plexus-compilers/plexus-compiler-javac/src/test/java/org/codehaus/plexus/compiler/javac/AbstractJavacCompilerTest.java b/plexus-compilers/plexus-compiler-javac/src/test/java/org/codehaus/plexus/compiler/javac/AbstractJavacCompilerTest.java index 9cb656a5..6c269e90 100644 --- a/plexus-compilers/plexus-compiler-javac/src/test/java/org/codehaus/plexus/compiler/javac/AbstractJavacCompilerTest.java +++ b/plexus-compilers/plexus-compiler-javac/src/test/java/org/codehaus/plexus/compiler/javac/AbstractJavacCompilerTest.java @@ -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(); diff --git a/plexus-compilers/plexus-compiler-javac/src/test/java/org/codehaus/plexus/compiler/javac/ErrorMessageParserTest.java b/plexus-compilers/plexus-compiler-javac/src/test/java/org/codehaus/plexus/compiler/javac/ErrorMessageParserTest.java index bfc416a4..8821f044 100644 --- a/plexus-compilers/plexus-compiler-javac/src/test/java/org/codehaus/plexus/compiler/javac/ErrorMessageParserTest.java +++ b/plexus-compilers/plexus-compiler-javac/src/test/java/org/codehaus/plexus/compiler/javac/ErrorMessageParserTest.java @@ -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 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()" diff --git a/plexus-compilers/plexus-compiler-javac/src/test/java/org/codehaus/plexus/compiler/javac/JavaxToolsCompilerTest.java b/plexus-compilers/plexus-compiler-javac/src/test/java/org/codehaus/plexus/compiler/javac/JavaxToolsCompilerTest.java index 5daa1717..2cc600de 100644 --- a/plexus-compilers/plexus-compiler-javac/src/test/java/org/codehaus/plexus/compiler/javac/JavaxToolsCompilerTest.java +++ b/plexus-compilers/plexus-compiler-javac/src/test/java/org/codehaus/plexus/compiler/javac/JavaxToolsCompilerTest.java @@ -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(); - } - } }