Return-Path: X-Original-To: archive-asf-public-internal@cust-asf2.ponee.io Delivered-To: archive-asf-public-internal@cust-asf2.ponee.io Received: from cust-asf.ponee.io (cust-asf.ponee.io [163.172.22.183]) by cust-asf2.ponee.io (Postfix) with ESMTP id 7E18C200B7E for ; Tue, 23 Aug 2016 05:51:46 +0200 (CEST) Received: by cust-asf.ponee.io (Postfix) id 7CC1E160ABD; Tue, 23 Aug 2016 03:51:46 +0000 (UTC) Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by cust-asf.ponee.io (Postfix) with SMTP id C1376160AB3 for ; Tue, 23 Aug 2016 05:51:45 +0200 (CEST) Received: (qmail 57094 invoked by uid 500); 23 Aug 2016 03:51:45 -0000 Mailing-List: contact commits-help@groovy.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@groovy.apache.org Delivered-To: mailing list commits@groovy.apache.org Received: (qmail 57081 invoked by uid 99); 23 Aug 2016 03:51:44 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 23 Aug 2016 03:51:44 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id BC11DE053F; Tue, 23 Aug 2016 03:51:44 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: paulk@apache.org To: commits@groovy.apache.org Date: Tue, 23 Aug 2016 03:51:44 -0000 Message-Id: <1f761b5390634d2cac95a17b5cebb67a@git.apache.org> X-Mailer: ASF-Git Admin Mailer 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 archived-at: Tue, 23 Aug 2016 03:51:46 -0000 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 Authored: Fri Jun 3 14:15:09 2016 +1000 Committer: paulk 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