[1/2] groovy git commit: GROOVY-8132: Owner properties should be preferred over properties of @Delegate (port to GROOVY_2_4_X)

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

[1/2] groovy git commit: GROOVY-8132: Owner properties should be preferred over properties of @Delegate (port to GROOVY_2_4_X)

paulk
Repository: groovy
Updated Branches:
  refs/heads/GROOVY_2_4_X 88dbd0e85 -> 715e99a58


GROOVY-8132: Owner properties should be preferred over properties of @Delegate (port to GROOVY_2_4_X)


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

Branch: refs/heads/GROOVY_2_4_X
Commit: 1ca681c566f61d8b494d4aadd3929e91a60a9196
Parents: 88dbd0e
Author: Shil Sinha <[hidden email]>
Authored: Wed Mar 22 13:26:24 2017 -0400
Committer: paulk <[hidden email]>
Committed: Tue Apr 18 12:19:42 2017 +1000

----------------------------------------------------------------------
 .../transform/DelegateTransformTest.groovy      | 30 ++++++++++++++++++++
 1 file changed, 30 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/groovy/blob/1ca681c5/src/test/org/codehaus/groovy/transform/DelegateTransformTest.groovy
----------------------------------------------------------------------
diff --git a/src/test/org/codehaus/groovy/transform/DelegateTransformTest.groovy b/src/test/org/codehaus/groovy/transform/DelegateTransformTest.groovy
index 0d46418..1e9cd18 100644
--- a/src/test/org/codehaus/groovy/transform/DelegateTransformTest.groovy
+++ b/src/test/org/codehaus/groovy/transform/DelegateTransformTest.groovy
@@ -720,6 +720,36 @@ assert foo.dm.x == '123'
             assert b.getC()
         """
     }
+
+    void testOwnerPropertyPreferredToDelegateProperty() {
+        assertScript '''
+            class Foo {
+                String pls
+                @groovy.lang.Delegate
+                Bar bar
+            }
+
+            class Bar {
+                String pls        
+            }
+            assert new Foo(pls: 'ok').pls == 'ok'
+        '''
+    }
+
+    void testOwnerMethodPreferredToDelegateMethod() {
+        assertScript '''
+            class Foo {
+                String pls() { 'foo pls' }
+                @groovy.lang.Delegate
+                Bar bar
+            }
+
+            class Bar {
+                String pls() { 'bar pls' }
+            }
+            assert new Foo(bar: new Bar()).pls() == 'foo pls'
+        '''
+    }
 }
 
 interface DelegateFoo {

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

[2/2] groovy git commit: GROOVY-8132: Owner properties should be preferred over properties of @Delegate (port to GROOVY_2_4_X)

paulk
GROOVY-8132: Owner properties should be preferred over properties of @Delegate (port to GROOVY_2_4_X)


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

Branch: refs/heads/GROOVY_2_4_X
Commit: 715e99a5844639595b9b90c7351eba5616ae8770
Parents: 1ca681c
Author: paulk <[hidden email]>
Authored: Tue Apr 18 12:20:46 2017 +1000
Committer: paulk <[hidden email]>
Committed: Tue Apr 18 12:20:46 2017 +1000

----------------------------------------------------------------------
 .../transform/DelegateASTTransformation.java    | 36 ++++++++++++++------
 1 file changed, 25 insertions(+), 11 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/groovy/blob/715e99a5/src/main/org/codehaus/groovy/transform/DelegateASTTransformation.java
----------------------------------------------------------------------
diff --git a/src/main/org/codehaus/groovy/transform/DelegateASTTransformation.java b/src/main/org/codehaus/groovy/transform/DelegateASTTransformation.java
index 6be955c..dc23b7a 100644
--- a/src/main/org/codehaus/groovy/transform/DelegateASTTransformation.java
+++ b/src/main/org/codehaus/groovy/transform/DelegateASTTransformation.java
@@ -22,6 +22,7 @@ import groovy.lang.Delegate;
 import groovy.lang.GroovyObject;
 
 import groovy.lang.Lazy;
+import groovy.lang.Reference;
 import org.codehaus.groovy.ast.ASTNode;
 import org.codehaus.groovy.ast.AnnotatedNode;
 import org.codehaus.groovy.ast.AnnotationNode;
@@ -47,6 +48,7 @@ import java.util.Set;
 
 import static org.codehaus.groovy.ast.ClassHelper.make;
 import static org.codehaus.groovy.ast.tools.GeneralUtils.assignS;
+import static org.codehaus.groovy.ast.tools.GeneralUtils.callThisX;
 import static org.codehaus.groovy.ast.tools.GeneralUtils.callX;
 import static org.codehaus.groovy.ast.tools.GeneralUtils.getAllMethods;
 import static org.codehaus.groovy.ast.tools.GeneralUtils.getAllProperties;
@@ -158,7 +160,7 @@ public class DelegateASTTransformation extends AbstractASTTransformation {
     private void addSetterIfNeeded(FieldNode fieldNode, ClassNode owner, PropertyNode prop, String name, List<String> includes, List<String> excludes) {
         String setterName = "set" + Verifier.capitalize(name);
         if ((prop.getModifiers() & ACC_FINAL) == 0
-                && owner.getSetterMethod(setterName) == null
+                && owner.getSetterMethod(setterName) == null && owner.getProperty(name) == null
                 && !shouldSkipPropertyMethod(name, setterName, excludes, includes)) {
             owner.addMethod(setterName,
                     ACC_PUBLIC,
@@ -183,22 +185,34 @@ public class DelegateASTTransformation extends AbstractASTTransformation {
             if (cNode.getGetterMethod("get" + suffix) != null && cNode.getGetterMethod("is" + suffix) == null)
                 willHaveIsAccessor = false;
         }
+        Reference<Boolean> ownerWillHaveGetAccessor = new Reference<Boolean>();
+        Reference<Boolean> ownerWillHaveIsAccessor = new Reference<Boolean>();
+        extractAccessorInfo(owner, name, ownerWillHaveGetAccessor, ownerWillHaveIsAccessor);
+
         for (String prefix : new String[]{"get", "is"}) {
             String getterName = prefix + suffix;
-            if (owner.getGetterMethod(getterName) == null
+            if ((prefix.equals("get") && willHaveGetAccessor && !ownerWillHaveGetAccessor.get()
+                    || prefix.equals("is") && willHaveIsAccessor && !ownerWillHaveIsAccessor.get())
                     && !shouldSkipPropertyMethod(name, getterName, excludes, includes)) {
-                if (prefix.equals("get") && willHaveGetAccessor || prefix.equals("is") && willHaveIsAccessor) {
-                    owner.addMethod(getterName,
-                            ACC_PUBLIC,
-                            GenericsUtils.nonGeneric(prop.getType()),
-                            Parameter.EMPTY_ARRAY,
-                            null,
-                            returnS(propX(varX(fieldNode), name)));
-                }
+                owner.addMethod(getterName,
+                        ACC_PUBLIC,
+                        GenericsUtils.nonGeneric(prop.getType()),
+                        Parameter.EMPTY_ARRAY,
+                        null,
+                        returnS(propX(varX(fieldNode), name)));
             }
         }
     }
-    
+
+    private static void extractAccessorInfo(ClassNode owner, String name, Reference<Boolean> willHaveGetAccessor, Reference<Boolean> willHaveIsAccessor) {
+        String suffix = Verifier.capitalize(name);
+        boolean hasGetAccessor = owner.getGetterMethod("get" + suffix) != null;
+        boolean hasIsAccessor = owner.getGetterMethod("is" + suffix) != null;
+        PropertyNode prop = owner.getProperty(name);
+        willHaveGetAccessor.set(hasGetAccessor || (prop != null && !hasIsAccessor));
+        willHaveIsAccessor.set(hasIsAccessor || (prop != null && !hasGetAccessor && prop.getOriginType().equals(ClassHelper.boolean_TYPE)));
+    }
+
     private boolean shouldSkipPropertyMethod(String propertyName, String methodName, List<String> excludes, List<String> includes) {
         return (deemedInternalName(propertyName)
                     || excludes != null && (excludes.contains(propertyName) || excludes.contains(methodName))

Loading...