You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by pa...@apache.org on 2020/07/28 03:11:28 UTC

[groovy] 01/02: GROOVY-9664: Groovy 3.0 does not work with Groovy 2 code using groovy.xml.XmlUtil

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

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

commit 7d30796ec2a9af0a06790b38e2e9b23e769cfc67
Author: Paul King <pa...@asert.com.au>
AuthorDate: Tue Jul 28 12:50:46 2020 +1000

    GROOVY-9664: Groovy 3.0 does not work with Groovy 2 code using groovy.xml.XmlUtil
---
 .../groovy-xml/src/main/java/groovy/xml/XmlUtil.java    | 17 +++++++++++++++++
 1 file changed, 17 insertions(+)

diff --git a/subprojects/groovy-xml/src/main/java/groovy/xml/XmlUtil.java b/subprojects/groovy-xml/src/main/java/groovy/xml/XmlUtil.java
index db8d8bb..1026a1d 100644
--- a/subprojects/groovy-xml/src/main/java/groovy/xml/XmlUtil.java
+++ b/subprojects/groovy-xml/src/main/java/groovy/xml/XmlUtil.java
@@ -406,6 +406,10 @@ public class XmlUtil {
     }
 
     private static String asString(GPathResult node) {
+        return asStringAdjusted(node);
+    }
+
+    private static String asStringAdjusted(Object node) {
         // little bit of hackery to avoid Groovy dependency in this file
         try {
             Object builder = Class.forName("groovy.xml.StreamingMarkupBuilder").getDeclaredConstructor().newInstance();
@@ -423,6 +427,9 @@ public class XmlUtil {
         if (writable instanceof GPathResult) {
             return asString((GPathResult) writable); //GROOVY-4285
         }
+        if (isOrExtendsLegacyGPathResult(writable.getClass())) {
+            return asStringAdjusted(writable); //GROOVY-4285 legacy case
+        }
         Writer sw = new StringBuilderWriter();
         try {
             writable.writeTo(sw);
@@ -432,6 +439,16 @@ public class XmlUtil {
         return sw.toString();
     }
 
+    private static boolean isOrExtendsLegacyGPathResult(Class candidate) {
+        if (candidate.getName().equals("java.lang.Object")) {
+            return false;
+        }
+        if (candidate.getName().endsWith(".slurpersupport.GPathResult")) {
+            return true;
+        }
+        return isOrExtendsLegacyGPathResult(candidate.getSuperclass());
+    }
+
     private static StreamSource asStreamSource(String xmlString) {
         return new StreamSource(new StringReader(xmlString));
     }