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 cf9f36def3f1a5288dd8d3515de2ef8747302749 Author: Daniel Sun <sun...@apache.org> AuthorDate: Thu Jul 23 11:12:38 2020 +0800 Make `GString` final --- src/main/java/groovy/lang/GString.java | 22 +++++----- 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, 26 insertions(+), 103 deletions(-) diff --git a/src/main/java/groovy/lang/GString.java b/src/main/java/groovy/lang/GString.java index ed4b937..c6fc509 100644 --- a/src/main/java/groovy/lang/GString.java +++ b/src/main/java/groovy/lang/GString.java @@ -27,8 +27,6 @@ import java.io.IOException; import java.io.Serializable; import java.io.UnsupportedEncodingException; import java.io.Writer; -import java.util.Arrays; -import java.util.List; import java.util.regex.Pattern; /** @@ -41,7 +39,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"; @@ -60,9 +58,13 @@ public class GString extends GroovyObjectSupport implements Comparable, CharSequ private final boolean immutable; public GString(Object values, String[] strings) { - this.values = (Object[]) values; - this.strings = strings; - this.immutable = checkImmutable(this.values); + this(convertToObjectArray(values), strings); + } + + private static Object[] convertToObjectArray(Object values) { + if (null == values) return EMPTY_OBJECT_ARRAY; + + return values instanceof Object[] ? (Object[]) values : new Object[] { values }; } /** @@ -99,8 +101,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 +112,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