This is an automated email from the ASF dual-hosted git repository.

emilles pushed a commit to branch GROOVY_4_0_X
in repository https://gitbox.apache.org/repos/asf/groovy.git

commit e4c7e2db58b4cf135448a5bcdc0395dd8e699401
Author: Eric Milles <eric.mil...@thomsonreuters.com>
AuthorDate: Fri Mar 8 18:48:17 2024 -0600

    GROOVY-11270: reduce "foo.bar.Baz" to "f.b.Baz" in long error message
---
 .../java/groovy/lang/MissingMethodException.java   |  4 +-
 .../org/codehaus/groovy/runtime/FormatHelper.java  | 48 +++++++++++++++------
 .../codehaus/groovy/runtime/FormatHelperTest.java  | 50 ++++++++++++++++++++++
 3 files changed, 88 insertions(+), 14 deletions(-)

diff --git a/src/main/java/groovy/lang/MissingMethodException.java 
b/src/main/java/groovy/lang/MissingMethodException.java
index 43848f07e9..0400eff438 100644
--- a/src/main/java/groovy/lang/MissingMethodException.java
+++ b/src/main/java/groovy/lang/MissingMethodException.java
@@ -60,9 +60,9 @@ public class MissingMethodException extends 
GroovyRuntimeException {
                 + "."
                 + method
                 + "() is applicable for argument types: ("
-                + FormatHelper.toTypeString(arguments, 60)
+                + FormatHelper.toTypeString(arguments, 80)
                 + ") values: "
-                + FormatHelper.toArrayString(arguments, 60, true)
+                + FormatHelper.toArrayString(arguments, 80, true)
                 + (type != null ? 
MethodRankHelper.getMethodSuggestionString(method, type, arguments) : "");
     }
 
diff --git a/src/main/java/org/codehaus/groovy/runtime/FormatHelper.java 
b/src/main/java/org/codehaus/groovy/runtime/FormatHelper.java
index 0e0b93f597..54f3c4409b 100644
--- a/src/main/java/org/codehaus/groovy/runtime/FormatHelper.java
+++ b/src/main/java/org/codehaus/groovy/runtime/FormatHelper.java
@@ -43,17 +43,18 @@ import java.util.Map;
 import java.util.Set;
 
 import static java.lang.Math.max;
+import static java.util.Arrays.stream;
 
 /**
  * Formatting methods
  */
 public class FormatHelper {
 
+    private FormatHelper() {}
+
     private static final String SQ = "'";
     private static final String DQ = "\"";
 
-    private FormatHelper() {}
-
     private static final Object[] EMPTY_ARGS = {};
 
     // heuristic size to pre-allocate stringbuffers for collections of items
@@ -319,19 +320,42 @@ public class FormatHelper {
         if (arguments == null) {
             return "null";
         }
-        StringBuilder argBuf = new StringBuilder();
-        for (int i = 0; i < arguments.length; i++) {
-            if (maxSize != -1 && argBuf.length() > maxSize) {
-                argBuf.append("...");
-                break;
-            } else {
-                if (i > 0) {
-                    argBuf.append(", ");
+        if (arguments.length == 0) {
+            return "";
+        }
+        if (maxSize < 0) {
+            return stream(arguments)
+                    .map(arg -> arg != null ? typeName(arg) : "null")
+                    .collect(java.util.stream.Collectors.joining(", "));
+        }
+
+        StringBuilder plainForm = new StringBuilder();
+        StringBuilder shortForm = new StringBuilder();
+        for (int i = 0; i < arguments.length; i += 1) {
+            String type = arguments[i] != null ? typeName(arguments[i]) : 
"null";
+
+            if (plainForm.length() < maxSize) {
+                if (i > 0) plainForm.append(", ");
+                plainForm.append(type);
+            } else if (plainForm.charAt(plainForm.length() - 1) != '.') {
+                plainForm.append("...");
+            }
+
+            if (shortForm.length() < maxSize) {
+                if (i > 0) shortForm.append(", ");
+                String[] tokens = type.split("\\.");
+                for (int j = 0; j < tokens.length - 1; j += 1) {
+                    // GROOVY-11270: reduce "foo.bar.Baz" to "f.b.Baz"
+                    
shortForm.appendCodePoint(tokens[j].codePointAt(0)).append('.');
                 }
-                argBuf.append(arguments[i] != null ? typeName(arguments[i]) : 
"null");
+                shortForm.append(tokens[tokens.length - 1]);
+            } else {
+                shortForm.append("...");
+                break;
             }
         }
-        return argBuf.toString();
+
+        return (plainForm.length() <= maxSize ? plainForm : 
shortForm).toString();
     }
 
     /**
diff --git a/src/test/org/codehaus/groovy/runtime/FormatHelperTest.java 
b/src/test/org/codehaus/groovy/runtime/FormatHelperTest.java
new file mode 100644
index 0000000000..8cd63eb300
--- /dev/null
+++ b/src/test/org/codehaus/groovy/runtime/FormatHelperTest.java
@@ -0,0 +1,50 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ */
+package org.codehaus.groovy.runtime;
+
+import org.junit.Test;
+
+import static org.junit.Assert.assertEquals;
+
+public class FormatHelperTest {
+
+    @Test
+    public void testToTypeString() {
+        Object[] objects = null;
+        assertEquals("null", FormatHelper.toTypeString(objects, 42));
+
+        objects = new Object[0];
+        assertEquals("", FormatHelper.toTypeString(objects, 42));
+
+        objects = new Object[] {null};
+        assertEquals("null", FormatHelper.toTypeString(objects, 42));
+
+        objects = new Object[] {0};
+        assertEquals("Integer", FormatHelper.toTypeString(objects, 42));
+
+        objects = new Object[] {0, 1d};
+        assertEquals("Integer, Double", FormatHelper.toTypeString(objects, 
42));
+
+        objects = new Object[] {new DummyBean(), new DummyBean()}; // 
GROOVY-11270
+        assertEquals("o.c.g.r.DummyBean, o.c.g.r.DummyBean", 
FormatHelper.toTypeString(objects, 42));
+
+        objects = new Object[] {0f, new DummyBean()};
+        assertEquals("Float, org.codehaus.groovy.runtime.DummyBean", 
FormatHelper.toTypeString(objects, 44));
+    }
+}

Reply via email to