groovy-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sun...@apache.org
Subject [groovy] 01/01: Fix method conflicts between JDK and GDK
Date Tue, 14 Jan 2020 00:00:21 GMT
This is an automated email from the ASF dual-hosted git repository.

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

commit 66e85f0e3aae1b38fc52a7564c34c8f16167facc
Author: Daniel Sun <sunlan@apache.org>
AuthorDate: Tue Jan 14 07:59:42 2020 +0800

    Fix method conflicts between JDK and GDK
---
 .../groovy/runtime/StringGroovyMethods.java        | 28 ++++++++++++++++++++++
 .../lang/ScriptSourcePositionInAstTest.groovy      |  4 ++--
 src/test/groovy/lang/StripMarginTest.groovy        |  4 ++--
 3 files changed, 32 insertions(+), 4 deletions(-)

diff --git a/src/main/java/org/codehaus/groovy/runtime/StringGroovyMethods.java b/src/main/java/org/codehaus/groovy/runtime/StringGroovyMethods.java
index 8500643..7191e04 100644
--- a/src/main/java/org/codehaus/groovy/runtime/StringGroovyMethods.java
+++ b/src/main/java/org/codehaus/groovy/runtime/StringGroovyMethods.java
@@ -21,12 +21,14 @@ package org.codehaus.groovy.runtime;
 import groovy.lang.Closure;
 import groovy.lang.EmptyRange;
 import groovy.lang.GString;
+import groovy.lang.GroovyRuntimeException;
 import groovy.lang.IntRange;
 import groovy.lang.Range;
 import groovy.transform.stc.ClosureParams;
 import groovy.transform.stc.FromString;
 import groovy.transform.stc.PickFirstResolver;
 import org.apache.groovy.io.StringBuilderWriter;
+import org.apache.groovy.lang.annotation.Incubating;
 import org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation;
 import org.codehaus.groovy.util.CharSequenceReader;
 
@@ -34,6 +36,9 @@ import java.io.BufferedWriter;
 import java.io.File;
 import java.io.IOException;
 import java.io.Writer;
+import java.lang.invoke.MethodHandle;
+import java.lang.invoke.MethodHandles;
+import java.lang.invoke.MethodType;
 import java.math.BigDecimal;
 import java.math.BigInteger;
 import java.util.ArrayList;
@@ -2600,6 +2605,29 @@ public class StringGroovyMethods extends DefaultGroovyMethodsSupport
{
     }
 
     /**
+     * Same logic to {@link #stripIndent(CharSequence)} if {@code forceGroovyBehavior} is
{@code true},
+     * otherwise Java13's {@code stripIndent} will be invoked
+     *
+     * @param self The CharSequence to strip the leading spaces from
+     * @param forceGroovyBehavior force groovy behavior to avoid conflicts with Java13's
stripIndent
+     * @since 3.0.0
+     */
+    @Incubating
+    public static String stripIndent(CharSequence self, boolean forceGroovyBehavior) {
+        if (!forceGroovyBehavior) {
+            try {
+                MethodHandle mh = MethodHandles.lookup().findVirtual(self.getClass(), "stripIndent",
MethodType.methodType(String.class));
+                return (String) mh.bindTo(self).invokeWithArguments();
+            } catch (NoSuchMethodException | IllegalAccessException ignored) {
+            } catch (Throwable t) {
+                throw new GroovyRuntimeException(t);
+            }
+        }
+
+        return stripIndent(self);
+    }
+
+    /**
      * Strip <tt>numChar</tt> leading characters from
      * every line in a CharSequence.
      * <pre class="groovyTestCase">
diff --git a/src/test/groovy/lang/ScriptSourcePositionInAstTest.groovy b/src/test/groovy/lang/ScriptSourcePositionInAstTest.groovy
index 9c96f21..a883535 100644
--- a/src/test/groovy/lang/ScriptSourcePositionInAstTest.groovy
+++ b/src/test/groovy/lang/ScriptSourcePositionInAstTest.groovy
@@ -49,7 +49,7 @@ class ScriptSourcePositionInAstTest extends GroovyTestCase {
         assert positionsForScript("""\
             println 'hello'
             println 'bye'
-        """.stripIndent()) == [[1, 1], [2, 14]]
+        """.stripIndent(true)) == [[1, 1], [2, 14]]
     }
 
     void testScriptWithClasses() {
@@ -58,6 +58,6 @@ class ScriptSourcePositionInAstTest extends GroovyTestCase {
             println 'hello'
             println 'bye'
             class Baz{}
-        """.stripIndent()) == [[2, 1], [3, 14]]
+        """.stripIndent(true)) == [[2, 1], [3, 14]]
     }
 }
diff --git a/src/test/groovy/lang/StripMarginTest.groovy b/src/test/groovy/lang/StripMarginTest.groovy
index 9862b4b..e37e2fd 100644
--- a/src/test/groovy/lang/StripMarginTest.groovy
+++ b/src/test/groovy/lang/StripMarginTest.groovy
@@ -57,7 +57,7 @@ class StripMarginTest extends GroovyTestCase {
             def method() {
                 return 'bar'
             }
-        """.stripIndent()
+        """.stripIndent(true)
 
         def expected = """
     return 'foo'
@@ -79,7 +79,7 @@ def method() {
             def method() {
                 return 'bar'
             }
-        """.stripIndent()
+        """.stripIndent(true)
         
         def expected = """\
     return 'foo'


Mime
View raw message