Menu

How to retrieve the value of ASTPrimarySuffix node

vidhi
2014-03-26
2014-03-30
  • vidhi

    vidhi - 2014-03-26

    While writing the rule I got stuck at a level where I'm unable to retrieve the value of ASTPrimarySuffix node. Can someone respond to this ASAP.

    This is how the tree looks like in PMD.

    Block
      BlockStatement
        Statement
         IfStatement
           Expression
             PrimaryExpression
               PrimaryPrefix
                 Name:ABC
               PrimarySuffix
                 Arguments
               PrimarySuffix:equals
    

    All three Primaryprefix PrimarySuffix and PrimaySuffix falls under PrimaryExpression at same level I want to retrieve the node primarySuffix whose value is equals .

    I tried retrieving the value with below line of code but its not working. I'm always getting null.
    ASTPrimarySuffix suffixValue = (ASTPrimarySuffix) primaryExpression.getFirstChildOfType(ASTPrimarySuffix.class);

     

    Last edit: Andreas Dangel 2014-03-30
  • vidhi

    vidhi - 2014-03-26

    attaching screenshot

     
  • Andreas Dangel

    Andreas Dangel - 2014-03-30

    I assume, you want the second ASTPrimarySuffix node, whose image happen to be equals. Is this correct?

    In your code example, you'll get the first suffix node, which is the wrong node. You would need to use the second node.

    In this case, you can call findChildrenOfType(ASTPrimarySuffix.class) which will return a list of all suffix nodes:

    List<ASTPrimarySuffix> suffixes = primaryExpression.findChildrenOfType(ASTPrimarySuffix.class);
    ASTPrimarySuffix suffixValue = suffixes.get(1);
    

    You could also iterate through the list, to search for the equals call:

    List<ASTPrimarySuffix> suffixes = primaryExpression.findChildrenOfType(ASTPrimarySuffix.class);
    ASTPrimarySuffix suffixValue = null;
    for (ASTPrimarySuffix s : suffixes) {
        if ("equals".equals(s.getImage()) {
            suffixValue = s;
            break;
        }
    }
    

    Hope this helps.

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.