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 e25a114  Return a copy when `getValues` is called
e25a114 is described below

commit e25a11450e86d2dc6ccc3361f523de1b4825ed9d
Author: Daniel Sun <sun...@apache.org>
AuthorDate: Mon Jul 13 07:54:41 2020 +0800

    Return a copy when `getValues` is called
---
 src/main/java/groovy/lang/GString.java | 6 +++---
 src/test/groovy/GStringTest.groovy     | 8 ++++++++
 2 files changed, 11 insertions(+), 3 deletions(-)

diff --git a/src/main/java/groovy/lang/GString.java 
b/src/main/java/groovy/lang/GString.java
index 855fec2..affd9b4 100644
--- a/src/main/java/groovy/lang/GString.java
+++ b/src/main/java/groovy/lang/GString.java
@@ -101,13 +101,13 @@ public abstract class GString extends GroovyObjectSupport 
implements Comparable,
     }
 
     public Object[] getValues() {
-        return values;
+        return values.clone();
     }
 
     public GString plus(GString that) {
-        Object[] values = getValues();
+        Object[] values = this.values;
 
-        return new GStringImpl(appendValues(values, that.getValues()), 
appendStrings(getStrings(), that.getStrings(), values.length));
+        return new GStringImpl(appendValues(values, that.values), 
appendStrings(getStrings(), that.getStrings(), values.length));
     }
 
     private String[] appendStrings(String[] strings, String[] thatStrings, int 
valuesLength) {
diff --git a/src/test/groovy/GStringTest.groovy 
b/src/test/groovy/GStringTest.groovy
index f7ad73e..33d2068 100644
--- a/src/test/groovy/GStringTest.groovy
+++ b/src/test/groovy/GStringTest.groovy
@@ -627,4 +627,12 @@ class GStringTest extends GroovyTestCase {
         def gstr9 = "a${(short) 1}"
         assert gstr9.toString() === gstr9.toString()
     }
+
+    void testImmutableValues() {
+        def x = 42G
+        def y = "Answer is $x"
+        assert 'Answer is 42' == y
+        y.values[0] = 'the question'
+        assert 'Answer is 42' == y
+    }
 }

Reply via email to