From 827115335ff1269df28085e215b081e24596ff1d Mon Sep 17 00:00:00 2001 From: Alexander Kriegisch Date: Sun, 22 Jan 2023 21:52:24 +0100 Subject: [PATCH] Fix indentation of compilation results on the console 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 --- .../java/org/aspectj/bridge/MessageUtil.java | 10 ++- .../aspectj/tools/ajc/CompilationResult.java | 88 ++++++++----------- 2 files changed, 43 insertions(+), 55 deletions(-) diff --git a/bridge/src/main/java/org/aspectj/bridge/MessageUtil.java b/bridge/src/main/java/org/aspectj/bridge/MessageUtil.java index 7ef4e92ab..4f5e6067b 100644 --- a/bridge/src/main/java/org/aspectj/bridge/MessageUtil.java +++ b/bridge/src/main/java/org/aspectj/bridge/MessageUtil.java @@ -798,10 +798,14 @@ public class MessageUtil { 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) { diff --git a/org.aspectj.ajdt.core/src/test/java/org/aspectj/tools/ajc/CompilationResult.java b/org.aspectj.ajdt.core/src/test/java/org/aspectj/tools/ajc/CompilationResult.java index f2757bdaa..c0dc6a95b 100644 --- a/org.aspectj.ajdt.core/src/test/java/org/aspectj/tools/ajc/CompilationResult.java +++ b/org.aspectj.ajdt.core/src/test/java/org/aspectj/tools/ajc/CompilationResult.java @@ -155,69 +155,53 @@ public class 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'); + } + } } -- 2.39.5