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:ja...@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)