You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by su...@apache.org on 2021/07/31 12:09:25 UTC

[groovy] branch master updated: Tweak `GinqASTTransformation` to align with GINQ macro methods(`GQ` and `GQL`)

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

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


The following commit(s) were added to refs/heads/master by this push:
     new ee68af5  Tweak `GinqASTTransformation` to align with GINQ macro methods(`GQ` and `GQL`)
ee68af5 is described below

commit ee68af54f08ac1aa4ccc11ebdbbf83f08fc1ebe3
Author: Daniel Sun <su...@apache.org>
AuthorDate: Sat Jul 31 20:09:10 2021 +0800

    Tweak `GinqASTTransformation` to align with GINQ macro methods(`GQ` and `GQL`)
---
 .../groovy/ginq/transform/GinqASTTransformation.java   | 18 ++++++++++++++----
 1 file changed, 14 insertions(+), 4 deletions(-)

diff --git a/subprojects/groovy-ginq/src/main/groovy/org/apache/groovy/ginq/transform/GinqASTTransformation.java b/subprojects/groovy-ginq/src/main/groovy/org/apache/groovy/ginq/transform/GinqASTTransformation.java
index 819181d..1d3638c 100644
--- a/subprojects/groovy-ginq/src/main/groovy/org/apache/groovy/ginq/transform/GinqASTTransformation.java
+++ b/subprojects/groovy-ginq/src/main/groovy/org/apache/groovy/ginq/transform/GinqASTTransformation.java
@@ -45,8 +45,10 @@ import java.util.stream.Collectors;
 import static java.util.Collections.unmodifiableMap;
 import static org.apache.groovy.ginq.GinqGroovyMethods.CONF_LIST;
 import static org.apache.groovy.ginq.GinqGroovyMethods.transformGinqCode;
+import static org.codehaus.groovy.ast.ClassHelper.LIST_TYPE;
 import static org.codehaus.groovy.ast.ClassHelper.make;
 import static org.codehaus.groovy.ast.tools.GeneralUtils.block;
+import static org.codehaus.groovy.ast.tools.GeneralUtils.callX;
 import static org.codehaus.groovy.ast.tools.GeneralUtils.castX;
 import static org.codehaus.groovy.ast.tools.GeneralUtils.constX;
 import static org.codehaus.groovy.ast.tools.GeneralUtils.mapX;
@@ -83,10 +85,18 @@ public class GinqASTTransformation extends AbstractASTTransformation {
                 }
             }
 
-            CastExpression castExpression = castX(targetType, transformGinqCode(sourceUnit, ginqConfigurationMapExpression, origCode));
-            castExpression.setCoerce(true);
+            Expression resultExpression = transformGinqCode(sourceUnit, ginqConfigurationMapExpression, origCode);
+            if (DEFAULT_RESULT_TYPE.equals(targetType)) { // same as `GQ {...}`
+                // DO NOTHING
+            } else if (LIST_TYPE.equals(targetType)) { // same as `GQL {...}`
+                resultExpression = callX(resultExpression, "toList");
+            } else { // same as `GQ {...} as TargetType`
+                CastExpression castExpression = castX(targetType, resultExpression);
+                castExpression.setCoerce(true);
+                resultExpression = castExpression;
+            }
 
-            BlockStatement newCode = block(stmt(castExpression));
+            BlockStatement newCode = block(stmt(resultExpression));
             newCode.setSourcePosition(origCode);
             methodNode.setCode(newCode);
             VariableScopeVisitor variableScopeVisitor = new VariableScopeVisitor(sourceUnit);
@@ -115,7 +125,7 @@ public class GinqASTTransformation extends AbstractASTTransformation {
 
     private static final String VALUE = "value";
     private static final ClassNode GQ_CLASS_NODE = make(GQ.class);
-    private static final ClassNode DEFAULT_RESULT_TYPE = ClassHelper.make((Class<?>) getDefaultOptionValue(VALUE));
+    private static final ClassNode DEFAULT_RESULT_TYPE = ClassHelper.makeWithoutCaching((Class<?>) getDefaultOptionValue(VALUE));
     private static final Map<String, Expression> DEFAULT_OPTION_MAP = unmodifiableMap(CONF_LIST.stream().collect(Collectors.toMap(
             c -> c,
             c -> constX(getDefaultOptionValue(c))