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/25 10:15:08 UTC
[groovy] branch master updated: Tweak GINQ methods and add 1 more
test
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 12e23c8 Tweak GINQ methods and add 1 more test
12e23c8 is described below
commit 12e23c80b52e7e2c554556fbc71e1e8807f80ef9
Author: Daniel Sun <su...@apache.org>
AuthorDate: Sun Jul 25 18:14:50 2021 +0800
Tweak GINQ methods and add 1 more test
---
.../apache/groovy/ginq/GinqGroovyMethods.groovy | 2 -
.../ginq/transform/GinqASTTransformation.java | 46 ++++++++---------
.../test/org/apache/groovy/ginq/GinqTest.groovy | 58 ++++++++++++++++++++++
3 files changed, 79 insertions(+), 27 deletions(-)
diff --git a/subprojects/groovy-ginq/src/main/groovy/org/apache/groovy/ginq/GinqGroovyMethods.groovy b/subprojects/groovy-ginq/src/main/groovy/org/apache/groovy/ginq/GinqGroovyMethods.groovy
index 0e41754..4841fd2 100644
--- a/subprojects/groovy-ginq/src/main/groovy/org/apache/groovy/ginq/GinqGroovyMethods.groovy
+++ b/subprojects/groovy-ginq/src/main/groovy/org/apache/groovy/ginq/GinqGroovyMethods.groovy
@@ -103,9 +103,7 @@ class GinqGroovyMethods {
static Expression transformGinqCode(SourceUnit sourceUnit, MapExpression ginqConfigurationMapExpression, Statement code) {
GinqAstBuilder ginqAstBuilder = new GinqAstBuilder(sourceUnit)
-
AbstractGinqExpression ginqExpression = ginqAstBuilder.buildAST(code)
-
Map<String, String> configuration = createConfiguration(sourceUnit, ginqConfigurationMapExpression)
if (ginqExpression instanceof GinqExpression && TRUE_STR == configuration.get(CONF_OPTIMIZE, TRUE_STR)) {
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 ae332c1..cd93870 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
@@ -22,10 +22,10 @@ import groovy.ginq.transform.GQ;
import org.apache.groovy.ginq.GinqGroovyMethods;
import org.codehaus.groovy.GroovyBugError;
import org.codehaus.groovy.ast.ASTNode;
+import org.codehaus.groovy.ast.AnnotatedNode;
import org.codehaus.groovy.ast.AnnotationNode;
import org.codehaus.groovy.ast.ClassNode;
import org.codehaus.groovy.ast.MethodNode;
-import org.codehaus.groovy.ast.ModuleNode;
import org.codehaus.groovy.ast.expr.Expression;
import org.codehaus.groovy.ast.expr.MapEntryExpression;
import org.codehaus.groovy.ast.expr.MapExpression;
@@ -37,7 +37,6 @@ import org.codehaus.groovy.transform.AbstractASTTransformation;
import org.codehaus.groovy.transform.GroovyASTTransformation;
import java.util.HashMap;
-import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
@@ -58,30 +57,27 @@ public class GinqASTTransformation extends AbstractASTTransformation {
@Override
public void visit(ASTNode[] nodes, SourceUnit sourceUnit) {
- ModuleNode moduleNode = sourceUnit.getAST();
- List<ClassNode> classNodeList = moduleNode.getClasses();
- if (classNodeList == null) return;
+ init(nodes, sourceUnit);
+ AnnotatedNode parent = (AnnotatedNode) nodes[1];
+ AnnotationNode node = (AnnotationNode) nodes[0];
+ if (!GQ_CLASS_NODE.equals(node.getClassNode())) return;
- classNodeList.stream().flatMap(c -> c.getMethods().stream())
- .filter(m -> !m.getAnnotations(GQ_CLASS_NODE).isEmpty())
- .map(m -> {
- if (m.isAbstract()) {
- addError("Error during " + GQ_CLASS_NODE.getName() + " processing: annotation not allowed on abstract method '" + m.getName() + "'", m);
- return m.getDeclaringClass();
- }
- BlockStatement origCode = (BlockStatement) m.getCode();
- MapExpression ginqConfigurationMapExpression = makeGinqConfigurationMapExpression(m);
- BlockStatement newCode = block(
- returnS(transformGinqCode(sourceUnit, ginqConfigurationMapExpression, origCode))
- );
- newCode.setSourcePosition(origCode);
- m.setCode(newCode);
- return m.getDeclaringClass();
- }).distinct()
- .forEach(c -> {
- VariableScopeVisitor variableScopeVisitor = new VariableScopeVisitor(sourceUnit);
- variableScopeVisitor.visitClass(c);
- });
+ if (parent instanceof MethodNode) {
+ MethodNode methodNode = (MethodNode) parent;
+ if (methodNode.isAbstract()) {
+ addError("Error during " + GQ_CLASS_NODE.getName() + " processing: annotation not allowed on abstract method '" + methodNode.getName() + "'", methodNode);
+ return;
+ }
+ BlockStatement origCode = (BlockStatement) methodNode.getCode();
+ MapExpression ginqConfigurationMapExpression = makeGinqConfigurationMapExpression(methodNode);
+ BlockStatement newCode = block(
+ returnS(transformGinqCode(sourceUnit, ginqConfigurationMapExpression, origCode))
+ );
+ newCode.setSourcePosition(origCode);
+ methodNode.setCode(newCode);
+ VariableScopeVisitor variableScopeVisitor = new VariableScopeVisitor(sourceUnit);
+ variableScopeVisitor.visitClass(methodNode.getDeclaringClass());
+ }
}
private MapExpression makeGinqConfigurationMapExpression(MethodNode m) {
diff --git a/subprojects/groovy-ginq/src/spec/test/org/apache/groovy/ginq/GinqTest.groovy b/subprojects/groovy-ginq/src/spec/test/org/apache/groovy/ginq/GinqTest.groovy
index 1b0a3d0..acff398 100644
--- a/subprojects/groovy-ginq/src/spec/test/org/apache/groovy/ginq/GinqTest.groovy
+++ b/subprojects/groovy-ginq/src/spec/test/org/apache/groovy/ginq/GinqTest.groovy
@@ -6246,6 +6246,64 @@ class GinqTest {
'''
}
+ @Test
+ void "testGinqMethod - GQ - 9"() {
+ assertScript '''
+ import groovy.ginq.transform.GQ
+
+ @GQ
+ def ginq1(x) {
+ from n in [1, 2, 3]
+ where n < x
+ select n
+ }
+
+ @GQ
+ def ginq2(x) {
+ from n in [2, 3, 4]
+ where n > x
+ select n
+ }
+
+ assert [1] == ginq1(2).toList()
+ assert [3, 4] == ginq2(2).toList()
+ '''
+ }
+
+ @Test
+ void "testGinqMethod - GQ - 10"() {
+ assertScript '''
+ import groovy.ginq.transform.GQ
+
+ abstract class AbstractGinqClass {
+ abstract def ginq1(x)
+ abstract def ginq2(x)
+ }
+
+ class GinqClass extends AbstractGinqClass {
+ @Override
+ @GQ
+ def ginq1(x) {
+ from n in [1, 2, 3]
+ where n < x
+ select n
+ }
+
+ @Override
+ @GQ
+ def ginq2(x) {
+ from n in [2, 3, 4]
+ where n > x
+ select n
+ }
+ }
+
+ def gc = new GinqClass()
+ assert [1] == gc.ginq1(2).toList()
+ assert [3, 4] == gc.ginq2(2).toList()
+ '''
+ }
+
@AfterClass
static void "testGinq - shutdown - 0"() {
assertScript '''