Uploaded image for project: 'Groovy'
  1. Groovy
  2. GROOVY-5701

AST - Groovy method call interpreted as PropertyExpression instead of MethodCallExpression

    XMLWordPrintableJSON

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Major
    • Resolution: Not A Problem
    • Affects Version/s: 1.8.8
    • Fix Version/s: None
    • Component/s: ast builder
    • Labels:
      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
      
      ASTTest.groovy
      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 ...

        Attachments

        1. ASTTest.groovy
          0.6 kB
          Guillaume CERNIER

          Activity

            People

            • Assignee:
              melix C├ędric Champeau
              Reporter:
              guillaume.cernier Guillaume CERNIER
            • Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: