Details
-
Bug
-
Status: Closed
-
Major
-
Resolution: Not A Problem
-
1.8.8
-
None
-
None
Description
Not sure that this is really a bug (maybe there are some good reasons to have this behaviour).
But it seems that, after building the AST of some groovy piece of code, it can happen that the returned AST contains node being a PropertyExpression while I would expect it to be a MethodCallExpression.
For example, as in Groovy we can omit parenthesis when performing a method call.
Those 2 piece of codes have the same result:
MyClass.getSomething parameter1 parameter2
MyClass.getSomething(parameter1,parameter2)
but they should also generate exactly the same AST, should not they ?
Here is the TestCase showing that it's not the case as it prints is:
PropertyExpression MethodCallExpression
class ASTTest extends GroovyTestCase { void test() { def nodes1 = new AstBuilder().buildFromString('MyClass.getSomething parameter1 parameter2') def nodes2 = new AstBuilder().buildFromString('MyClass.getSomething(parameter1,parameter2)') commonBrowseNodes(nodes1) commonBrowseNodes(nodes2) } void commonBrowseNodes(node) { def blockStatement = node[0] def returnStatement = blockStatement.statements[0] def expression = returnStatement.expression println expression.getClass().getSimpleName() } }
or to copy-paste in the Groovy Web Console:
import org.codehaus.groovy.ast.builder.AstBuilder; class ASTTest // extends GroovyTestCase { void test() { def nodes1 = new AstBuilder().buildFromString('MyClass.callMyMethod parameter1 parameter2') def nodes2 = new AstBuilder().buildFromString('MyClass.callMyMethod(parameter1,parameter2)') commonBrowseNodes(nodes1) commonBrowseNodes(nodes2) } void commonBrowseNodes(node) { def blockStatement = node[0] def returnStatement = blockStatement.statements[0] def expression = returnStatement.expression println expression.getClass().getSimpleName() } } new ASTTest().test()
This discrepancy could have some impacts for AST "parsers" which have some rules (for security or whatever) depending on PropertyExpression, MethodCallExpression, etc ...