groovy-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From pa...@apache.org
Subject groovy git commit: GROOVY-8455: @TupleConstructor gives wrong ordering when includeSuperFields is set
Date Mon, 22 Jan 2018 22:13:19 GMT
Repository: groovy
Updated Branches:
  refs/heads/GROOVY_2_5_X dde9aa2cf -> ff56ec147


GROOVY-8455: @TupleConstructor gives wrong ordering when includeSuperFields is set


Project: http://git-wip-us.apache.org/repos/asf/groovy/repo
Commit: http://git-wip-us.apache.org/repos/asf/groovy/commit/ff56ec14
Tree: http://git-wip-us.apache.org/repos/asf/groovy/tree/ff56ec14
Diff: http://git-wip-us.apache.org/repos/asf/groovy/diff/ff56ec14

Branch: refs/heads/GROOVY_2_5_X
Commit: ff56ec14784c9c1783ff60e8873904a5b904ad90
Parents: dde9aa2
Author: paulk <paulk@asert.com.au>
Authored: Mon Jan 22 23:28:56 2018 +1000
Committer: paulk <paulk@asert.com.au>
Committed: Tue Jan 23 08:13:03 2018 +1000

----------------------------------------------------------------------
 .../codehaus/groovy/ast/tools/GeneralUtils.java | 24 ++++++++++++++
 .../TupleConstructorASTTransformation.java      | 10 ++----
 .../TupleConstructorTransformTest.groovy        | 34 ++++++++++++++++++++
 3 files changed, 61 insertions(+), 7 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/groovy/blob/ff56ec14/src/main/java/org/codehaus/groovy/ast/tools/GeneralUtils.java
----------------------------------------------------------------------
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 252d6e3..33bdd55 100644
--- a/src/main/java/org/codehaus/groovy/ast/tools/GeneralUtils.java
+++ b/src/main/java/org/codehaus/groovy/ast/tools/GeneralUtils.java
@@ -463,6 +463,30 @@ public class GeneralUtils {
         return result;
     }
 
+    public static List<FieldNode> getAllFields(ClassNode cNode, boolean includeSuperProperties,
boolean includeSuperFields) {
+        final List<FieldNode> result;
+        if (cNode == ClassHelper.OBJECT_TYPE) {
+            result = new ArrayList<FieldNode>();
+        } else {
+            result = getAllFields(cNode.getSuperClass(), includeSuperProperties, includeSuperFields);
+        }
+        if (includeSuperProperties) {
+            for (PropertyNode pNode : cNode.getProperties()) {
+                if (!pNode.isStatic()) {
+                    result.add(pNode.getField());
+                }
+            }
+        }
+        if (includeSuperFields) {
+            for (FieldNode fNode : cNode.getFields()) {
+                if (!fNode.isStatic() && cNode.getProperty(fNode.getName()) == null)
{
+                    result.add(fNode);
+                }
+            }
+        }
+        return result;
+    }
+
     public static BinaryExpression hasClassX(Expression instance, ClassNode cNode) {
         return eqX(classX(cNode), callX(instance, "getClass"));
     }

http://git-wip-us.apache.org/repos/asf/groovy/blob/ff56ec14/src/main/java/org/codehaus/groovy/transform/TupleConstructorASTTransformation.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/codehaus/groovy/transform/TupleConstructorASTTransformation.java
b/src/main/java/org/codehaus/groovy/transform/TupleConstructorASTTransformation.java
index 2dd469d..d30fbd6 100644
--- a/src/main/java/org/codehaus/groovy/transform/TupleConstructorASTTransformation.java
+++ b/src/main/java/org/codehaus/groovy/transform/TupleConstructorASTTransformation.java
@@ -58,11 +58,10 @@ import static org.codehaus.groovy.ast.tools.GeneralUtils.constX;
 import static org.codehaus.groovy.ast.tools.GeneralUtils.copyStatementsWithSuperAdjustment;
 import static org.codehaus.groovy.ast.tools.GeneralUtils.ctorX;
 import static org.codehaus.groovy.ast.tools.GeneralUtils.equalsNullX;
+import static org.codehaus.groovy.ast.tools.GeneralUtils.getAllFields;
 import static org.codehaus.groovy.ast.tools.GeneralUtils.getInstanceNonPropertyFields;
 import static org.codehaus.groovy.ast.tools.GeneralUtils.getInstancePropertyFields;
 import static org.codehaus.groovy.ast.tools.GeneralUtils.getSetterName;
-import static org.codehaus.groovy.ast.tools.GeneralUtils.getSuperNonPropertyFields;
-import static org.codehaus.groovy.ast.tools.GeneralUtils.getSuperPropertyFields;
 import static org.codehaus.groovy.ast.tools.GeneralUtils.ifElseS;
 import static org.codehaus.groovy.ast.tools.GeneralUtils.ifS;
 import static org.codehaus.groovy.ast.tools.GeneralUtils.params;
@@ -169,11 +168,8 @@ public class TupleConstructorASTTransformation extends AbstractASTTransformation
         if (!cNode.getDeclaredConstructors().isEmpty() && !force) return;
 
         List<FieldNode> superList = new ArrayList<FieldNode>();
-        if (includeSuperProperties) {
-            superList.addAll(getSuperPropertyFields(cNode.getSuperClass()));
-        }
-        if (includeSuperFields) {
-            superList.addAll(getSuperNonPropertyFields(cNode.getSuperClass()));
+        if (includeSuperProperties || includeSuperFields) {
+            superList.addAll(getAllFields(cNode.getSuperClass(), includeSuperProperties,
includeSuperFields));
         }
 
         List<FieldNode> list = new ArrayList<FieldNode>();

http://git-wip-us.apache.org/repos/asf/groovy/blob/ff56ec14/src/test/org/codehaus/groovy/transform/TupleConstructorTransformTest.groovy
----------------------------------------------------------------------
diff --git a/src/test/org/codehaus/groovy/transform/TupleConstructorTransformTest.groovy b/src/test/org/codehaus/groovy/transform/TupleConstructorTransformTest.groovy
index af5829b..9d3cacf 100644
--- a/src/test/org/codehaus/groovy/transform/TupleConstructorTransformTest.groovy
+++ b/src/test/org/codehaus/groovy/transform/TupleConstructorTransformTest.groovy
@@ -214,4 +214,38 @@ class TupleConstructorTransformTest extends GroovyShellTestCase {
         '''
     }
 
+    void testSuperPropertyAndSuperFieldOrder_groovy8455() {
+        assertScript '''
+            import groovy.transform.*
+
+            class Foopubf{}
+            class Foop{}
+            class Foo {
+                Foop foop
+                public Foopubf foopubf
+                protected Short fooProtField
+            }
+
+            class Barpubf{}
+            class Barp{}
+            class Bar extends Foo {
+                Barp barp
+                public Barpubf barpubf
+                protected Integer barProtField
+            }
+
+            class Bazpubf{}
+            class Bazp{}
+            @TupleConstructor(includeSuperProperties=true, includeFields=true, includeSuperFields=true)
+            class Baz extends Bar {
+                Bazp bazp
+                public Bazpubf bazpubf
+                protected Long bazProtField
+            }
+
+            assert Baz.constructors.max{ it.parameters.size() }.toString() ==
+                'public Baz(Foop,Foopubf,java.lang.Short,Barp,Barpubf,java.lang.Integer,Bazp,Bazpubf,java.lang.Long)'
+        '''
+    }
+
 }
\ No newline at end of file


Mime
View raw message