Skip to content

Commit

Permalink
Remove "error: " prefixes from javac error messages
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
kriegaex committed Dec 27, 2023
1 parent 026fd94 commit 0577d01
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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));

Expand All @@ -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));

Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 0577d01

Please sign in to comment.