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

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


The following commit(s) were added to refs/heads/master by this push:
     new 297488104e GROOVY-6360: Add more `String` methods to avoid reflection
297488104e is described below

commit 297488104e186cfb23295711f97dc3e75ebc25ef
Author: Daniel Sun <[email protected]>
AuthorDate: Fri Nov 1 21:02:30 2024 +0800

    GROOVY-6360: Add more `String` methods to avoid reflection
    
    Just add `String` methods introduced by Java 11
---
 .../org/codehaus/groovy/runtime/GStringImpl.java   | 25 ++++++++++++++++++++++
 src/test/groovy/GStringTest.groovy                 | 11 ++++++++++
 2 files changed, 36 insertions(+)

diff --git a/src/main/java/org/codehaus/groovy/runtime/GStringImpl.java 
b/src/main/java/org/codehaus/groovy/runtime/GStringImpl.java
index 9d7fc95192..2fe072ab44 100644
--- a/src/main/java/org/codehaus/groovy/runtime/GStringImpl.java
+++ b/src/main/java/org/codehaus/groovy/runtime/GStringImpl.java
@@ -28,6 +28,7 @@ import java.io.Writer;
 import java.lang.reflect.Method;
 import java.nio.charset.Charset;
 import java.util.Locale;
+import java.util.stream.Stream;
 
 /**
  * Default implementation of a GString used by the compiler. A GString consists
@@ -151,6 +152,30 @@ public class GStringImpl extends GString {
         return toString().isEmpty();
     }
 
+    public Stream<String> lines() {
+        return toString().lines();
+    }
+
+    public boolean isBlank() {
+        return toString().isBlank();
+    }
+
+    public String repeat(int count) {
+        return toString().repeat(count);
+    }
+
+    public String stripLeading() {
+        return toString().stripLeading();
+    }
+
+    public String stripTrailing() {
+        return toString().stripTrailing();
+    }
+
+    public String strip() {
+        return toString().strip();
+    }
+
     public int codePointAt(int index) {
         return toString().codePointAt(index);
     }
diff --git a/src/test/groovy/GStringTest.groovy 
b/src/test/groovy/GStringTest.groovy
index ad61c0760b..974265b2ca 100644
--- a/src/test/groovy/GStringTest.groovy
+++ b/src/test/groovy/GStringTest.groovy
@@ -597,6 +597,17 @@ class GStringTest extends GroovyTestCase {
         assert gstring.toString() == 'Green eggs and ham'
     }
 
+    // GROOVY-6360
+    void testGStringDelegatingMethods() {
+        def gstring = " Hello, ${'world'}! "
+        assert gstring.lines().toList() == [' Hello, world! ']
+        assert !gstring.isBlank()
+        assert gstring.repeat(2) == " Hello, world!  Hello, world! "
+        assert gstring.stripLeading() == "Hello, world! "
+        assert gstring.stripTrailing() == " Hello, world!"
+        assert gstring.strip() == "Hello, world!"
+    }
+
     // GROOVY-7494
     void testGStringCoercionForArrayPutAt() {
         String[] fubar = new String[1]

Reply via email to