Repository: groovy
Updated Branches:
  refs/heads/GROOVY_2_5_X e2ec90380 -> 7f9d36f4f


Refine `LoggableTextifier` to support logging invocation positions

(cherry picked from commit 56d4a57)


Project: http://git-wip-us.apache.org/repos/asf/groovy/repo
Commit: http://git-wip-us.apache.org/repos/asf/groovy/commit/7f9d36f4
Tree: http://git-wip-us.apache.org/repos/asf/groovy/tree/7f9d36f4
Diff: http://git-wip-us.apache.org/repos/asf/groovy/diff/7f9d36f4

Branch: refs/heads/GROOVY_2_5_X
Commit: 7f9d36f4fa0db1cfa417f453b7d67b33f7d589f9
Parents: e2ec903
Author: sunlan <sun...@apache.org>
Authored: Wed Mar 7 13:58:01 2018 +0800
Committer: sunlan <sun...@apache.org>
Committed: Wed Mar 7 14:42:07 2018 +0800

----------------------------------------------------------------------
 .../classgen/asm/util/LoggableTextifier.java    | 33 ++++++++++++++++++--
 .../groovy/runtime/StringGroovyMethods.java     | 14 +++++++++
 .../groovy/runtime/StringGroovyMethodsTest.java | 24 ++++++++++++++
 3 files changed, 68 insertions(+), 3 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/groovy/blob/7f9d36f4/src/main/java/org/codehaus/groovy/classgen/asm/util/LoggableTextifier.java
----------------------------------------------------------------------
diff --git 
a/src/main/java/org/codehaus/groovy/classgen/asm/util/LoggableTextifier.java 
b/src/main/java/org/codehaus/groovy/classgen/asm/util/LoggableTextifier.java
index dce4417..fe1d39e 100644
--- a/src/main/java/org/codehaus/groovy/classgen/asm/util/LoggableTextifier.java
+++ b/src/main/java/org/codehaus/groovy/classgen/asm/util/LoggableTextifier.java
@@ -19,14 +19,15 @@
 package org.codehaus.groovy.classgen.asm.util;
 
 import org.codehaus.groovy.control.CompilerConfiguration;
+import org.codehaus.groovy.runtime.StringGroovyMethods;
 import org.objectweb.asm.Attribute;
 import org.objectweb.asm.Handle;
 import org.objectweb.asm.Label;
-import org.objectweb.asm.Opcodes;
 import org.objectweb.asm.TypePath;
 import org.objectweb.asm.util.Printer;
 import org.objectweb.asm.util.Textifier;
 
+import java.util.LinkedList;
 import java.util.List;
 
 /**
@@ -35,7 +36,8 @@ import java.util.List;
  * @since 2.5.0
  */
 public class LoggableTextifier extends Textifier {
-//    private static final Logger LOGGER = 
Logger.getLogger(LoggableTextifier.class.getName());
+    private static final String GROOVY = ".groovy.";
+    private static final String LOGGABLE_TEXTIFIER = ".LoggableTextifier";
     private int loggedLineCnt = 0;
 
     public LoggableTextifier() {
@@ -50,6 +52,7 @@ public class LoggableTextifier extends Textifier {
     protected void log() {
         int textSize = text.size();
 
+        List<Object> bcList = new LinkedList<>();
         for (int i = loggedLineCnt; i < textSize; i++) {
             Object bc = text.get(i);
 
@@ -57,12 +60,36 @@ public class LoggableTextifier extends Textifier {
                 continue;
             }
 
-            System.out.print(bc);
+            bcList.add(bc);
+        }
+
+        if (bcList.size() > 0) {
+            String invocationPositionInfo = getInvocationPositionInfo();
+            if (!StringGroovyMethods.isBlank(invocationPositionInfo)) {
+                
System.out.println("\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t//
 " + invocationPositionInfo);
+            }
+
+            for (Object bc : bcList) {
+                System.out.print(bc);
+            }
         }
 
         loggedLineCnt = textSize;
     }
 
+    private String getInvocationPositionInfo() {
+        StackTraceElement[] stackTraceElements = new 
Throwable().getStackTrace();
+
+        for (StackTraceElement stackTraceElement : stackTraceElements) {
+            String className = stackTraceElement.getClassName();
+            if (className.contains(GROOVY) && 
!className.endsWith(LOGGABLE_TEXTIFIER)) {
+                return String.format("%s#%s:%s", className, 
stackTraceElement.getMethodName(), stackTraceElement.getLineNumber());
+            }
+        }
+
+        return "";
+    }
+
 
     @Override
     public void visit(int version, int access, String name, String signature, 
String superName, String[] interfaces) {

http://git-wip-us.apache.org/repos/asf/groovy/blob/7f9d36f4/src/main/java/org/codehaus/groovy/runtime/StringGroovyMethods.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/codehaus/groovy/runtime/StringGroovyMethods.java 
b/src/main/java/org/codehaus/groovy/runtime/StringGroovyMethods.java
index 8d87e40..0ae574c 100644
--- a/src/main/java/org/codehaus/groovy/runtime/StringGroovyMethods.java
+++ b/src/main/java/org/codehaus/groovy/runtime/StringGroovyMethods.java
@@ -3835,4 +3835,18 @@ public class StringGroovyMethods extends 
DefaultGroovyMethodsSupport {
 
         return false;
     }
+
+    /**
+     * Tests if this CharSequence is blank
+     * @return {@code true} if this CharSequence is blank
+     *
+     * @since   2.5.0
+     */
+    public static boolean isBlank(CharSequence self) {
+        if (null == self) {
+            return true;
+        }
+
+        return self.toString().matches("\\s*");
+    }
 }

http://git-wip-us.apache.org/repos/asf/groovy/blob/7f9d36f4/src/test/org/codehaus/groovy/runtime/StringGroovyMethodsTest.java
----------------------------------------------------------------------
diff --git a/src/test/org/codehaus/groovy/runtime/StringGroovyMethodsTest.java 
b/src/test/org/codehaus/groovy/runtime/StringGroovyMethodsTest.java
index 6bfbac0..ea73e97 100644
--- a/src/test/org/codehaus/groovy/runtime/StringGroovyMethodsTest.java
+++ b/src/test/org/codehaus/groovy/runtime/StringGroovyMethodsTest.java
@@ -101,4 +101,28 @@ public class StringGroovyMethodsTest extends 
GroovyTestCase {
         assertTrue(StringGroovyMethods.endsWithAny("abcd", "cd", "ef"));
         assertFalse(StringGroovyMethods.endsWithAny("abcd", "ef", "gh"));
     }
+
+    public void testIsBlank() {
+        assertTrue(StringGroovyMethods.isBlank(""));
+        assertTrue(StringGroovyMethods.isBlank(" "));
+        assertTrue(StringGroovyMethods.isBlank("  "));
+        assertTrue(StringGroovyMethods.isBlank("\t"));
+        assertTrue(StringGroovyMethods.isBlank("\t\t"));
+        assertTrue(StringGroovyMethods.isBlank(" \t"));
+        assertTrue(StringGroovyMethods.isBlank("\t "));
+        assertTrue(StringGroovyMethods.isBlank(" \n "));
+        assertTrue(StringGroovyMethods.isBlank("\n"));
+        assertTrue(StringGroovyMethods.isBlank("\n\n"));
+        assertTrue(StringGroovyMethods.isBlank(" \n"));
+        assertTrue(StringGroovyMethods.isBlank("\n "));
+        assertTrue(StringGroovyMethods.isBlank(" \n "));
+        assertTrue(StringGroovyMethods.isBlank(" \n \t "));
+        assertFalse(StringGroovyMethods.isBlank("abc"));
+        assertFalse(StringGroovyMethods.isBlank("abc "));
+        assertFalse(StringGroovyMethods.isBlank(" abc"));
+        assertFalse(StringGroovyMethods.isBlank(" abc "));
+        assertFalse(StringGroovyMethods.isBlank("\tabc"));
+        assertFalse(StringGroovyMethods.isBlank("abc\t"));
+        assertFalse(StringGroovyMethods.isBlank("\tabc\t"));
+    }
 }

Reply via email to