Class StatementUtils


  • public final class StatementUtils
    extends Object
    General utility methods to deal with Statement instances
    Since:
    0.1.5
    See Also:
    Utils
    • Constructor Detail

      • StatementUtils

        public StatementUtils()
    • Method Detail

      • groupStatementsByLabel

        public List<StatementUtils.Group> groupStatementsByLabel​(BlockStatement blockStmt)
        Use this function to group expression statements found within a given block statement. Statements will be group in StatementUtils.Group instances.
        Parameters:
        blockStmt - the block statement where the statements we want to group are
        Returns:
        a list of StatementUtils.Group
        Since:
        0.1.5
      • extractLabelFrom

        public StatementUtils.Label extractLabelFrom​(Statement stmt)
        If you would like to take the text from a label in your code
        
          check: 'is greater than'
          //...
         
        Parameters:
        stmt - The statement we would like to take a label from
        Returns:
        an instance of StatementUtils.Label
      • applyToStatementsByLabelFlatten

        public List<Statement> applyToStatementsByLabelFlatten​(List<StatementUtils.Group> source,
                                                               Map<String,​Closure<Statement>> mappings)
        If you have a given list of StatementUtils.Group you can apply a set of transformations on certain statements within a given group. Lets say we have group all statements in a method and we would like to apply a transformation to statements in the group identified with 'check':
        
         ['check': { Group group, ExpressionStatement stmt -> stmt }]
         
        This particular transformation will do nothing, it only returns the same statement.
        Parameters:
        source - the list of groups we want to operate over
        mappings - a map. Keys are label names and values are closures transforming every statement of the identified group.
        Returns:
        all statements (transformed and not transformed) returned in order.
      • applyToStatementsByLabel

        public List<StatementUtils.Group> applyToStatementsByLabel​(List<StatementUtils.Group> source,
                                                                   Map<String,​Closure<Statement>> mappings)
        If you have a given list of StatementUtils.Group you can apply a set of transformations on certain statements within a given group. Lets say we have group all statements in a method and we would like to apply a transformation to statements in the group identified with 'check':
        
         ['check': { Group group, ExpressionStatement stmt -> stmt }]
         
        This particular transformation will do nothing, it only returns the same statement.
        Parameters:
        source - the list of groups we want to operate over
        mappings - a map. Keys are label names and values are closures transforming every statement of the identified group.
        Returns:
        all statements in their correspondent group.