groovy-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From pa...@apache.org
Subject [1/2] groovy git commit: GROOVY-7853: o.c.g.r.t.DefaultTypeTransformation does not apply the right toString on primitve arrays when transforming to String
Date Tue, 23 Aug 2016 03:51:44 GMT
Repository: groovy
Updated Branches:
  refs/heads/master b914a7447 -> eec3b1f3d


GROOVY-7853: o.c.g.r.t.DefaultTypeTransformation does not apply the right toString on primitve
arrays when transforming to String


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

Branch: refs/heads/master
Commit: 20519b9dd8451bc2415f27521367d31346cbf653
Parents: b914a74
Author: paulk <paulk@asert.com.au>
Authored: Fri Jun 3 14:15:09 2016 +1000
Committer: paulk <paulk@asert.com.au>
Committed: Tue Aug 23 13:46:57 2016 +1000

----------------------------------------------------------------------
 .../runtime/typehandling/ShortTypeHandling.java | 13 +++++-
 .../groovy/classgen/CastToStringTest.groovy     | 44 ++++++++++++++++++++
 2 files changed, 55 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/groovy/blob/20519b9d/src/main/org/codehaus/groovy/runtime/typehandling/ShortTypeHandling.java
----------------------------------------------------------------------
diff --git a/src/main/org/codehaus/groovy/runtime/typehandling/ShortTypeHandling.java b/src/main/org/codehaus/groovy/runtime/typehandling/ShortTypeHandling.java
index f45ca0c..b76f0b9 100644
--- a/src/main/org/codehaus/groovy/runtime/typehandling/ShortTypeHandling.java
+++ b/src/main/org/codehaus/groovy/runtime/typehandling/ShortTypeHandling.java
@@ -20,12 +20,12 @@ package org.codehaus.groovy.runtime.typehandling;
 
 import groovy.lang.GString;
 
+import java.util.Arrays;
+
 /**
  * Class providing various short paths for type conversions. Read the comments
  * to what conditions have to be met to get valid results!
  * Any method here must not depend on the groovy runtime.
- *
- * @author Jochen Theodorou
  */
 public class ShortTypeHandling {
 
@@ -41,6 +41,15 @@ public class ShortTypeHandling {
 
     public static String castToString(Object object) {
         if (object==null) return null;
+        if (object instanceof boolean[]) return Arrays.toString((boolean[])object);
+        if (object instanceof byte[]) return Arrays.toString((byte[])object);
+        if (object instanceof char[]) return new String((char[])object);
+        if (object instanceof double[]) return Arrays.toString((double[])object);
+        if (object instanceof float[]) return Arrays.toString((float[])object);
+        if (object instanceof int[]) return Arrays.toString((int[])object);
+        if (object instanceof long[]) return Arrays.toString((long[])object);
+        if (object instanceof short[]) return Arrays.toString((short[])object);
+        if (object instanceof Object[]) return Arrays.toString((Object[])object);
         return object.toString();
     }
 

http://git-wip-us.apache.org/repos/asf/groovy/blob/20519b9d/src/test/org/codehaus/groovy/classgen/CastToStringTest.groovy
----------------------------------------------------------------------
diff --git a/src/test/org/codehaus/groovy/classgen/CastToStringTest.groovy b/src/test/org/codehaus/groovy/classgen/CastToStringTest.groovy
new file mode 100644
index 0000000..350bd52
--- /dev/null
+++ b/src/test/org/codehaus/groovy/classgen/CastToStringTest.groovy
@@ -0,0 +1,44 @@
+/*
+ * 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.classgen
+
+class CastToStringTest extends GroovyShellTestCase {
+    void testNormalAndPrimitiveArrayHandling_Groovy7853() {
+        assertScript '''
+            byte[] bytes = "hello".bytes
+            char[] chars = "hello".chars
+            boolean[] flags = [true, false]
+            long[] nums = [34L, 45L]
+            String[] pets = ['cat', 'dog']
+
+            String convert(byte[] data) { data }
+            String convert(char[] data) { data }
+            String convert(boolean[] data) { data }
+            String convert(long[] data) { data }
+            String convert(String[] data) { data }
+
+            assert bytes.toString() == convert(bytes)
+            assert chars.toString() == convert(chars)
+            assert flags.toString() == convert(flags)
+            assert nums.toString() == convert(nums)
+            assert pets.toString() == convert(pets)
+        '''
+    }
+}
\ No newline at end of file


Mime
View raw message