You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by em...@apache.org on 2022/02/19 17:12:42 UTC

[groovy] branch GROOVY_3_0_X updated: GROOVY-10497: `@NamedVariant`: don't replace explicit value with default

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

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


The following commit(s) were added to refs/heads/GROOVY_3_0_X by this push:
     new 80a57ea  GROOVY-10497: `@NamedVariant`: don't replace explicit value with default
80a57ea is described below

commit 80a57ea077f70a0207c2055bb253aa7e0cfd9bca
Author: Eric Milles <er...@thomsonreuters.com>
AuthorDate: Wed Feb 16 11:24:47 2022 -0600

    GROOVY-10497: `@NamedVariant`: don't replace explicit value with default
    
    Conflicts:
    	src/main/java/org/codehaus/groovy/transform/NamedVariantASTTransformation.java
---
 .../groovy/transform/NamedVariantASTTransformation.java |  8 +++++++-
 .../groovy/transform/NamedVariantTransformTest.groovy   | 17 ++++++++++++++++-
 2 files changed, 23 insertions(+), 2 deletions(-)

diff --git a/src/main/java/org/codehaus/groovy/transform/NamedVariantASTTransformation.java b/src/main/java/org/codehaus/groovy/transform/NamedVariantASTTransformation.java
index 28e0cbc..385102c 100644
--- a/src/main/java/org/codehaus/groovy/transform/NamedVariantASTTransformation.java
+++ b/src/main/java/org/codehaus/groovy/transform/NamedVariantASTTransformation.java
@@ -71,10 +71,12 @@ import static org.codehaus.groovy.ast.tools.GeneralUtils.getAllProperties;
 import static org.codehaus.groovy.ast.tools.GeneralUtils.list2args;
 import static org.codehaus.groovy.ast.tools.GeneralUtils.mapX;
 import static org.codehaus.groovy.ast.tools.GeneralUtils.nullX;
+import static org.codehaus.groovy.ast.tools.GeneralUtils.notNullX;
 import static org.codehaus.groovy.ast.tools.GeneralUtils.param;
 import static org.codehaus.groovy.ast.tools.GeneralUtils.plusX;
 import static org.codehaus.groovy.ast.tools.GeneralUtils.propX;
 import static org.codehaus.groovy.ast.tools.GeneralUtils.stmt;
+import static org.codehaus.groovy.ast.tools.GeneralUtils.ternaryX;
 import static org.codehaus.groovy.ast.tools.GeneralUtils.varX;
 
 @GroovyASTTransformation(phase = CompilePhase.SEMANTIC_ANALYSIS)
@@ -281,7 +283,10 @@ public class NamedVariantASTTransformation extends AbstractASTTransformation {
             defaultValue = defaultValueX(type);
         }
         if (defaultValue != null) {
-            value = elvisX(value, defaultValue);
+            if (isPrimitiveType(type)) { // handle null for primitive
+                value = ternaryX(notNullX(value), value, defaultValueX(type));
+            }
+            value = ternaryX(containsKey(mapParam, name), value, defaultValue);
         }
         return asType(value, type, coerce);
     }
@@ -292,6 +297,7 @@ public class NamedVariantASTTransformation extends AbstractASTTransformation {
 
     private static Expression containsKey(final Parameter mapParam, final String name) {
         MethodCallExpression call = callX(varX(mapParam), "containsKey", constX(name));
+        call.setImplicitThis(false); // required for use before super ctor call
         call.setMethodTarget(MAP_TYPE.getMethods("containsKey").get(0));
         return call;
     }
diff --git a/src/test/org/codehaus/groovy/transform/NamedVariantTransformTest.groovy b/src/test/org/codehaus/groovy/transform/NamedVariantTransformTest.groovy
index 2ec9135..0c882fd 100644
--- a/src/test/org/codehaus/groovy/transform/NamedVariantTransformTest.groovy
+++ b/src/test/org/codehaus/groovy/transform/NamedVariantTransformTest.groovy
@@ -192,7 +192,7 @@ final class NamedVariantTransformTest {
         '''
     }
 
-    @Test // GROOVY-9158
+    @Test // GROOVY-9158, GROOVY-10497
     void testNamedParamWithDefaultArgument() {
         assertScript '''
             import groovy.transform.*
@@ -226,6 +226,21 @@ final class NamedVariantTransformTest {
                 m()
             }
         '''
+
+        assertScript '''
+            import groovy.transform.*
+
+            @NamedVariant
+            def m(int one, int two = 42) {
+                "$one $two"
+            }
+
+            String result = m(one:0, two:0)
+            assert result == '0 0'
+
+            result = m(one:0, two:null)
+            assert result == '0 0'
+        '''
     }
 
     @Test // GROOVY-10176