Skip to content

Commit 3d85ccb

Browse files
committed
Fix method conflicts between JDK13 and GDK(closes #1139)
1 parent 0caea18 commit 3d85ccb

3 files changed

Lines changed: 32 additions & 4 deletions

File tree

src/main/java/org/codehaus/groovy/runtime/StringGroovyMethods.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,24 @@
2121
import groovy.lang.Closure;
2222
import groovy.lang.EmptyRange;
2323
import groovy.lang.GString;
24+
import groovy.lang.GroovyRuntimeException;
2425
import groovy.lang.IntRange;
2526
import groovy.lang.Range;
2627
import groovy.transform.stc.ClosureParams;
2728
import groovy.transform.stc.FromString;
2829
import groovy.transform.stc.PickFirstResolver;
2930
import org.apache.groovy.io.StringBuilderWriter;
31+
import org.apache.groovy.lang.annotation.Incubating;
3032
import org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation;
3133
import org.codehaus.groovy.util.CharSequenceReader;
3234

3335
import java.io.BufferedWriter;
3436
import java.io.File;
3537
import java.io.IOException;
3638
import java.io.Writer;
39+
import java.lang.invoke.MethodHandle;
40+
import java.lang.invoke.MethodHandles;
41+
import java.lang.invoke.MethodType;
3742
import java.math.BigDecimal;
3843
import java.math.BigInteger;
3944
import java.util.ArrayList;
@@ -2599,6 +2604,29 @@ public static String stripIndent(CharSequence self) {
25992604
return stripIndent(self, runningCount == -1 ? 0 : runningCount);
26002605
}
26012606

2607+
/**
2608+
* Same logic to {@link #stripIndent(CharSequence)} if {@code forceGroovyBehavior} is {@code true},
2609+
* otherwise Java13's {@code stripIndent} will be invoked
2610+
*
2611+
* @param self The CharSequence to strip the leading spaces from
2612+
* @param forceGroovyBehavior force groovy behavior to avoid conflicts with Java13's stripIndent
2613+
* @since 3.0.0
2614+
*/
2615+
@Incubating
2616+
public static String stripIndent(CharSequence self, boolean forceGroovyBehavior) {
2617+
if (!forceGroovyBehavior) {
2618+
try {
2619+
MethodHandle mh = MethodHandles.lookup().findVirtual(self.getClass(), "stripIndent", MethodType.methodType(String.class));
2620+
return (String) mh.bindTo(self).invokeWithArguments();
2621+
} catch (NoSuchMethodException | IllegalAccessException ignored) {
2622+
} catch (Throwable t) {
2623+
throw new GroovyRuntimeException(t);
2624+
}
2625+
}
2626+
2627+
return stripIndent(self);
2628+
}
2629+
26022630
/**
26032631
* Strip <tt>numChar</tt> leading characters from
26042632
* every line in a CharSequence.

src/test/groovy/lang/ScriptSourcePositionInAstTest.groovy

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ class ScriptSourcePositionInAstTest extends GroovyTestCase {
4949
assert positionsForScript("""\
5050
println 'hello'
5151
println 'bye'
52-
""".stripIndent()) == [[1, 1], [2, 14]]
52+
""".stripIndent(true)) == [[1, 1], [2, 14]]
5353
}
5454

5555
void testScriptWithClasses() {
@@ -58,6 +58,6 @@ class ScriptSourcePositionInAstTest extends GroovyTestCase {
5858
println 'hello'
5959
println 'bye'
6060
class Baz{}
61-
""".stripIndent()) == [[2, 1], [3, 14]]
61+
""".stripIndent(true)) == [[2, 1], [3, 14]]
6262
}
6363
}

src/test/groovy/lang/StripMarginTest.groovy

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ class StripMarginTest extends GroovyTestCase {
5757
def method() {
5858
return 'bar'
5959
}
60-
""".stripIndent()
60+
""".stripIndent(true)
6161

6262
def expected = """
6363
return 'foo'
@@ -79,7 +79,7 @@ def method() {
7979
def method() {
8080
return 'bar'
8181
}
82-
""".stripIndent()
82+
""".stripIndent(true)
8383

8484
def expected = """\
8585
return 'foo'

0 commit comments

Comments
 (0)