You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by em...@apache.org on 2024/03/09 01:12:26 UTC
(groovy) 02/02: GROOVY-11270: reduce "foo.bar.Baz" to "f.b.Baz" in long error message
This is an automated email from the ASF dual-hosted git repository.
emilles pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/groovy.git
commit 1b72bb86ad67c8f7b0ec52c3c00298b304f0149f
Author: Eric Milles <er...@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 | 47 +++++++++++++++++++++
3 files changed, 85 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..aeaa4d420a 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(", "));
+ }
+
+ var plainForm = new StringBuilder();
+ var 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..e30ace30bb
--- /dev/null
+++ b/src/test/org/codehaus/groovy/runtime/FormatHelperTest.java
@@ -0,0 +1,47 @@
+/*
+ * 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));
+ }
+}