[groovy] branch GROOVY_3_0_X updated: GROOVY-7494: ArrayStoreException assigning GStringImpl to String[] when using Indy

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

[groovy] branch GROOVY_3_0_X updated: GROOVY-7494: ArrayStoreException assigning GStringImpl to String[] when using Indy

Daniel.Sun
This is an automated email from the ASF dual-hosted git repository.

sunlan pushed a commit to branch GROOVY_3_0_X
in repository https://gitbox.apache.org/repos/asf/groovy.git


The following commit(s) were added to refs/heads/GROOVY_3_0_X by this push:
     new 7c491df  GROOVY-7494: ArrayStoreException assigning GStringImpl to String[] when using Indy
7c491df is described below

commit 7c491df7cdeba81fe528e2005780d94e076f9743
Author: Paul King <[hidden email]>
AuthorDate: Wed Oct 14 18:15:07 2020 +0800

    GROOVY-7494: ArrayStoreException assigning GStringImpl to String[] when using Indy
   
    (cherry picked from commit bad02b2f8d9ba3906321af2cf2ffd8abbb51a954)
---
 .../groovy/runtime/dgmimpl/arrays/ObjectArrayPutAtMetaMethod.java  | 2 ++
 src/test/groovy/GStringTest.groovy                                 | 7 +++++++
 2 files changed, 9 insertions(+)

diff --git a/src/main/java/org/codehaus/groovy/runtime/dgmimpl/arrays/ObjectArrayPutAtMetaMethod.java b/src/main/java/org/codehaus/groovy/runtime/dgmimpl/arrays/ObjectArrayPutAtMetaMethod.java
index 3db454d..55ace06 100644
--- a/src/main/java/org/codehaus/groovy/runtime/dgmimpl/arrays/ObjectArrayPutAtMetaMethod.java
+++ b/src/main/java/org/codehaus/groovy/runtime/dgmimpl/arrays/ObjectArrayPutAtMetaMethod.java
@@ -56,6 +56,8 @@ public class ObjectArrayPutAtMetaMethod extends ArrayPutAtMetaMethod {
             }
         } else if (Character.class.isAssignableFrom(arrayComponentClass)) {
             adjustedNewVal = DefaultTypeTransformation.getCharFromSizeOneString(newValue);
+        } else if (String.class.equals(arrayComponentClass) && newValue instanceof GString) {
+            adjustedNewVal = DefaultTypeTransformation.castToType(newValue, arrayComponentClass);
         } else if (Number.class.isAssignableFrom(arrayComponentClass)) {
             if (newValue instanceof Character || newValue instanceof String || newValue instanceof GString) {
                 Character ch = DefaultTypeTransformation.getCharFromSizeOneString(newValue);
diff --git a/src/test/groovy/GStringTest.groovy b/src/test/groovy/GStringTest.groovy
index 6b5c24a..663f449 100644
--- a/src/test/groovy/GStringTest.groovy
+++ b/src/test/groovy/GStringTest.groovy
@@ -593,4 +593,11 @@ class GStringTest extends GroovyTestCase {
         gstring.strings[1] = ' and '
         assert gstring.toString() == 'Green eggs and ham'
     }
+
+    // GROOVY-7494
+    void testGStringCoercionForArrayPutAt() {
+        String[] fubar = new String[1]
+        fubar[0] = "x${'y'}"
+        assert fubar.toString() == '[xy]'
+    }
 }

Apache Groovy committer & PMC member

Blog: http://blog.sunlan.me
Twitter: @daniel_sun