[groovy] branch GROOVY-9183 updated: GROOVY-9183: add support for boolean properties

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

[groovy] branch GROOVY-9183 updated: GROOVY-9183: add support for boolean properties

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

emilles pushed a commit to branch GROOVY-9183
in repository https://gitbox.apache.org/repos/asf/groovy.git


The following commit(s) were added to refs/heads/GROOVY-9183 by this push:
     new d7b55f0  GROOVY-9183: add support for boolean properties
d7b55f0 is described below

commit d7b55f067abefab542e08909cd3d69fc7b3696ce
Author: Eric Milles <[hidden email]>
AuthorDate: Sun Dec 1 12:56:35 2019 -0600

    GROOVY-9183: add support for boolean properties
---
 .../transform/NamedVariantASTTransformation.java       | 18 ++++++++++++++----
 1 file changed, 14 insertions(+), 4 deletions(-)

diff --git a/src/main/java/org/codehaus/groovy/transform/NamedVariantASTTransformation.java b/src/main/java/org/codehaus/groovy/transform/NamedVariantASTTransformation.java
index ae9ea5c..9316a07 100644
--- a/src/main/java/org/codehaus/groovy/transform/NamedVariantASTTransformation.java
+++ b/src/main/java/org/codehaus/groovy/transform/NamedVariantASTTransformation.java
@@ -51,8 +51,9 @@ import static org.apache.groovy.ast.tools.ClassNodeUtils.isInnerClass;
 import static org.apache.groovy.ast.tools.VisibilityUtils.getVisibility;
 import static org.codehaus.groovy.ast.ClassHelper.MAP_TYPE;
 import static org.codehaus.groovy.ast.ClassHelper.STRING_TYPE;
+import static org.codehaus.groovy.ast.ClassHelper.boolean_TYPE;
 import static org.codehaus.groovy.ast.ClassHelper.getWrapper;
-import static org.codehaus.groovy.ast.ClassHelper.isNumberType;
+import static org.codehaus.groovy.ast.ClassHelper.isPrimitiveType;
 import static org.codehaus.groovy.ast.ClassHelper.make;
 import static org.codehaus.groovy.ast.ClassHelper.makeWithoutCaching;
 import static org.codehaus.groovy.ast.tools.GeneralUtils.args;
@@ -190,8 +191,7 @@ public class NamedVariantASTTransformation extends AbstractASTTransformation {
         for (PropertyNode pNode : props) {
             String name = pNode.getName();
             // create entry [name: __namedArgs.getOrDefault('name', initialValue)]
-            Expression defaultValue = Optional.ofNullable(pNode.getInitialExpression()).orElseGet(() ->
-                    isNumberType(pNode.getType()) ? castX(getWrapper(pNode.getType()), constX(0)) : nullX());
+            Expression defaultValue = Optional.ofNullable(pNode.getInitialExpression()).orElseGet(() -> getDefaultExpression(pNode.getType()));
             entries.add(entryX(constX(name), callX(varX(mapParam), "getOrDefault", args(constX(name), defaultValue))));
             // create annotation @NamedParam(value='name', type=DelegateType)
             AnnotationNode namedParam = new AnnotationNode(NAMED_PARAM_TYPE);
@@ -204,6 +204,16 @@ public class NamedVariantASTTransformation extends AbstractASTTransformation {
         return true;
     }
 
+    private Expression getDefaultExpression(ClassNode pType) {
+        if (isPrimitiveType(pType)) {
+            if (boolean_TYPE.equals(pType)) {
+                return constX(Boolean.FALSE);
+            }
+            return castX(getWrapper(pType), constX(0));
+        }
+        return nullX();
+    }
+
     private boolean hasDuplicates(final MethodNode mNode, final List<String> propNames, final String next) {
         if (propNames.contains(next)) {
             addError("Error during " + NAMED_VARIANT + " processing. Duplicate property '" + next + "' found.", mNode);
@@ -255,4 +265,4 @@ public class NamedVariantASTTransformation extends AbstractASTTransformation {
             );
         }
     }
-}
\ No newline at end of file
+}