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 2022/03/21 00:43:49 UTC

[groovy] 01/02: Merge some refactoring parts of GROOVY-10439 to ease future maintenance across versions

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 7d0a99cf86714637426b69187b2658cb1915f094
Author: Eric Milles <er...@thomsonreuters.com>
AuthorDate: Thu Jan 6 11:38:40 2022 -0600

    Merge some refactoring parts of GROOVY-10439 to ease future maintenance across versions
---
 .../codehaus/groovy/ast/tools/GeneralUtils.java    | 26 ++++++++++-------
 .../transform/DelegateASTTransformation.java       | 33 ++++++++++------------
 2 files changed, 31 insertions(+), 28 deletions(-)

diff --git a/src/main/java/org/codehaus/groovy/ast/tools/GeneralUtils.java b/src/main/java/org/codehaus/groovy/ast/tools/GeneralUtils.java
index 41e6efe..d3967bf 100644
--- a/src/main/java/org/codehaus/groovy/ast/tools/GeneralUtils.java
+++ b/src/main/java/org/codehaus/groovy/ast/tools/GeneralUtils.java
@@ -81,6 +81,7 @@ import java.util.Set;
 import java.util.function.Consumer;
 
 import static org.apache.groovy.util.BeanUtils.capitalize;
+import static org.codehaus.groovy.ast.tools.GenericsUtils.parameterizeType;
 
 /**
  * Handy methods when working with the Groovy AST
@@ -419,18 +420,23 @@ public class GeneralUtils {
         return result;
     }
 
-    public static Set<ClassNode> getInterfacesAndSuperInterfaces(final ClassNode type) {
-        Set<ClassNode> res = new LinkedHashSet<>();
-        if (type.isInterface()) {
-            res.add(type);
-            return res;
+    private static void addAllInterfaces(final Set<ClassNode> result, final ClassNode source) {
+        for (ClassNode in : source.getInterfaces()) {
+            in = parameterizeType(source, in);
+            if (result.add(in))
+                addAllInterfaces(result, in);
         }
-        ClassNode next = type;
-        while (next != null) {
-            res.addAll(next.getAllInterfaces());
-            next = next.getSuperClass();
+        ClassNode sc = source.redirect().getUnresolvedSuperClass(false);
+        if (sc != null && ClassHelper.OBJECT_TYPE != sc) {
+            addAllInterfaces(result, parameterizeType(source, sc));
         }
-        return res;
+    }
+
+    public static Set<ClassNode> getInterfacesAndSuperInterfaces(final ClassNode cNode) {
+        Set<ClassNode> result = new LinkedHashSet<>();
+        if (cNode.isInterface()) result.add(cNode);
+        addAllInterfaces(result, cNode);
+        return result;
     }
 
     public static List<FieldNode> getSuperNonPropertyFields(final ClassNode cNode) {
diff --git a/src/main/java/org/codehaus/groovy/transform/DelegateASTTransformation.java b/src/main/java/org/codehaus/groovy/transform/DelegateASTTransformation.java
index 2015fb3..1e58a77 100644
--- a/src/main/java/org/codehaus/groovy/transform/DelegateASTTransformation.java
+++ b/src/main/java/org/codehaus/groovy/transform/DelegateASTTransformation.java
@@ -35,7 +35,6 @@ import org.codehaus.groovy.ast.expr.ArgumentListExpression;
 import org.codehaus.groovy.ast.expr.Expression;
 import org.codehaus.groovy.ast.expr.MethodCallExpression;
 import org.codehaus.groovy.ast.tools.BeanUtils;
-import org.codehaus.groovy.ast.tools.GenericsUtils;
 import org.codehaus.groovy.control.CompilePhase;
 import org.codehaus.groovy.control.SourceUnit;
 
@@ -47,6 +46,7 @@ import java.util.ListIterator;
 import java.util.Map;
 import java.util.Set;
 
+import static java.util.Arrays.copyOf;
 import static java.util.stream.Collectors.toSet;
 import static org.apache.groovy.ast.tools.ClassNodeUtils.addGeneratedMethod;
 import static org.apache.groovy.util.BeanUtils.capitalize;
@@ -67,6 +67,7 @@ import static org.codehaus.groovy.ast.tools.GenericsUtils.correctToGenericsSpec;
 import static org.codehaus.groovy.ast.tools.GenericsUtils.correctToGenericsSpecRecurse;
 import static org.codehaus.groovy.ast.tools.GenericsUtils.createGenericsSpec;
 import static org.codehaus.groovy.ast.tools.GenericsUtils.extractSuperClassGenerics;
+import static org.codehaus.groovy.ast.tools.GenericsUtils.nonGeneric;
 import static org.codehaus.groovy.ast.tools.ParameterUtils.parametersEqual;
 
 /**
@@ -185,21 +186,17 @@ public class DelegateASTTransformation extends AbstractASTTransformation {
 
             if (skipInterfaces) return;
 
-            final Set<ClassNode> allInterfaces = getInterfacesAndSuperInterfaces(delegate.type);
-            final Set<ClassNode> ownerIfaces = delegate.owner.getAllInterfaces();
-            Map<String,ClassNode> genericsSpec = createGenericsSpec(delegate.owner);
-            genericsSpec = createGenericsSpec(delegate.type, genericsSpec);
-            for (ClassNode iface : allInterfaces) {
-                if (Modifier.isPublic(iface.getModifiers()) && !ownerIfaces.contains(iface)) {
-                    final ClassNode[] ifaces = delegate.owner.getInterfaces();
-                    final int nFaces = ifaces.length;
-
-                    final ClassNode[] newIfaces = new ClassNode[nFaces + 1];
-                    for (int i = 0; i < nFaces; i += 1) {
-                        newIfaces[i] = correctToGenericsSpecRecurse(genericsSpec, ifaces[i]);
+            Set<ClassNode> addedInterfaces = getInterfacesAndSuperInterfaces(delegate.type);
+            addedInterfaces.removeIf(i -> !Modifier.isPublic(i.getModifiers()));
+            if (!addedInterfaces.isEmpty()) {
+                Set<ClassNode> ownerInterfaces = getInterfacesAndSuperInterfaces(delegate.owner);
+                for (ClassNode i : addedInterfaces) {
+                    if (!ownerInterfaces.contains(i)) {
+                        ClassNode[] faces = delegate.owner.getInterfaces();
+                        faces = copyOf(faces, faces.length + 1);
+                        faces[faces.length - 1] = i;
+                        delegate.owner.setInterfaces(faces);
                     }
-                    newIfaces[nFaces] = correctToGenericsSpecRecurse(genericsSpec, iface);
-                    delegate.owner.setInterfaces(newIfaces);
                 }
             }
         }
@@ -310,7 +307,7 @@ public class DelegateASTTransformation extends AbstractASTTransformation {
                     setterName,
                     ACC_PUBLIC,
                     ClassHelper.VOID_TYPE,
-                    params(new Parameter(GenericsUtils.nonGeneric(prop.getType()), "value")),
+                    params(new Parameter(nonGeneric(prop.getType()), "value")),
                     null,
                     assignS(propX(delegate.getOp, name), varX("value"))
             );
@@ -342,7 +339,7 @@ public class DelegateASTTransformation extends AbstractASTTransformation {
                     delegate.owner,
                     getterName,
                     ACC_PUBLIC,
-                    GenericsUtils.nonGeneric(prop.getType()),
+                    nonGeneric(prop.getType()),
                     Parameter.EMPTY_ARRAY,
                     null,
                     returnS(propX(delegate.getOp, name))
@@ -355,7 +352,7 @@ public class DelegateASTTransformation extends AbstractASTTransformation {
                     delegate.owner,
                     isserName,
                     ACC_PUBLIC,
-                    GenericsUtils.nonGeneric(prop.getType()),
+                    nonGeneric(prop.getType()),
                     Parameter.EMPTY_ARRAY,
                     null,
                     returnS(propX(delegate.getOp, name))