You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by pa...@apache.org on 2016/07/29 00:02:27 UTC
[1/7] groovy git commit: InvokerHelper: Refactor: Avoid unnecessary
array transformation for Object[]
Repository: groovy
Updated Branches:
refs/heads/master c9bd001cc -> be3f41431
InvokerHelper: Refactor: Avoid unnecessary array transformation for Object[]
Project: http://git-wip-us.apache.org/repos/asf/groovy/repo
Commit: http://git-wip-us.apache.org/repos/asf/groovy/commit/46e4d326
Tree: http://git-wip-us.apache.org/repos/asf/groovy/tree/46e4d326
Diff: http://git-wip-us.apache.org/repos/asf/groovy/diff/46e4d326
Branch: refs/heads/master
Commit: 46e4d326b54f7f0edd656d939900f2a7ae56678d
Parents: c9bd001
Author: Thibault Kruse <th...@gmx.de>
Authored: Wed Sep 2 00:17:40 2015 +0200
Committer: paulk <pa...@asert.com.au>
Committed: Fri Jul 29 08:36:38 2016 +1000
----------------------------------------------------------------------
src/main/org/codehaus/groovy/runtime/InvokerHelper.java | 4 ++++
1 file changed, 4 insertions(+)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/groovy/blob/46e4d326/src/main/org/codehaus/groovy/runtime/InvokerHelper.java
----------------------------------------------------------------------
diff --git a/src/main/org/codehaus/groovy/runtime/InvokerHelper.java b/src/main/org/codehaus/groovy/runtime/InvokerHelper.java
index 37cdf29..84924be 100644
--- a/src/main/org/codehaus/groovy/runtime/InvokerHelper.java
+++ b/src/main/org/codehaus/groovy/runtime/InvokerHelper.java
@@ -583,9 +583,13 @@ public class InvokerHelper {
return (String) nullObject.getMetaClass().invokeMethod(nullObject, "toString", EMPTY_ARGS);
}
if (arguments.getClass().isArray()) {
+ if (arguments instanceof Object[]) {
+ return toArrayString((Object[]) arguments, maxSize, false);
+ }
if (arguments instanceof char[]) {
return new String((char[]) arguments);
}
+ // other primitives
return formatCollection(DefaultTypeTransformation.arrayAsCollection(arguments), verbose, maxSize);
}
if (arguments instanceof Range) {
[3/7] groovy git commit: InvokerHelper: Refactor: Implement Array
formatting like Collection formatting
Posted by pa...@apache.org.
InvokerHelper: Refactor: Implement Array formatting like Collection formatting
Project: http://git-wip-us.apache.org/repos/asf/groovy/repo
Commit: http://git-wip-us.apache.org/repos/asf/groovy/commit/ff4c62ef
Tree: http://git-wip-us.apache.org/repos/asf/groovy/tree/ff4c62ef
Diff: http://git-wip-us.apache.org/repos/asf/groovy/diff/ff4c62ef
Branch: refs/heads/master
Commit: ff4c62ef5e843a235692d4ca262885e38996d289
Parents: 42427df
Author: Thibault Kruse <th...@gmx.de>
Authored: Fri Aug 21 16:11:52 2015 +0200
Committer: paulk <pa...@asert.com.au>
Committed: Fri Jul 29 08:36:41 2016 +1000
----------------------------------------------------------------------
.../codehaus/groovy/runtime/InvokerHelper.java | 43 ++++++++++++++++----
1 file changed, 36 insertions(+), 7 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/groovy/blob/ff4c62ef/src/main/org/codehaus/groovy/runtime/InvokerHelper.java
----------------------------------------------------------------------
diff --git a/src/main/org/codehaus/groovy/runtime/InvokerHelper.java b/src/main/org/codehaus/groovy/runtime/InvokerHelper.java
index b27f0fb..632b79b 100644
--- a/src/main/org/codehaus/groovy/runtime/InvokerHelper.java
+++ b/src/main/org/codehaus/groovy/runtime/InvokerHelper.java
@@ -579,7 +579,7 @@ public class InvokerHelper {
}
if (arguments.getClass().isArray()) {
if (arguments instanceof Object[]) {
- return toArrayString((Object[]) arguments, maxSize, false);
+ return toArrayString((Object[]) arguments, verbose, maxSize, false);
}
if (arguments instanceof char[]) {
return new String((char[]) arguments);
@@ -796,16 +796,45 @@ public class InvokerHelper {
* @return the string representation of the array
*/
public static String toArrayString(Object[] arguments) {
- if (arguments == null) {
+ return toArrayString(arguments, false, -1, false);
+ }
+
+ private static String toArrayString(Object[] collection, boolean verbose, int maxSize, boolean safe) {
+ if (collection == null) {
return "null";
}
- StringBuilder argBuf = new StringBuilder(arguments.length);
+ boolean first = true;
+ StringBuilder argBuf = new StringBuilder(collection.length);
argBuf.append('[');
- for (int i = 0; i < arguments.length; i++) {
- if (i > 0) {
+
+ for (Object item : collection) {
+ if (first) {
+ first = false;
+ } else {
argBuf.append(", ");
}
- argBuf.append(format(arguments[i], false));
+ if (maxSize != -1 && argBuf.length() > maxSize) {
+ argBuf.append("...");
+ break;
+ }
+ if (item == collection) {
+ argBuf.append("(this Collection)");
+ } else {
+ String str;
+ try {
+ str = format(item, verbose, sizeLeft(maxSize, argBuf));
+ } catch (Exception ex) {
+ if (!safe) throw new GroovyRuntimeException(ex);
+ String hash;
+ try {
+ hash = Integer.toHexString(item.hashCode());
+ } catch (Exception ignored) {
+ hash = "????";
+ }
+ str = "<" + item.getClass().getName() + "@" + hash + ">";
+ }
+ argBuf.append(str);
+ }
}
argBuf.append(']');
return argBuf.toString();
@@ -821,7 +850,7 @@ public class InvokerHelper {
* @return the string representation of the array
*/
public static String toArrayString(Object[] arguments, int maxSize, boolean safe) {
- return toListString(DefaultTypeTransformation.asCollection(arguments), maxSize, safe);
+ return toArrayString(arguments, false, maxSize, safe);
}
public static List createRange(Object from, Object to, boolean inclusive) {
[4/7] groovy git commit: GROOVY-7563: InvokerHelper: Safe format
recursively and consistently (not just for lists)
Posted by pa...@apache.org.
GROOVY-7563: InvokerHelper: Safe format recursively and consistently (not just for lists)
Project: http://git-wip-us.apache.org/repos/asf/groovy/repo
Commit: http://git-wip-us.apache.org/repos/asf/groovy/commit/e28f0014
Tree: http://git-wip-us.apache.org/repos/asf/groovy/tree/e28f0014
Diff: http://git-wip-us.apache.org/repos/asf/groovy/diff/e28f0014
Branch: refs/heads/master
Commit: e28f0014d17458a9f964c74c400a390100497966
Parents: ff4c62e
Author: Thibault Kruse <th...@gmx.de>
Authored: Wed Sep 2 19:38:55 2015 +0200
Committer: paulk <pa...@asert.com.au>
Committed: Fri Jul 29 08:36:43 2016 +1000
----------------------------------------------------------------------
.../codehaus/groovy/runtime/InvokerHelper.java | 75 +++++++++-----------
.../runtime/InvokerHelperFormattingTest.groovy | 20 ++++--
2 files changed, 50 insertions(+), 45 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/groovy/blob/e28f0014/src/main/org/codehaus/groovy/runtime/InvokerHelper.java
----------------------------------------------------------------------
diff --git a/src/main/org/codehaus/groovy/runtime/InvokerHelper.java b/src/main/org/codehaus/groovy/runtime/InvokerHelper.java
index 632b79b..53055f0 100644
--- a/src/main/org/codehaus/groovy/runtime/InvokerHelper.java
+++ b/src/main/org/codehaus/groovy/runtime/InvokerHelper.java
@@ -124,7 +124,7 @@ public class InvokerHelper {
}
public static String toString(Object arguments) {
- return format(arguments, false);
+ return format(arguments, false, -1, false);
}
public static String inspect(Object self) {
@@ -573,19 +573,23 @@ public class InvokerHelper {
}
public static String format(Object arguments, boolean verbose, int maxSize) {
+ return format(arguments, verbose, maxSize, false);
+ }
+
+ public static String format(Object arguments, boolean verbose, int maxSize, boolean safe) {
if (arguments == null) {
final NullObject nullObject = NullObject.getNullObject();
return (String) nullObject.getMetaClass().invokeMethod(nullObject, "toString", EMPTY_ARGS);
}
if (arguments.getClass().isArray()) {
if (arguments instanceof Object[]) {
- return toArrayString((Object[]) arguments, verbose, maxSize, false);
+ return toArrayString((Object[]) arguments, verbose, maxSize, safe);
}
if (arguments instanceof char[]) {
return new String((char[]) arguments);
}
// other primitives
- return formatCollection(DefaultTypeTransformation.arrayAsCollection(arguments), verbose, maxSize);
+ return formatCollection(DefaultTypeTransformation.arrayAsCollection(arguments), verbose, maxSize, safe);
}
if (arguments instanceof Range) {
Range range = (Range) arguments;
@@ -596,10 +600,10 @@ public class InvokerHelper {
}
}
if (arguments instanceof Collection) {
- return formatCollection((Collection) arguments, verbose, maxSize);
+ return formatCollection((Collection) arguments, verbose, maxSize, safe);
}
if (arguments instanceof Map) {
- return formatMap((Map) arguments, verbose, maxSize);
+ return formatMap((Map) arguments, verbose, maxSize, safe);
}
if (arguments instanceof Element) {
try {
@@ -626,7 +630,15 @@ public class InvokerHelper {
}
// TODO: For GROOVY-2599 do we need something like below but it breaks other things
// return (String) invokeMethod(arguments, "toString", EMPTY_ARGS);
- return arguments.toString();
+ try {
+ return arguments.toString();
+ } catch (RuntimeException ex) {
+ if (!safe) throw ex;
+ return handleFormattingException(arguments, ex);
+ } catch (Exception ex) {
+ if (!safe) throw new GroovyRuntimeException(ex);
+ return handleFormattingException(arguments, ex);
+ }
}
public static String escapeBackslashes(String orig) {
@@ -639,7 +651,18 @@ public class InvokerHelper {
.replaceAll("\\f", "\\\\f"); // form feed
}
- private static String formatMap(Map map, boolean verbose, int maxSize) {
+ private static String handleFormattingException(Object item, Exception ex) {
+
+ String hash;
+ try {
+ hash = Integer.toHexString(item.hashCode());
+ } catch (Exception ignored) {
+ hash = "????";
+ }
+ return "<" + item.getClass().getName() + "@" + hash + ">";
+ }
+
+ private static String formatMap(Map map, boolean verbose, int maxSize, boolean safe) {
if (map.isEmpty()) {
return "[:]";
}
@@ -662,7 +685,7 @@ public class InvokerHelper {
if (entry.getValue() == map) {
buffer.append("(this Map)");
} else {
- buffer.append(format(entry.getValue(), verbose, sizeLeft(maxSize, buffer)));
+ buffer.append(format(entry.getValue(), verbose, sizeLeft(maxSize, buffer), safe));
}
}
buffer.append(']');
@@ -673,10 +696,6 @@ public class InvokerHelper {
return maxSize == -1 ? maxSize : Math.max(0, maxSize - buffer.length());
}
- private static String formatCollection(Collection collection, boolean verbose, int maxSize) {
- return formatCollection(collection, verbose, maxSize, false);
- }
-
private static String formatCollection(Collection collection, boolean verbose, int maxSize, boolean safe) {
StringBuilder buffer = new StringBuilder(ITEM_ALLOCATE_SIZE * collection.size());
buffer.append('[');
@@ -694,20 +713,7 @@ public class InvokerHelper {
if (item == collection) {
buffer.append("(this Collection)");
} else {
- String str;
- try {
- str = format(item, verbose, sizeLeft(maxSize, buffer));
- } catch (Exception ex) {
- if (!safe) throw new GroovyRuntimeException(ex);
- String hash;
- try {
- hash = Integer.toHexString(item.hashCode());
- } catch (Exception ignored) {
- hash = "????";
- }
- str = "<" + item.getClass().getName() + "@" + hash + ">";
- }
- buffer.append(str);
+ buffer.append(format(item, verbose, sizeLeft(maxSize, buffer), safe));
}
}
buffer.append(']');
@@ -752,7 +758,7 @@ public class InvokerHelper {
* @return the string representation of the map
*/
public static String toMapString(Map arg, int maxSize) {
- return formatMap(arg, false, maxSize);
+ return formatMap(arg, false, maxSize, false);
}
/**
@@ -820,20 +826,7 @@ public class InvokerHelper {
if (item == collection) {
argBuf.append("(this Collection)");
} else {
- String str;
- try {
- str = format(item, verbose, sizeLeft(maxSize, argBuf));
- } catch (Exception ex) {
- if (!safe) throw new GroovyRuntimeException(ex);
- String hash;
- try {
- hash = Integer.toHexString(item.hashCode());
- } catch (Exception ignored) {
- hash = "????";
- }
- str = "<" + item.getClass().getName() + "@" + hash + ">";
- }
- argBuf.append(str);
+ argBuf.append(format(item, verbose, sizeLeft(maxSize, argBuf), safe));
}
}
argBuf.append(']');
http://git-wip-us.apache.org/repos/asf/groovy/blob/e28f0014/src/test/org/codehaus/groovy/runtime/InvokerHelperFormattingTest.groovy
----------------------------------------------------------------------
diff --git a/src/test/org/codehaus/groovy/runtime/InvokerHelperFormattingTest.groovy b/src/test/org/codehaus/groovy/runtime/InvokerHelperFormattingTest.groovy
index 7c70159..bed83fa 100644
--- a/src/test/org/codehaus/groovy/runtime/InvokerHelperFormattingTest.groovy
+++ b/src/test/org/codehaus/groovy/runtime/InvokerHelperFormattingTest.groovy
@@ -72,6 +72,7 @@ class InvokerHelperFormattingTest extends GroovyTestCase {
shouldFail(UnsupportedOperationException) {
InvokerHelper.format(eObject, false)
}
+ assert InvokerHelper.format(new ExceptionOnToString(), false, -1, true) =~ (ExceptionOnToString.MATCHER)
}
public void testFormatRanges() {
@@ -79,6 +80,7 @@ class InvokerHelperFormattingTest extends GroovyTestCase {
assert "a'b..a'd" == InvokerHelper.format('a\'b'..'a\'d', false)
assert "[1..4]" == InvokerHelper.format([1..4], false)
assert "[a'b..a'd]" == InvokerHelper.format(['a\'b'..'a\'d'], false)
+
// verbose
assert '1..4' == InvokerHelper.format(1..4, true)
assert "'a\\'b'..'a\\'d'" == InvokerHelper.format('a\'b'..'a\'d', true)
@@ -104,16 +106,16 @@ class InvokerHelperFormattingTest extends GroovyTestCase {
Object eObject = new ExceptionOnToString()
assert InvokerHelper.toListString([eObject], -1, true) =~ "\\[${ExceptionOnToString.MATCHER}\\]"
List list = [[z: eObject]]
- assert InvokerHelper.toListString(list, -1, true) == '[<java.util.LinkedHashMap@' + Integer.toHexString(list[0].hashCode()) +'>]'
+ assert InvokerHelper.toListString(list, -1, true) =~ "\\[\\[z:${ExceptionOnToString.MATCHER}\\]\\]"
// even when throwing object is deeply nested, exception handling only happens in Collection
list = [[x: [y: [z: eObject, a: 2, b: 4]]]]
- assert InvokerHelper.toListString(list, -1, true) == '[<java.util.LinkedHashMap@' + Integer.toHexString(list[0].hashCode()) + '>]'
+ assert InvokerHelper.toListString(list, -1, true) =~ "\\[\\[x:\\[y:\\[z:${ExceptionOnToString.MATCHER}, a:2, b:4\\]\\]\\]\\]"
list = [[eObject, 1, 2]]
// safe argument is not passed on recursively
- assert InvokerHelper.toListString(list, -1, true) == '[<java.util.ArrayList@' + Integer.toHexString(list[0].hashCode()) + '>]'
+ assert InvokerHelper.toListString(list, -1, true) =~ "\\[\\[${ExceptionOnToString.MATCHER}, 1, 2\\]\\]"
list = [[[[[eObject, 1, 2]]]]]
- assert InvokerHelper.toListString(list, -1, true) == '[<java.util.ArrayList@' + Integer.toHexString(list[0].hashCode()) + '>]'
+ assert InvokerHelper.toListString(list, -1, true) =~ "\\[\\[\\[\\[\\[${ExceptionOnToString.MATCHER}, 1, 2\\]\\]\\]\\]\\]"
shouldFail(UnsupportedOperationException) {
InvokerHelper.toListString([eObject], -1, false)
@@ -147,6 +149,8 @@ class InvokerHelperFormattingTest extends GroovyTestCase {
shouldFail(UnsupportedOperationException) {
InvokerHelper.format([eObject] as ExceptionOnToString[], false)
}
+
+ assert InvokerHelper.format([new ExceptionOnToString()] as Object[], false, -1, true) =~ "\\[${ExceptionOnToString.MATCHER}\\]"
}
public void testToStringMaps() {
@@ -157,12 +161,18 @@ class InvokerHelperFormattingTest extends GroovyTestCase {
public void testFormatMaps() {
assert '[:]' == InvokerHelper.format([:], false)
assert "[a'b:1, 2:b'c]" == InvokerHelper.format(['a\'b':1, 2:'b\'c'], false)
+ assert "['a\\'b':1, 2:'b\\'c']" == InvokerHelper.format(['a\'b':1, 2:'b\'c'], true, -1, true)
Object eObject = new ExceptionOnToString()
shouldFail(UnsupportedOperationException) {
InvokerHelper.format([foo: eObject], false)
}
+ assert InvokerHelper.format([foo: eObject], false, -1, true) =~ "\\[foo:${ExceptionOnToString.MATCHER}\\]"
+ assert InvokerHelper.format([foo: eObject], true, -1, true) =~ "\\['foo':${ExceptionOnToString.MATCHER}\\]"
+ Map m = [:]
+ m.put(eObject, eObject)
+ assert InvokerHelper.format(m, false, -1, true) =~ "\\[${ExceptionOnToString.MATCHER}:${ExceptionOnToString.MATCHER}\\]"
}
public void testToMapString() {
@@ -188,6 +198,8 @@ class InvokerHelperFormattingTest extends GroovyTestCase {
list.add(['h', 'i'] as String[])
list.add([10, 11] as int[])
assert "[key:[[a'b:c'd], [e, f, g], 5..9, fog..fop, [h, i], [10, 11]]]" == InvokerHelper.toString([key: list])
+
+ assert "['key':[['a\\'b':'c\\'d'], ['e', 'f', 'g'], 5..9, 'fog'..'fop', ['h', 'i'], [10, 11]]]" == InvokerHelper.format([key:list], true, -1, false)
}
public void testToStringSelfContained() {
[5/7] groovy git commit: GROOVY-7563: InvokerHelper: Map formatting
safe and self-detecting also for keys
Posted by pa...@apache.org.
GROOVY-7563: InvokerHelper: Map formatting safe and self-detecting also for keys
Project: http://git-wip-us.apache.org/repos/asf/groovy/repo
Commit: http://git-wip-us.apache.org/repos/asf/groovy/commit/56904d6e
Tree: http://git-wip-us.apache.org/repos/asf/groovy/tree/56904d6e
Diff: http://git-wip-us.apache.org/repos/asf/groovy/diff/56904d6e
Branch: refs/heads/master
Commit: 56904d6e2d5022d6ae4e70374971a663b06b22a4
Parents: e28f001
Author: Thibault Kruse <th...@gmx.de>
Authored: Fri Aug 21 16:23:37 2015 +0200
Committer: paulk <pa...@asert.com.au>
Committed: Fri Jul 29 08:36:45 2016 +1000
----------------------------------------------------------------------
src/main/org/codehaus/groovy/runtime/InvokerHelper.java | 6 +++++-
.../groovy/runtime/InvokerHelperFormattingTest.groovy | 10 ++--------
2 files changed, 7 insertions(+), 9 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/groovy/blob/56904d6e/src/main/org/codehaus/groovy/runtime/InvokerHelper.java
----------------------------------------------------------------------
diff --git a/src/main/org/codehaus/groovy/runtime/InvokerHelper.java b/src/main/org/codehaus/groovy/runtime/InvokerHelper.java
index 53055f0..17f7ca9 100644
--- a/src/main/org/codehaus/groovy/runtime/InvokerHelper.java
+++ b/src/main/org/codehaus/groovy/runtime/InvokerHelper.java
@@ -680,7 +680,11 @@ public class InvokerHelper {
break;
}
Map.Entry entry = (Map.Entry) o;
- buffer.append(format(entry.getKey(), verbose));
+ if (entry.getKey() == map) {
+ buffer.append("(this Map)");
+ } else {
+ buffer.append(format(entry.getKey(), verbose, sizeLeft(maxSize, buffer), safe));
+ }
buffer.append(":");
if (entry.getValue() == map) {
buffer.append("(this Map)");
http://git-wip-us.apache.org/repos/asf/groovy/blob/56904d6e/src/test/org/codehaus/groovy/runtime/InvokerHelperFormattingTest.groovy
----------------------------------------------------------------------
diff --git a/src/test/org/codehaus/groovy/runtime/InvokerHelperFormattingTest.groovy b/src/test/org/codehaus/groovy/runtime/InvokerHelperFormattingTest.groovy
index bed83fa..4e55623 100644
--- a/src/test/org/codehaus/groovy/runtime/InvokerHelperFormattingTest.groovy
+++ b/src/test/org/codehaus/groovy/runtime/InvokerHelperFormattingTest.groovy
@@ -208,14 +208,8 @@ class InvokerHelperFormattingTest extends GroovyTestCase {
assert '[(this Collection)]' == InvokerHelper.toString(l)
Map m = [:]
- m.put('x', m)
- assert '[x:(this Map)]' == InvokerHelper.toString(m)
-
- Map m2 = [:]
- m2.put(m2, m2)
- shouldFail(StackOverflowError) {
- InvokerHelper.toString(m2)
- }
+ m.put(m, m)
+ assert '[(this Map):(this Map)]' == InvokerHelper.toString(m)
}
}
[7/7] groovy git commit: GROOVY-7563: Make InvokerHelper output
self-consistent (minor refactor - closes #112)
Posted by pa...@apache.org.
GROOVY-7563: Make InvokerHelper output self-consistent (minor refactor - closes #112)
Project: http://git-wip-us.apache.org/repos/asf/groovy/repo
Commit: http://git-wip-us.apache.org/repos/asf/groovy/commit/be3f4143
Tree: http://git-wip-us.apache.org/repos/asf/groovy/tree/be3f4143
Diff: http://git-wip-us.apache.org/repos/asf/groovy/diff/be3f4143
Branch: refs/heads/master
Commit: be3f41431884aea75a1399f02f65bb1344c30a21
Parents: 9be00f2
Author: paulk <pa...@asert.com.au>
Authored: Fri Jul 29 10:01:26 2016 +1000
Committer: paulk <pa...@asert.com.au>
Committed: Fri Jul 29 10:01:26 2016 +1000
----------------------------------------------------------------------
.../codehaus/groovy/runtime/InvokerHelper.java | 68 +++++++++++---------
.../runtime/InvokerHelperFormattingTest.groovy | 28 ++++----
2 files changed, 52 insertions(+), 44 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/groovy/blob/be3f4143/src/main/org/codehaus/groovy/runtime/InvokerHelper.java
----------------------------------------------------------------------
diff --git a/src/main/org/codehaus/groovy/runtime/InvokerHelper.java b/src/main/org/codehaus/groovy/runtime/InvokerHelper.java
index 199ed74..41fb360 100644
--- a/src/main/org/codehaus/groovy/runtime/InvokerHelper.java
+++ b/src/main/org/codehaus/groovy/runtime/InvokerHelper.java
@@ -18,7 +18,23 @@
*/
package org.codehaus.groovy.runtime;
-import groovy.lang.*;
+import groovy.lang.Binding;
+import groovy.lang.Closure;
+import groovy.lang.GString;
+import groovy.lang.GroovyInterceptable;
+import groovy.lang.GroovyObject;
+import groovy.lang.GroovyRuntimeException;
+import groovy.lang.GroovySystem;
+import groovy.lang.MetaClass;
+import groovy.lang.MetaClassRegistry;
+import groovy.lang.MissingMethodException;
+import groovy.lang.MissingPropertyException;
+import groovy.lang.Range;
+import groovy.lang.Script;
+import groovy.lang.SpreadMap;
+import groovy.lang.SpreadMapEvaluatingException;
+import groovy.lang.Tuple;
+import groovy.lang.Writable;
import org.codehaus.groovy.runtime.metaclass.MetaClassRegistryImpl;
import org.codehaus.groovy.runtime.metaclass.MissingMethodExecutionFailed;
import org.codehaus.groovy.runtime.powerassert.PowerAssertionError;
@@ -52,8 +68,6 @@ import java.util.regex.Pattern;
/**
* A static helper class to make bytecode generation easier and act as a facade over the Invoker
- *
- * @author <a href="mailto:james@coredevelopers.net">James Strachan</a>
*/
public class InvokerHelper {
private static final Object[] EMPTY_MAIN_ARGS = new Object[]{new String[0]};
@@ -113,13 +127,14 @@ public class InvokerHelper {
return Arrays.asList((Object[]) value);
}
if (value instanceof Enumeration) {
+ Enumeration e = (Enumeration) value;
List answer = new ArrayList();
- for (Enumeration e = (Enumeration) value; e.hasMoreElements();) {
+ while (e.hasMoreElements()) {
answer.add(e.nextElement());
}
return answer;
}
- // lets assume its a collection of 1
+ // let's assume its a collection of 1
return Collections.singletonList(value);
}
@@ -273,9 +288,8 @@ public class InvokerHelper {
if (value instanceof ArrayList) {
// value is a list.
List newlist = new ArrayList();
- Iterator it = ((ArrayList) value).iterator();
- for (; it.hasNext();) {
- newlist.add(unaryMinus(it.next()));
+ for (Object o : ((ArrayList) value)) {
+ newlist.add(unaryMinus(o));
}
return newlist;
}
@@ -296,9 +310,8 @@ public class InvokerHelper {
if (value instanceof ArrayList) {
// value is a list.
List newlist = new ArrayList();
- Iterator it = ((ArrayList) value).iterator();
- for (; it.hasNext();) {
- newlist.add(unaryPlus(it.next()));
+ for (Object o : ((ArrayList) value)) {
+ newlist.add(unaryPlus(o));
}
return newlist;
}
@@ -358,9 +371,7 @@ public class InvokerHelper {
if (value instanceof Map) {
Object[] values = new Object[((Map) value).keySet().size() * 2];
int index = 0;
- Iterator it = ((Map) value).keySet().iterator();
- for (; it.hasNext();) {
- Object key = it.next();
+ for (Object key : ((Map) value).keySet()) {
values[index++] = key;
values[index++] = ((Map) value).get(key);
}
@@ -381,9 +392,7 @@ public class InvokerHelper {
while (i < values.length - 1) {
if ((values[i] instanceof SpreadMap) && (values[i + 1] instanceof Map)) {
Map smap = (Map) values[i + 1];
- Iterator iter = smap.keySet().iterator();
- for (; iter.hasNext();) {
- Object key = iter.next();
+ for (Object key : smap.keySet()) {
answer.put(key, smap.get(key));
}
i += 2;
@@ -636,9 +645,9 @@ public class InvokerHelper {
return (String) arguments;
}
}
- // TODO: For GROOVY-2599 do we need something like below but it breaks other things
-// return (String) invokeMethod(arguments, "toString", EMPTY_ARGS);
try {
+ // TODO: For GROOVY-2599 do we need something like below but it breaks other things
+// return (String) invokeMethod(arguments, "toString", EMPTY_ARGS);
return arguments.toString();
} catch (RuntimeException ex) {
if (!safe) throw ex;
@@ -817,15 +826,15 @@ public class InvokerHelper {
return toArrayString(arguments, false, -1, false);
}
- private static String toArrayString(Object[] collection, boolean verbose, int maxSize, boolean safe) {
- if (collection == null) {
+ private static String toArrayString(Object[] array, boolean verbose, int maxSize, boolean safe) {
+ if (array == null) {
return "null";
}
boolean first = true;
- StringBuilder argBuf = new StringBuilder(collection.length);
+ StringBuilder argBuf = new StringBuilder(array.length);
argBuf.append('[');
- for (Object item : collection) {
+ for (Object item : array) {
if (first) {
first = false;
} else {
@@ -835,8 +844,8 @@ public class InvokerHelper {
argBuf.append("...");
break;
}
- if (item == collection) {
- argBuf.append("(this Collection)");
+ if (item == array) {
+ argBuf.append("(this array)");
} else {
argBuf.append(format(item, verbose, sizeLeft(maxSize, argBuf), safe));
}
@@ -850,8 +859,8 @@ public class InvokerHelper {
* with brace boundaries "[" and "]".
*
* @param arguments the array to process
- * @param maxSize stop after approximately this many characters and append '...'
- * @param safe whether to use a default object representation for any item in the array if an exception occurs when generating its toString
+ * @param maxSize stop after approximately this many characters and append '...'
+ * @param safe whether to use a default object representation for any item in the array if an exception occurs when generating its toString
* @return the string representation of the array
*/
public static String toArrayString(Object[] arguments, int maxSize, boolean safe) {
@@ -893,9 +902,8 @@ public class InvokerHelper {
if (value instanceof ArrayList) {
// value is a list.
List newlist = new ArrayList();
- Iterator it = ((ArrayList) value).iterator();
- for (; it.hasNext();) {
- newlist.add(bitwiseNegate(it.next()));
+ for (Object o : ((ArrayList) value)) {
+ newlist.add(bitwiseNegate(o));
}
return newlist;
}
http://git-wip-us.apache.org/repos/asf/groovy/blob/be3f4143/src/test/org/codehaus/groovy/runtime/InvokerHelperFormattingTest.groovy
----------------------------------------------------------------------
diff --git a/src/test/org/codehaus/groovy/runtime/InvokerHelperFormattingTest.groovy b/src/test/org/codehaus/groovy/runtime/InvokerHelperFormattingTest.groovy
index 3e87770..479588bc 100644
--- a/src/test/org/codehaus/groovy/runtime/InvokerHelperFormattingTest.groovy
+++ b/src/test/org/codehaus/groovy/runtime/InvokerHelperFormattingTest.groovy
@@ -34,7 +34,7 @@ class InvokerHelperFormattingTest extends GroovyTestCase {
}
}
- public void testToStringLiterals() {
+ void testToStringLiterals() {
assert 'null' == InvokerHelper.toString(null)
assert '0.5' == InvokerHelper.toString(0.5)
assert '2' == InvokerHelper.toString(2)
@@ -44,13 +44,13 @@ class InvokerHelperFormattingTest extends GroovyTestCase {
}
- public void testToStringThrows() {
+ void testToStringThrows() {
shouldFail(UnsupportedOperationException) {
InvokerHelper.toString(new ExceptionOnToString())
}
}
- public void testFormat() {
+ void testFormat() {
assert 'null' == InvokerHelper.format(null, false)
assert '0.5' == InvokerHelper.format(0.5, false)
assert '2' == InvokerHelper.format(2, false)
@@ -75,7 +75,7 @@ class InvokerHelperFormattingTest extends GroovyTestCase {
assert InvokerHelper.format(new ExceptionOnToString(), false, -1, true) =~ (ExceptionOnToString.MATCHER)
}
- public void testFormatRanges() {
+ void testFormatRanges() {
assert '1..4' == InvokerHelper.format(1..4, false)
assert "a'b..a'd" == InvokerHelper.format('a\'b'..'a\'d', false)
assert "[1..4]" == InvokerHelper.format([1..4], false)
@@ -96,12 +96,12 @@ class InvokerHelperFormattingTest extends GroovyTestCase {
assert InvokerHelper.format(eObject..eObject, false, -1, true) == '<groovy.lang.ObjectRange@????>'
}
- public void testToStringLists() {
+ void testToStringLists() {
assert '[]' == InvokerHelper.toString([])
assert '[1, true, a, \'b\']' == InvokerHelper.toString([1, true, 'a, \'b\''])
}
- public void testToListString() {
+ void testToListString() {
assert '[]' == InvokerHelper.toString([])
assert '[1, true, a, \'b\']' == InvokerHelper.toListString([1, true, 'a, \'b\''])
assert '[1, ...]' == InvokerHelper.toListString([1, true, 'a, \'b\''], 2)
@@ -130,19 +130,19 @@ class InvokerHelperFormattingTest extends GroovyTestCase {
}
}
- public void testToStringRanges() {
+ void testToStringRanges() {
assert '1..4' == InvokerHelper.toString(1..4)
assert "a'b..a'd" == InvokerHelper.toString('a\'b'..'a\'d')
assert "[1..4]" == InvokerHelper.toString([1..4])
assert "[a'b..a'd]" == InvokerHelper.toString(['a\'b'..'a\'d'])
}
- public void testToStringArrays() {
+ void testToStringArrays() {
assert "[a, a'b]" == InvokerHelper.toString(['a', 'a\'b'] as String[])
assert "[a, a'b]" == InvokerHelper.toString(['a', 'a\'b'] as Object[])
}
- public void testFormatArrays() {
+ void testFormatArrays() {
assert "[a, a'b]" == InvokerHelper.format(['a', 'a\'b'] as String[], false)
assert "[a, a'b]" == InvokerHelper.format(['a', 'a\'b'] as Object[], false)
assert "['a', 'a\\'b']" == InvokerHelper.format(['a', 'a\'b'] as String[], true)
@@ -155,12 +155,12 @@ class InvokerHelperFormattingTest extends GroovyTestCase {
assert InvokerHelper.format([new ExceptionOnToString()] as Object[], false, -1, true) =~ "\\[${ExceptionOnToString.MATCHER}\\]"
}
- public void testToStringMaps() {
+ void testToStringMaps() {
assert '[:]' == InvokerHelper.toString([:])
assert "[a'b:1, 2:b'c]" == InvokerHelper.toString(['a\'b':1, 2:'b\'c'])
}
- public void testFormatMaps() {
+ void testFormatMaps() {
assert '[:]' == InvokerHelper.format([:], false)
assert "[a'b:1, 2:b'c]" == InvokerHelper.format(['a\'b':1, 2:'b\'c'], false)
assert "['a\\'b':1, 2:'b\\'c']" == InvokerHelper.format(['a\'b':1, 2:'b\'c'], true, -1, true)
@@ -177,7 +177,7 @@ class InvokerHelperFormattingTest extends GroovyTestCase {
assert InvokerHelper.format(m, false, -1, true) =~ "\\[${ExceptionOnToString.MATCHER}:${ExceptionOnToString.MATCHER}\\]"
}
- public void testToMapString() {
+ void testToMapString() {
assert '[:]' == InvokerHelper.toMapString([:])
assert "[a'b:1, 2:b'c]" == InvokerHelper.toMapString(['a\'b':1, 2:'b\'c'])
assert "[a'b:1, ...]" == InvokerHelper.toMapString(['a\'b':1, 2:'b\'c'], 2)
@@ -191,7 +191,7 @@ class InvokerHelperFormattingTest extends GroovyTestCase {
}
}
- public void testEmbedded() {
+ void testEmbedded() {
List list = []
list.add(['a\'b': 'c\'d'])
list.add(['e', 'f', 'g'])
@@ -204,7 +204,7 @@ class InvokerHelperFormattingTest extends GroovyTestCase {
assert "['key':[['a\\'b':'c\\'d'], ['e', 'f', 'g'], 5..9, 'fog'..'fop', ['h', 'i'], [10, 11]]]" == InvokerHelper.format([key:list], true, -1, false)
}
- public void testToStringSelfContained() {
+ void testToStringSelfContained() {
List l = [];
l.add(l)
assert '[(this Collection)]' == InvokerHelper.toString(l)
[6/7] groovy git commit: GROOVY-7563: InvokerHelper: Safe formatting
of ranges
Posted by pa...@apache.org.
GROOVY-7563: InvokerHelper: Safe formatting of ranges
Project: http://git-wip-us.apache.org/repos/asf/groovy/repo
Commit: http://git-wip-us.apache.org/repos/asf/groovy/commit/9be00f21
Tree: http://git-wip-us.apache.org/repos/asf/groovy/tree/9be00f21
Diff: http://git-wip-us.apache.org/repos/asf/groovy/diff/9be00f21
Branch: refs/heads/master
Commit: 9be00f218b56739fa6b850136f83abf024d5f44b
Parents: 56904d6
Author: Thibault Kruse <th...@gmx.de>
Authored: Wed Sep 2 19:40:55 2015 +0200
Committer: paulk <pa...@asert.com.au>
Committed: Fri Jul 29 08:36:47 2016 +1000
----------------------------------------------------------------------
.../org/codehaus/groovy/runtime/InvokerHelper.java | 16 ++++++++++++----
.../runtime/InvokerHelperFormattingTest.groovy | 2 ++
2 files changed, 14 insertions(+), 4 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/groovy/blob/9be00f21/src/main/org/codehaus/groovy/runtime/InvokerHelper.java
----------------------------------------------------------------------
diff --git a/src/main/org/codehaus/groovy/runtime/InvokerHelper.java b/src/main/org/codehaus/groovy/runtime/InvokerHelper.java
index 17f7ca9..199ed74 100644
--- a/src/main/org/codehaus/groovy/runtime/InvokerHelper.java
+++ b/src/main/org/codehaus/groovy/runtime/InvokerHelper.java
@@ -593,10 +593,18 @@ public class InvokerHelper {
}
if (arguments instanceof Range) {
Range range = (Range) arguments;
- if (verbose) {
- return range.inspect();
- } else {
- return range.toString();
+ try {
+ if (verbose) {
+ return range.inspect();
+ } else {
+ return range.toString();
+ }
+ } catch (RuntimeException ex) {
+ if (!safe) throw ex;
+ return handleFormattingException(arguments, ex);
+ } catch (Exception ex) {
+ if (!safe) throw new GroovyRuntimeException(ex);
+ return handleFormattingException(arguments, ex);
}
}
if (arguments instanceof Collection) {
http://git-wip-us.apache.org/repos/asf/groovy/blob/9be00f21/src/test/org/codehaus/groovy/runtime/InvokerHelperFormattingTest.groovy
----------------------------------------------------------------------
diff --git a/src/test/org/codehaus/groovy/runtime/InvokerHelperFormattingTest.groovy b/src/test/org/codehaus/groovy/runtime/InvokerHelperFormattingTest.groovy
index 4e55623..3e87770 100644
--- a/src/test/org/codehaus/groovy/runtime/InvokerHelperFormattingTest.groovy
+++ b/src/test/org/codehaus/groovy/runtime/InvokerHelperFormattingTest.groovy
@@ -92,6 +92,8 @@ class InvokerHelperFormattingTest extends GroovyTestCase {
shouldFail(UnsupportedOperationException) {
InvokerHelper.format(eObject..eObject2)
}
+
+ assert InvokerHelper.format(eObject..eObject, false, -1, true) == '<groovy.lang.ObjectRange@????>'
}
public void testToStringLists() {
[2/7] groovy git commit: GROOVY-7563: InvokerHelper: Print ranges in
their code form in toString
Posted by pa...@apache.org.
GROOVY-7563: InvokerHelper: Print ranges in their code form in toString
Project: http://git-wip-us.apache.org/repos/asf/groovy/repo
Commit: http://git-wip-us.apache.org/repos/asf/groovy/commit/42427df5
Tree: http://git-wip-us.apache.org/repos/asf/groovy/tree/42427df5
Diff: http://git-wip-us.apache.org/repos/asf/groovy/diff/42427df5
Branch: refs/heads/master
Commit: 42427df55a535a559419b157ea7a02f5da8a46c5
Parents: 46e4d32
Author: Thibault Kruse <th...@gmx.de>
Authored: Wed Sep 2 10:54:16 2015 +0200
Committer: paulk <pa...@asert.com.au>
Committed: Fri Jul 29 08:36:39 2016 +1000
----------------------------------------------------------------------
src/main/org/codehaus/groovy/runtime/InvokerHelper.java | 5 -----
.../codehaus/groovy/runtime/InvokerHelperFormattingTest.groovy | 5 ++---
2 files changed, 2 insertions(+), 8 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/groovy/blob/42427df5/src/main/org/codehaus/groovy/runtime/InvokerHelper.java
----------------------------------------------------------------------
diff --git a/src/main/org/codehaus/groovy/runtime/InvokerHelper.java b/src/main/org/codehaus/groovy/runtime/InvokerHelper.java
index 84924be..b27f0fb 100644
--- a/src/main/org/codehaus/groovy/runtime/InvokerHelper.java
+++ b/src/main/org/codehaus/groovy/runtime/InvokerHelper.java
@@ -124,11 +124,6 @@ public class InvokerHelper {
}
public static String toString(Object arguments) {
- if (arguments instanceof Range) {
- // for historic reasons, toString() formats Ranges by printing
- // them as a list, whereas format prints them in .. notation
- return toListString((Collection) arguments);
- }
return format(arguments, false);
}
http://git-wip-us.apache.org/repos/asf/groovy/blob/42427df5/src/test/org/codehaus/groovy/runtime/InvokerHelperFormattingTest.groovy
----------------------------------------------------------------------
diff --git a/src/test/org/codehaus/groovy/runtime/InvokerHelperFormattingTest.groovy b/src/test/org/codehaus/groovy/runtime/InvokerHelperFormattingTest.groovy
index f28d226..7c70159 100644
--- a/src/test/org/codehaus/groovy/runtime/InvokerHelperFormattingTest.groovy
+++ b/src/test/org/codehaus/groovy/runtime/InvokerHelperFormattingTest.groovy
@@ -127,9 +127,8 @@ class InvokerHelperFormattingTest extends GroovyTestCase {
}
public void testToStringRanges() {
- assert '[1, 2, 3, 4]' == InvokerHelper.toString(1..4)
- assert "[a'b, a'c, a'd]" == InvokerHelper.toString('a\'b'..'a\'d')
- // within other lists, ranges get ToStringed in code form
+ assert '1..4' == InvokerHelper.toString(1..4)
+ assert "a'b..a'd" == InvokerHelper.toString('a\'b'..'a\'d')
assert "[1..4]" == InvokerHelper.toString([1..4])
assert "[a'b..a'd]" == InvokerHelper.toString(['a\'b'..'a\'d'])
}