groovy-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From pa...@apache.org
Subject [2/3] groovy git commit: GROOVY-5752: DelegateASTTransformation#addGetterIfNeeded doesn't take boolean isX accessors into account (closes #380)
Date Sat, 06 Aug 2016 00:08:10 GMT
GROOVY-5752: DelegateASTTransformation#addGetterIfNeeded doesn't take boolean isX accessors
into account (closes #380)


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

Branch: refs/heads/master
Commit: 8ed7f551125cf1aa6b927898f0811a86fe783af2
Parents: d96e370
Author: paulk <paulk@asert.com.au>
Authored: Fri Aug 5 21:17:22 2016 +1000
Committer: paulk <paulk@asert.com.au>
Committed: Fri Aug 5 21:28:59 2016 +1000

----------------------------------------------------------------------
 .../transform/DelegateASTTransformation.java    | 34 ++++++++++++++------
 .../transform/DelegateTransformTest.groovy      | 34 +++++++++++++++++---
 2 files changed, 55 insertions(+), 13 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/groovy/blob/8ed7f551/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 03cbd57..949a260 100644
--- a/src/main/org/codehaus/groovy/transform/DelegateASTTransformation.java
+++ b/src/main/org/codehaus/groovy/transform/DelegateASTTransformation.java
@@ -200,15 +200,31 @@ public class DelegateASTTransformation extends AbstractASTTransformation
{
     }
 
     private static void addGetterIfNeeded(DelegateDescription delegate, PropertyNode prop,
String name, boolean allNames) {
-        String getterName = "get" + Verifier.capitalize(name);
-        if (delegate.owner.getGetterMethod(getterName) == null
-                && !shouldSkipPropertyMethod(name, getterName, delegate.excludes,
delegate.includes, allNames)) {
-            delegate.owner.addMethod(getterName,
-                    ACC_PUBLIC,
-                    GenericsUtils.nonGeneric(prop.getType()),
-                    Parameter.EMPTY_ARRAY,
-                    null,
-                    returnS(propX(delegate.getOp, name)));
+        boolean isPrimBool = prop.getOriginType().equals(ClassHelper.boolean_TYPE);
+        // do a little bit of pre-work since Groovy compiler hasn't added property accessors
yet
+        boolean willHaveGetAccessor = true;
+        boolean willHaveIsAccessor = isPrimBool;
+        String suffix = Verifier.capitalize(name);
+        if (isPrimBool) {
+            ClassNode cNode = prop.getDeclaringClass();
+            if (cNode.getGetterMethod("is" + suffix) != null && cNode.getGetterMethod("get"
+ suffix) == null)
+                willHaveGetAccessor = false;
+            if (cNode.getGetterMethod("get" + suffix) != null && cNode.getGetterMethod("is"
+ suffix) == null)
+                willHaveIsAccessor = false;
+        }
+        for (String prefix : new String[]{"get", "is"}) {
+            String getterName = prefix + suffix;
+            if (delegate.owner.getGetterMethod(getterName) == null
+                    && !shouldSkipPropertyMethod(name, getterName, delegate.excludes,
delegate.includes, allNames)) {
+                if (prefix.equals("get") && willHaveGetAccessor || prefix.equals("is")
&& willHaveIsAccessor) {
+                    delegate.owner.addMethod(getterName,
+                            ACC_PUBLIC,
+                            GenericsUtils.nonGeneric(prop.getType()),
+                            Parameter.EMPTY_ARRAY,
+                            null,
+                            returnS(propX(delegate.getOp, name)));
+                }
+            }
         }
     }
     

http://git-wip-us.apache.org/repos/asf/groovy/blob/8ed7f551/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 45218ed..646223a 100644
--- a/src/test/org/codehaus/groovy/transform/DelegateTransformTest.groovy
+++ b/src/test/org/codehaus/groovy/transform/DelegateTransformTest.groovy
@@ -21,10 +21,7 @@ package org.codehaus.groovy.transform
 import gls.CompilableTestSupport
 
 /**
- * @author Alex Tkachman
- * @author Guillaume Laforge
- * @author Paul King
- * @author Andre Steingress
+ * Tests for the @{code @Delegate} AST transform.
  */
 class DelegateTransformTest extends CompilableTestSupport {
 
@@ -754,6 +751,35 @@ assert foo.dm.x == '123'
         // then:
         assert foo.foo() == delegate().foo()
     }
+
+    // GROOVY-5752
+    void testDelegationShouldAccountForPrimitiveBooleanProperties() {
+        assertScript """
+            class A {
+                boolean a
+                boolean b
+                boolean isB() { b }
+                boolean c
+                boolean getC() { c }
+            }
+
+            class B {
+                @Delegate A a = new A(a: true, b: true, c: true)
+            }
+
+            def a = new A(a: true, b: true, c: true)
+            assert a.getA()
+            assert a.isA()
+            assert a.isB()
+            assert a.getC()
+
+            def b = new B()
+            assert b.getA()
+            assert b.isA()
+            assert b.isB()
+            assert b.getC()
+        """
+    }
 }
 
 interface DelegateFoo {


Mime
View raw message