]> source.dussan.org Git - aspectj.git/commitdiff
Fix indentation of compilation results on the console
authorAlexander Kriegisch <Alexander@Kriegisch.name>
Sun, 22 Jan 2023 20:52:24 +0000 (21:52 +0100)
committerAlexander Kriegisch <Alexander@Kriegisch.name>
Sun, 22 Jan 2023 20:52:24 +0000 (21:52 +0100)
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 <Alexander@Kriegisch.name>
bridge/src/main/java/org/aspectj/bridge/MessageUtil.java
org.aspectj.ajdt.core/src/test/java/org/aspectj/tools/ajc/CompilationResult.java

index 7ef4e92abbad73bf548e95615939bb06b3b85de1..4f5e6067b83cc10f6a9a9e9650aaae0170f9552c 100644 (file)
@@ -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) {
index f2757bdaa5fb80608bec7f2e9dc844cc8501f14f..c0dc6a95b051148ef9c31df346e0acda631aa236 100644 (file)
@@ -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');
+               }
+       }
 }