You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by cc...@apache.org on 2015/10/07 21:26:49 UTC

[29/37] incubator-groovy git commit: ASTMatcher: Rework wildcard so that it only works on the pattern side

ASTMatcher: Rework wildcard so that it only works on the pattern side


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

Branch: refs/heads/master
Commit: cd39e26d6e648417f23beed441fc26c728b1f778
Parents: 1cd27ea
Author: Cedric Champeau <ce...@gmail.com>
Authored: Fri Oct 17 16:19:21 2014 +0200
Committer: Sergei Egorov <bs...@gmail.com>
Committed: Mon Sep 28 14:33:12 2015 +0300

----------------------------------------------------------------------
 .../codehaus/groovy/macro/matcher/ASTMatcher.groovy | 16 ++++++++--------
 .../groovy/macro/matcher/ASTMatcherTest.groovy      | 16 ++++++++++++++--
 2 files changed, 22 insertions(+), 10 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-groovy/blob/cd39e26d/subprojects/groovy-macro/src/main/groovy/org/codehaus/groovy/macro/matcher/ASTMatcher.groovy
----------------------------------------------------------------------
diff --git a/subprojects/groovy-macro/src/main/groovy/org/codehaus/groovy/macro/matcher/ASTMatcher.groovy b/subprojects/groovy-macro/src/main/groovy/org/codehaus/groovy/macro/matcher/ASTMatcher.groovy
index 858924b..5b5dc8e 100644
--- a/subprojects/groovy-macro/src/main/groovy/org/codehaus/groovy/macro/matcher/ASTMatcher.groovy
+++ b/subprojects/groovy-macro/src/main/groovy/org/codehaus/groovy/macro/matcher/ASTMatcher.groovy
@@ -20,7 +20,6 @@ import groovy.transform.TypeCheckingMode
 import org.codehaus.groovy.ast.*
 import org.codehaus.groovy.ast.expr.*
 import org.codehaus.groovy.ast.stmt.BlockStatement
-import org.codehaus.groovy.ast.stmt.EmptyStatement
 import org.codehaus.groovy.ast.stmt.ExpressionStatement
 import org.codehaus.groovy.ast.stmt.ForStatement
 import org.codehaus.groovy.ast.stmt.IfStatement
@@ -68,12 +67,13 @@ class ASTMatcher extends ClassCodeVisitorSupport {
         match = match && value
     }
 
-    private static boolean matchByName(String a, String b) {
-        return a.equals(b) || WILDCARD.equals(a) || WILDCARD.equals(b);
+    private static boolean matchByName(String potentialWildcard, String node) {
+        return node.equals(potentialWildcard) || WILDCARD.equals(potentialWildcard);
     }
 
     private static boolean isWildcardExpression(Object exp) {
-        return exp instanceof VariableExpression && WILDCARD.equals(exp.getName());
+        return (exp instanceof VariableExpression && WILDCARD.equals(exp.getName())
+            || (exp instanceof ConstantExpression && WILDCARD.equals(exp.getValue())));
     }
 
     /**
@@ -176,7 +176,7 @@ class ASTMatcher extends ClassCodeVisitorSupport {
                     }
                 }
             }
-            failIfNot(matchByName(cur.name,node.name))
+            failIfNot(matchByName(cur.name, node.name))
         }
     }
 
@@ -354,7 +354,7 @@ class ASTMatcher extends ClassCodeVisitorSupport {
         doWithNode(node, current) {
             visitAnnotations(node)
             def fieldNode = (FieldNode) current
-            failIfNot(matchByName(fieldNode.name,node.name))
+            failIfNot(matchByName(node.name, fieldNode.name))
             failIfNot(fieldNode.originType == node.originType)
             failIfNot(fieldNode.modifiers == node.modifiers)
 
@@ -571,7 +571,7 @@ class ASTMatcher extends ClassCodeVisitorSupport {
                     def n = nodeParams[i]
                     def c = curParams[i]
                     doWithNode(n, c) {
-                        failIfNot(matchByName(n.name,c.name) &&
+                        failIfNot(matchByName(n.name, c.name) &&
                                 (n.originType == c.originType) &&
                                 (n.type == c.originType))
                     }
@@ -762,7 +762,7 @@ class ASTMatcher extends ClassCodeVisitorSupport {
     public void visitVariableExpression(final VariableExpression expression) {
         doWithNode(expression, current) {
             def curVar = (VariableExpression) current
-            failIfNot(matchByName(expression.name,curVar.name) &&
+            failIfNot(matchByName(expression.name, curVar.name) &&
                     (expression.type == curVar.type) &&
                     (expression.originType == curVar.originType))
         }

http://git-wip-us.apache.org/repos/asf/incubator-groovy/blob/cd39e26d/subprojects/groovy-macro/src/test/groovy/org/codehaus/groovy/macro/matcher/ASTMatcherTest.groovy
----------------------------------------------------------------------
diff --git a/subprojects/groovy-macro/src/test/groovy/org/codehaus/groovy/macro/matcher/ASTMatcherTest.groovy b/subprojects/groovy-macro/src/test/groovy/org/codehaus/groovy/macro/matcher/ASTMatcherTest.groovy
index e1e88a3..63d4a9a 100644
--- a/subprojects/groovy-macro/src/test/groovy/org/codehaus/groovy/macro/matcher/ASTMatcherTest.groovy
+++ b/subprojects/groovy-macro/src/test/groovy/org/codehaus/groovy/macro/matcher/ASTMatcherTest.groovy
@@ -580,7 +580,8 @@ class ASTMatcherTest extends GroovyTestCase {
         def ast2 = macro { _ }
         def ast3 = macro { b }
         assert ASTMatcher.matches(ast1, ast2)
-        assert ASTMatcher.matches(ast2, ast3)
+        assert !ASTMatcher.matches(ast2, ast1)
+        assert !ASTMatcher.matches(ast2, ast3)
     }
 
     void testWildcardMatchVariableInBinaryExpression() {
@@ -591,6 +592,7 @@ class ASTMatcherTest extends GroovyTestCase {
         def ast5 = macro { a+_ }
         def ast6 = macro { _+b }
         assert ASTMatcher.matches(ast1, ast2)
+        assert !ASTMatcher.matches(ast2, ast1)
         assert !ASTMatcher.matches(ast1, ast3)
         assert !ASTMatcher.matches(ast1, ast4)
         assert ASTMatcher.matches(ast1, ast5)
@@ -601,7 +603,17 @@ class ASTMatcherTest extends GroovyTestCase {
         def ast1 = macro { a+foo(b) }
         def ast2 = macro { _+foo(b) }
         def ast3 = macro { a+_ }
-        //assert ASTMatcher.matches(ast1, ast2)
+        assert ASTMatcher.matches(ast1, ast2)
+        assert !ASTMatcher.matches(ast2, ast1)
         assert ASTMatcher.matches(ast1, ast3)
     }
+
+    void testWildcardInMethodName() {
+        def ast1 = macro { a+foo(b) }
+        def ast2 = macro { a+_(b) }
+        def ast3 = macro { a+_(c) }
+        assert ASTMatcher.matches(ast1, ast2)
+        assert !ASTMatcher.matches(ast2, ast1)
+        assert !ASTMatcher.matches(ast1, ast3)
+    }
 }