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
commit 623c1e66817f13e11e31c107b1f17d8e4c6b74e2 Author: Daniel Sun <sun...@apache.org> AuthorDate: Thu Jul 23 10:18:21 2020 +0800 Make `GString` final --- src/main/java/groovy/lang/GString.java | 10 ++--- src/test/groovy/lang/DummyGString.java | 47 ---------------------- src/test/groovy/lang/DummyGStringBase.java | 32 --------------- src/test/groovy/lang/GStringTest.java | 24 +++++------ .../codehaus/groovy/runtime/FileAppendTest.groovy | 2 +- .../groovy/runtime/WriterAppendTest.groovy | 2 +- 6 files changed, 19 insertions(+), 98 deletions(-) diff --git a/src/main/java/groovy/lang/GString.java b/src/main/java/groovy/lang/GString.java index ed4b937..11ef1ad 100644 --- a/src/main/java/groovy/lang/GString.java +++ b/src/main/java/groovy/lang/GString.java @@ -41,7 +41,7 @@ import java.util.regex.Pattern; * James Strachan: The lovely name of this class was suggested by Jules Gosnell * and was such a good idea, I couldn't resist :) */ -public class GString extends GroovyObjectSupport implements Comparable, CharSequence, Writable, Buildable, Serializable { +public final class GString extends GroovyObjectSupport implements Comparable, CharSequence, Writable, Buildable, Serializable { private static final long serialVersionUID = -2638020355892246323L; private static final String MKP = "mkp"; @@ -99,8 +99,8 @@ public class GString extends GroovyObjectSupport implements Comparable, CharSequ } } - public final List<Object> getValues() { - return Arrays.asList(values); + public Object[] getValues() { + return values.clone(); } /** @@ -110,8 +110,8 @@ public class GString extends GroovyObjectSupport implements Comparable, CharSequ * the values will result in changes of the GString. It is not recommended * to do so. */ - public final List<String> getStrings() { - return Arrays.asList(strings); + public String[] getStrings() { + return strings.clone(); } public GString plus(GString that) { diff --git a/src/test/groovy/lang/DummyGString.java b/src/test/groovy/lang/DummyGString.java deleted file mode 100644 index beb79a1..0000000 --- a/src/test/groovy/lang/DummyGString.java +++ /dev/null @@ -1,47 +0,0 @@ -/* - * 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 groovy.lang; - -/** - * A hand crafted example GString - */ -public class DummyGString extends DummyGStringBase { - - private MetaClass metaClass; - - public DummyGString(Object[] values) { - this(values, new String[]{"Hello ", "!"}); - } - - public DummyGString(Object[] values, String[] strings) { - super(values, strings); - } - - public MetaClass getMetaClass() { - return metaClass; - } - - public void setMetaClass(MetaClass metaClass) { - this.metaClass = metaClass; - } - - public Object invokeMethod(String name, Object arguments) { - return metaClass.invokeMethod(this, name, arguments); - } -} diff --git a/src/test/groovy/lang/DummyGStringBase.java b/src/test/groovy/lang/DummyGStringBase.java deleted file mode 100644 index d92a581..0000000 --- a/src/test/groovy/lang/DummyGStringBase.java +++ /dev/null @@ -1,32 +0,0 @@ -/* - * 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 groovy.lang; - -public class DummyGStringBase extends GString { - protected String[] strings; - - public DummyGStringBase(Object[] values) { - this(values, new String[]{"Hello ", "!"}); - } - - public DummyGStringBase(final Object[] values, String[] strings) { - super(values, strings); - this.strings = strings; - } -} diff --git a/src/test/groovy/lang/GStringTest.java b/src/test/groovy/lang/GStringTest.java index 0e7655f..37025aa 100644 --- a/src/test/groovy/lang/GStringTest.java +++ b/src/test/groovy/lang/GStringTest.java @@ -27,14 +27,14 @@ import org.codehaus.groovy.runtime.InvokerHelper; public class GStringTest extends GroovyTestCase { public void testIterateOverText() { - DummyGString compString = new DummyGString(new Object[]{"James"}); - assertArrayEquals(new String[]{"Hello ", "!"}, compString.getStrings().toArray(new String[0])); - assertArrayEquals(new Object[]{"James"}, compString.getValues().toArray()); + GString compString = new GString(new Object[]{"James"}, new String[]{"Hello ", "!"}); + assertArrayEquals(new String[]{"Hello ", "!"}, compString.getStrings()); + assertArrayEquals(new Object[]{"James"}, compString.getValues()); assertEquals("Hello James!", compString.toString()); } public void testAppendString() { - DummyGString a = new DummyGString(new Object[]{"James"}); + GString a = new GString(new Object[]{"James"}, new String[]{"Hello ", "!"}); GString result = a.plus(" how are you?"); assertEquals("Hello James! how are you?", result.toString()); assertEquals('J', a.charAt(6)); @@ -42,7 +42,7 @@ public class GStringTest extends GroovyTestCase { } public void testAppendString2() { - DummyGString a = new DummyGString(new Object[]{"James"}, new String[]{"Hello "}); + GString a = new GString(new Object[]{"James"}, new String[]{"Hello "}); GString result = a.plus(" how are you?"); System.out.println("Strings: " + InvokerHelper.toString(result.getStrings())); System.out.println("Values: " + InvokerHelper.toString(result.getValues())); @@ -50,23 +50,23 @@ public class GStringTest extends GroovyTestCase { } public void testAppendGString() { - DummyGString a = new DummyGString(new Object[]{"James"}); - DummyGString b = new DummyGString(new Object[]{"Bob"}); + GString a = new GString(new Object[]{"James"}, new String[]{"Hello ", "!"}); + GString b = new GString(new Object[]{"Bob"}, new String[]{"Hello ", "!"}); GString result = a.plus(b); assertEquals("Hello James!Hello Bob!", result.toString()); } public void testAppendGString2() { - DummyGString a = new DummyGString(new Object[]{"James"}, new String[]{"Hello "}); - DummyGString b = new DummyGString(new Object[]{"Bob"}, new String[]{"Hello "}); + GString a = new GString(new Object[]{"James"}, new String[]{"Hello "}); + GString b = new GString(new Object[]{"Bob"}, new String[]{"Hello "}); GString result = a.plus(b); assertEquals("Hello JamesHello Bob", result.toString()); } public void testEqualsAndHashCode() { - DummyGString a = new DummyGString(new Object[]{Integer.valueOf(1)}); - DummyGString b = new DummyGString(new Object[]{Long.valueOf(1)}); - Comparable c = new DummyGString(new Object[]{new Double(2.3)}); + GString a = new GString(new Object[]{Integer.valueOf(1)}, new String[]{"Hello ", "!"}); + GString b = new GString(new Object[]{Long.valueOf(1)}, new String[]{"Hello ", "!"}); + Comparable c = new GString(new Object[]{new Double(2.3)}, new String[]{"Hello ", "!"}); assertTrue("a == b", a.equals(b)); assertEquals("hashcode a == b", a.hashCode(), b.hashCode()); diff --git a/src/test/org/codehaus/groovy/runtime/FileAppendTest.groovy b/src/test/org/codehaus/groovy/runtime/FileAppendTest.groovy index f67b240..381a452 100644 --- a/src/test/org/codehaus/groovy/runtime/FileAppendTest.groovy +++ b/src/test/org/codehaus/groovy/runtime/FileAppendTest.groovy @@ -43,7 +43,7 @@ class FileAppendTest extends GroovyTestCase { </characters> </groovy> """.stripIndent() - static Writable gPathResult = new DummyGStringBase(text) + static Writable gPathResult = new GString(text , new String[]{"Hello ", "!"}) static gPathWriteTo; public FileAppendTest() { diff --git a/src/test/org/codehaus/groovy/runtime/WriterAppendTest.groovy b/src/test/org/codehaus/groovy/runtime/WriterAppendTest.groovy index 387e55f..7d59319 100644 --- a/src/test/org/codehaus/groovy/runtime/WriterAppendTest.groovy +++ b/src/test/org/codehaus/groovy/runtime/WriterAppendTest.groovy @@ -43,7 +43,7 @@ class WriterAppendTest extends GroovyTestCase { </characters> </groovy> """ - static gPathResult = new DummyGStringBase(text) + static gPathResult = new GString(text, new String[]{"Hello ", "!"}) static gPathWriteTo static defaultEncoding static UTF8_ENCODING