From 0577d01cd068baf76a7e26ad671878ff5b3d9b42 Mon Sep 17 00:00:00 2001 From: Alexander Kriegisch Date: Wed, 27 Dec 2023 12:35:32 +0700 Subject: [PATCH] Remove "error: " prefixes from javac error messages Before, "warning: " prefixes were already removed. This change brings both warnings and errors in sync with regard to message processing. Some tests had to be slightly adjusted to reflect the now cleaner error messages. --- .../plexus/compiler/javac/JavacCompiler.java | 27 +++++++++++++------ .../javac/ErrorMessageParserTest.java | 8 +++--- 2 files changed, 22 insertions(+), 13 deletions(-) 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 bdd6d212..8c20da68 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 @@ -969,11 +969,14 @@ static CompilerMessage parseModernError(int exitCode, String error) { final StringBuilder msgBuffer = new StringBuilder(); String msg = tokens.nextToken(EOL).substring(2); - // Remove the 'warning: ' prefix - final String warnPrefix = getWarningPrefix(msg); - if (warnPrefix != null) { + // Remove "error: " and "warning: " prefixes + String prefix; + if ((prefix = getErrorPrefix(msg)) != null) { + messageKind = ERROR; + msg = msg.substring(prefix.length()); + } else if ((prefix = getWarningPrefix(msg)) != null) { messageKind = WARNING; - msg = msg.substring(warnPrefix.length()); + msg = msg.substring(prefix.length()); } msgBuffer.append(msg).append(EOL); @@ -1012,15 +1015,23 @@ static CompilerMessage parseModernError(int exitCode, String error) { } } - private static String getWarningPrefix(String msg) { - for (String warningPrefix : WARNING_PREFIXES) { - if (msg.startsWith(warningPrefix)) { - return warningPrefix; + private static String getMessagePrefix(String message, String[] prefixes) { + for (String prefix : prefixes) { + if (message.startsWith(prefix)) { + return prefix; } } return null; } + private static String getWarningPrefix(String message) { + return getMessagePrefix(message, WARNING_PREFIXES); + } + + private static String getErrorPrefix(String message) { + return getMessagePrefix(message, ERROR_PREFIXES); + } + /** * put args into a temp file to be referenced using the @ option in javac command line * 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 8ac5ddff..ad707f4e 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 @@ -753,8 +753,7 @@ public void testJava7Error() throws Exception { assertThat( message1.getMessage(), - is("error: cannot find symbol" + EOL + " symbol: class Properties" + EOL - + " location: class Error")); + is("cannot find symbol" + EOL + " symbol: class Properties" + EOL + " location: class Error")); assertThat(message1.getStartColumn(), is(16)); @@ -770,8 +769,7 @@ public void testJava7Error() throws Exception { assertThat( message2.getMessage(), - is("error: cannot find symbol" + EOL + " symbol: class Properties" + EOL - + " location: class Error")); + is("cannot find symbol" + EOL + " symbol: class Properties" + EOL + " location: class Error")); assertThat(message2.getStartColumn(), is(35)); @@ -1259,7 +1257,7 @@ public void testWarningFollowedByBadSourceFileError() throws Exception { private void validateBadSourceFile(CompilerMessage message) { assertThat("Is an Error", message.getKind(), is(CompilerMessage.Kind.ERROR)); assertThat("On Correct File", message.getFile(), is("/MTOOLCHAINS-19/src/main/java/ch/pecunifex/x/Cls1.java")); - assertThat("Message starts with access Error", message.getMessage(), startsWith("error: cannot access Cls2")); + assertThat("Message starts with access Error", message.getMessage(), startsWith("cannot access Cls2")); } private static void assertEquivalent(CompilerMessage expected, CompilerMessage actual) {