|
21 | 21 | import groovy.lang.Closure; |
22 | 22 | import groovy.lang.EmptyRange; |
23 | 23 | import groovy.lang.GString; |
| 24 | +import groovy.lang.GroovyRuntimeException; |
24 | 25 | import groovy.lang.IntRange; |
25 | 26 | import groovy.lang.Range; |
26 | 27 | import groovy.transform.stc.ClosureParams; |
27 | 28 | import groovy.transform.stc.FromString; |
28 | 29 | import groovy.transform.stc.PickFirstResolver; |
29 | 30 | import org.apache.groovy.io.StringBuilderWriter; |
| 31 | +import org.apache.groovy.lang.annotation.Incubating; |
30 | 32 | import org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation; |
31 | 33 | import org.codehaus.groovy.util.CharSequenceReader; |
32 | 34 |
|
33 | 35 | import java.io.BufferedWriter; |
34 | 36 | import java.io.File; |
35 | 37 | import java.io.IOException; |
36 | 38 | import java.io.Writer; |
| 39 | +import java.lang.invoke.MethodHandle; |
| 40 | +import java.lang.invoke.MethodHandles; |
| 41 | +import java.lang.invoke.MethodType; |
37 | 42 | import java.math.BigDecimal; |
38 | 43 | import java.math.BigInteger; |
39 | 44 | import java.util.ArrayList; |
@@ -2599,6 +2604,29 @@ public static String stripIndent(CharSequence self) { |
2599 | 2604 | return stripIndent(self, runningCount == -1 ? 0 : runningCount); |
2600 | 2605 | } |
2601 | 2606 |
|
| 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 | + |
2602 | 2630 | /** |
2603 | 2631 | * Strip <tt>numChar</tt> leading characters from |
2604 | 2632 | * every line in a CharSequence. |
|
0 commit comments