Skip to content

Commit

Permalink
Fix indentation of compilation results on the console
Browse files Browse the repository at this point in the history
Whenever warnings or errors were printed via CompilationResult.toString,
indirectly also using MessageUtil.renderMessage(IMessage, boolean),
messages containing context info such as code snippets with carets
marking erroneous tokens - see also the previous commit - prefixes like
"[warning 1] warning at " were printed right in front of the code
snippets. I.e., the carets marking erroneous tokens in the second line
were not indented like the first line with the code snippet, leading to
(simplified) output like:

  [warning 1] warning at after() : execution(FooBar Blah.*()) {
                      ^^^^^^
  xxx FooBar [Xlint:invalidAbsoluteTypeName]

This was fixed to now correctly indent lines 2 to n according to line 1,
yielding the correct output:

  [warning 1] warning at
              after() : execution(FooBar Blah.*()) {
                                  ^^^^^^
              xxx FooBar [Xlint:invalidAbsoluteTypeName]

Especially with longer, more complex context lines, this helps to
identify the erroneous section. BTW, for one-line messages, everything
of course looks like before.

Signed-off-by: Alexander Kriegisch <[email protected]>
  • Loading branch information
kriegaex committed Jan 22, 2023
1 parent ac2b81c commit 8271153
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 55 deletions.
10 changes: 7 additions & 3 deletions bridge/src/main/java/org/aspectj/bridge/MessageUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -798,10 +798,14 @@ public static String renderMessage(IMessage message, boolean elide) {
return "((IMessage) null)";
}

String result = message.getKind().toString();
ISourceLocation loc = message.getSourceLocation();
String locString = (null == loc ? "" : " at " + loc);

String result = message.getKind() + locString + " " + message.getMessage();
if (loc != null) {
String context = loc.getContext();
result += context == null || context.trim().isEmpty() ? " at " : " at\n";
result += loc;
}
result += " " + message.getMessage();

Throwable thrown = message.getThrown();
if (thrown != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,69 +155,53 @@ protected CompilationResult(
public String toString() {
StringBuilder buff = new StringBuilder();
buff.append("AspectJ Compilation Result:\n");

int totalMessages = infoMessages.size() + warningMessages.size() + errorMessages.size() + failMessages.size() + weaveMessages.size();
buff.append(totalMessages);
buff.append(" messages");
buff.append(totalMessages).append(" messages");
if (totalMessages > 0) {
buff.append(" (");
buff.append(infoMessages.size());
buff.append(" info, ");
buff.append(warningMessages.size());
buff.append(" warning, ");
buff.append(errorMessages.size());
buff.append(" error, ");
buff.append(failMessages.size());
buff.append(" fail, )");
buff.append(weaveMessages.size());
buff.append(" weaveInfo");
buff
.append(" (")
.append(infoMessages.size()).append(" info, ")
.append(warningMessages.size()).append(" warning, ")
.append(errorMessages.size()).append(" error, ")
.append(failMessages.size()).append(" fail, ")
.append(weaveMessages.size()).append(" weaveInfo")
.append(")");
}
buff.append("\n");

int msgNo = 1;
for (IMessage failMessage : failMessages) {
buff.append("[fail ");
buff.append(msgNo++);
buff.append("] ");
buff.append(failMessage.toString());
buff.append("\n");
}
for (IMessage failMessage : failMessages)
indentWithPrefix(buff, "[fail " + msgNo++ + "] ", failMessage.toString());
msgNo = 1;
for (IMessage errorMessage : errorMessages) {
buff.append("[error ");
buff.append(msgNo++);
buff.append("] ");
buff.append(errorMessage.toString());
buff.append("\n");
}
for (IMessage errorMessage : errorMessages)
indentWithPrefix(buff, "[error " + msgNo++ + "] ", errorMessage.toString());
msgNo = 1;
for (IMessage warningMessage : warningMessages) {
buff.append("[warning ");
buff.append(msgNo++);
buff.append("] ");
buff.append(warningMessage.toString());
buff.append("\n");
}
for (IMessage warningMessage : warningMessages)
indentWithPrefix(buff, "[warning " + msgNo++ + "] ", warningMessage.toString());
msgNo = 1;
for (IMessage infoMessage : infoMessages) {
buff.append("[info ");
buff.append(msgNo++);
buff.append("] ");
buff.append(infoMessage.toString());
buff.append("\n");
}
for (IMessage infoMessage : infoMessages)
indentWithPrefix(buff, "[info " + msgNo++ + "] ", infoMessage.toString());
msgNo = 1;
for (IMessage weaveMessage : weaveMessages) {
buff.append("[weaveInfo ");
buff.append(msgNo++);
buff.append("] ");
buff.append(weaveMessage.toString());
buff.append("\n");
}
for (IMessage weaveMessage : weaveMessages)
indentWithPrefix(buff, "[weaveInfo " + msgNo++ + "] ", weaveMessage.toString());

buff.append("\nCommand: 'ajc");
for (String arg : args) {
buff.append(' ');
buff.append(arg);
}
for (String arg : args)
buff.append(' ').append(arg);
buff.append("'\n");

return buff.toString();
}

private void indentWithPrefix(StringBuilder buffer, String prefix, String message) {
String[] lines = message.split("\n|\r\n|\r");
boolean firstLine = true;
for (String line : lines) {
buffer.append(prefix);
if (firstLine)
prefix = prefix.replaceAll(".", " ");
buffer.append(line).append('\n');
}
}
}

0 comments on commit 8271153

Please sign in to comment.