You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by su...@apache.org on 2020/07/13 01:28:40 UTC

[groovy] branch GROOVY-9637 updated: Return a copy when `getStrings` is called

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

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


The following commit(s) were added to refs/heads/GROOVY-9637 by this push:
     new 1a7cbc6  Return a copy when `getStrings` is called
1a7cbc6 is described below

commit 1a7cbc66d25259b6f77b2d029a8970b8f2eb9f61
Author: Daniel Sun <su...@apache.org>
AuthorDate: Mon Jul 13 09:27:08 2020 +0800

    Return a copy when `getStrings` is called
---
 src/main/java/org/codehaus/groovy/runtime/GStringImpl.java | 2 +-
 src/test/groovy/GStringTest.groovy                         | 9 ++++++++-
 2 files changed, 9 insertions(+), 2 deletions(-)

diff --git a/src/main/java/org/codehaus/groovy/runtime/GStringImpl.java b/src/main/java/org/codehaus/groovy/runtime/GStringImpl.java
index 4b660b1..f7ece63 100644
--- a/src/main/java/org/codehaus/groovy/runtime/GStringImpl.java
+++ b/src/main/java/org/codehaus/groovy/runtime/GStringImpl.java
@@ -58,7 +58,7 @@ public class GStringImpl extends GString {
      */
     @Override
     public String[] getStrings() {
-        return strings;
+        return strings.clone();
     }
 
 }
diff --git a/src/test/groovy/GStringTest.groovy b/src/test/groovy/GStringTest.groovy
index 33d2068..cfd745e 100644
--- a/src/test/groovy/GStringTest.groovy
+++ b/src/test/groovy/GStringTest.groovy
@@ -628,11 +628,18 @@ class GStringTest extends GroovyTestCase {
         assert gstr9.toString() === gstr9.toString()
     }
 
-    void testImmutableValues() {
+    void testImmutableStringsAndValues() {
         def x = 42G
         def y = "Answer is $x"
+        def literal = y.toString()
+
         assert 'Answer is 42' == y
         y.values[0] = 'the question'
         assert 'Answer is 42' == y
+
+        y.strings[0] = '6 x 7 = '
+        assert 'Answer is 42' == y
+
+        assert literal === y.toString()
     }
 }