Skip to content

Commit

Permalink
GROOVY-6360: Add more String methods to avoid reflection
Browse files Browse the repository at this point in the history
Just add `String` methods introduced by Java 11
  • Loading branch information
daniellansun committed Nov 3, 2024
1 parent 6b12d2a commit 2974881
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
25 changes: 25 additions & 0 deletions src/main/java/org/codehaus/groovy/runtime/GStringImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
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
Expand Down Expand Up @@ -151,6 +152,30 @@ public boolean isEmpty() {
return toString().isEmpty();
}

public Stream<String> lines() {

This comment has been minimized.

Copy link
@eric-milles

eric-milles Nov 4, 2024

Member

Can you add "@SInCE" javadoc metadata for these?

This comment has been minimized.

Copy link
@daniellansun

daniellansun Nov 9, 2024

Author Contributor
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);
}
Expand Down
11 changes: 11 additions & 0 deletions src/test/groovy/GStringTest.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down

0 comments on commit 2974881

Please sign in to comment.