Package asteroid

Class Expressions


  • public final class Expressions
    extends Object
    This class hides the different implementations to create expressions through the Groovy api to provide a unified an easier way to create expressions when coding an AST transformation. Normally users should access this class invoking A.EXPR:
    
     A.EXPR.propX(owner, property)
     A.EXPR.listX(expression1, expression2)
     // ...etc
     
    Since:
    0.1.0
    • Constructor Detail

      • Expressions

        public Expressions()
    • Method Detail

      • boolHasSameFieldX

        public BooleanExpression boolHasSameFieldX​(FieldNode fNode,
                                                   Expression other)
        Builds an instance of BooleanExpression of type:

        AST
        
         boolHasSameFieldX(personNameFieldNode, anotherPersonRef);
         
        Result
        
         name is (peter.name) // as a boolean expression
         
        Parameters:
        fNode - the node we would like to check
        other - Another expression having as a result another field we would like to check against
        Returns:
        an instance of BooleanExpression
        Since:
        0.1.5
      • boolHasSamePropertyX

        public BooleanExpression boolHasSamePropertyX​(PropertyNode pNode,
                                                      Expression other)
        Builds an instance of BooleanExpression of type:

        AST
        
         boolHasSamePropertyX(personNamePropertyNode, anotherPersonRef);
         
        Result
        
         name is (peter.name) // as a boolean expression
         
        Parameters:
        pNode - the node we would like to check
        other - Another expression having as a result another property we would like to check against
        Returns:
        an instance of BooleanExpression
        Since:
        0.1.5
      • boolIsInstanceOfX

        public BooleanExpression boolIsInstanceOfX​(Expression objectExpression,
                                                   ClassNode cNode)
        Use it to create an "instanceof" expression to know whether an instance is of a given type or not. Builds an instance of BooleanExpression of type:

        AST
        
         boolIsInstanceOfX(stringExpression, stringClassNode);
         
        Result
        
         "hello" instanceof String // as a boolean expression
         
        Parameters:
        objectExpression - expression to evaluate
        cNode - a ClassNode
        Returns:
        an instance of BooleanExpression
        Since:
        0.1.5
      • boolIsInstanceOfX

        public BooleanExpression boolIsInstanceOfX​(Expression objectExpression,
                                                   Class cNode)
        Use it to create an "instanceof" expression to know whether an instance is of a given type or not. Builds an instance of BooleanExpression of type:

        AST
        boolIsInstanceOfX(stringExpression, stringClass)
        Result
        "hello" instanceof String // as a boolean expression
        WARNING!: Bear in mind that a given class might not be available when using it in an AST transformation.
        Parameters:
        objectExpression - expression to evaluate
        cNode - a Class. This
        Returns:
        an instance of BooleanExpression
        Since:
        0.1.5
      • boolIsOneX

        public BooleanExpression boolIsOneX​(Expression expr)
        Builds an instance of BooleanExpression of type:

        AST
        
         boolIsOneX(numberExpression);
         
        Result
        
         10 == 1 // as a boolean expression
         
        Utility class to check a given expression equals to the constant expression 1.
        Parameters:
        expr - expression to check against 1
        Returns:
        an instance of BooleanExpression
        Since:
        0.1.5
        See Also:
        Types.COMPARE_EQUAL
      • boolIsTrueX

        public BooleanExpression boolIsTrueX​(Expression argExpr)
        Builds an instance of BooleanExpression of type:

        AST
        
         boolIsTrueX(anyExpression);
         
        Result
        
         "something" == Boolean.TRUE // as a boolean expression or...
         (1 == 1) == Boolean.TRUE
         
        Utility class to check a given expression equals to true
        Parameters:
        argExpr - expression checked to be true
        Returns:
        an instance of BooleanExpression
        Since:
        0.1.5
      • boolIsZeroX

        public BooleanExpression boolIsZeroX​(Expression expr)
        Builds an instance of BooleanExpression of type:

        AST
        
         boolIsZeroX(numberExpression);
         
        Result
        
         10 == 0 // as a boolean expression
         
        Utility class to check a given expression equals to the constant expression 0.
        Parameters:
        expr - expression to check against 0
        Returns:
        an instance of BooleanExpression
        Since:
        0.1.5
        See Also:
        Types.COMPARE_EQUAL
      • boolSameX

        public BooleanExpression boolSameX​(Expression self,
                                           Expression other)
        Builds an instance of BooleanExpression of type:

        AST
        
         boolSameX(self, another)
         
        Result
        
         thisInstance is otherInstance
         
        Use it to create expressions asking whether a given instance is the same as another (like the == operator used in Java).
        Parameters:
        self - the expression we want to check against
        other - another expression
        Returns:
        an instance of BooleanExpression answering whether a given instance is the same as another.
        Since:
        0.1.5
        See Also:
        Types
      • constX

        public static ConstantExpression constX​(Object constant)
        Builds an instance of ConstantExpression from the constant value passed as argument

        AST
        
         callThisX("println",
             constX("1") // constant value "1"
         )
         
        Result
        println "1" // print constant value "1"
        Parameters:
        constant - the constant value
        Returns:
        an instance of ConstantExpression
        Since:
        0.1.0
      • constX

        public static ConstantExpression constX​(Object constant,
                                                Boolean keepPrimitive)
        Builds an instance of ConstantExpression from the constant value passed as argument, and it keeps the type as primitive if it has to.

        AST
        
         callThisX("println",
             constX(1, true) // constant value 1 of type primitive int
         )
         
        Result
        println 1 // print constant primitive type 1
        Parameters:
        constant - the constant value
        Returns:
        an instance of ConstantExpression
        Since:
        0.4.3
      • listX

        public static ListExpression listX​(Expression... expressions)
        Creates an instance of ListExpression:

        AST:
        listX(constX(1), constX(2))
        Result:
        [1,2]
        Parameters:
        expressions - list items as expressions
        Returns:
        an instance of ListExpression
        Since:
        0.1.0
      • classX

        public static ClassExpression classX​(ClassNode classNode)
        Creates a class expression from a ClassNode

        AST
        classX(A.NODES.clazz(String.class))
        Result
        String.class
        Parameters:
        classNode -
        Returns:
        an instance of ClassNode
        Since:
        0.1.0
      • callThisX

        public static MethodCallExpression callThisX​(String methodName,
                                                     Expression... args)
        Creates a method call expression using `this` as target instance

        AST
        A.NODES.callThisX("println", A.EXPR.constX("hello"))
        Result
        println "hello"
        Parameters:
        methodName - The name of the method to invoke
        args - with different argument expressions
        Returns:
        an instance of MethodCallExpression
        Since:
        0.1.0
      • callX

        public static MethodCallExpression callX​(Expression receiver,
                                                 String methodName,
                                                 Expression... args)
        Creates a method call expression

        AST
        A.NODES.callX(ownerExpression, "toMD5", A.EXPR.constX("hello"))
        Result
        owner.toMD5("hello")
        Parameters:
        receiver - target instance expression
        methodName - The name of the method to invoke
        args - with different argument expressions
        Returns:
        an instance of MethodCallExpression
        Since:
        0.1.0
      • safeCallX

        public static MethodCallExpression safeCallX​(Expression receiver,
                                                     String methodName,
                                                     Expression... args)
        Creates a safe method call expression. It will return null if the receiver object is null.

        AST
        A.NODES.safeCallX(ownerExpression, "toMD5", A.EXPR.constX("hello"))
        Result
        owner?.toMD5("hello")
        Parameters:
        receiver - target instance expression
        methodName - The name of the method to invoke
        args - with different argument expressions
        Returns:
        an instance of MethodCallExpression
        Since:
        0.1.3
      • staticCallX

        public static StaticMethodCallExpression staticCallX​(ClassNode clazz,
                                                             String methodName,
                                                             Expression... args)
        Creates a static method expression

        AST
        A.NODES.staticCallX(systemClassNode, "currentTimeMillis")
        Result
        System.currentTimeMillis()
        Parameters:
        clazz - the ClassNode the method belongs
        methodName - The name of the method to invoke
        args - with different argument expressions
        Returns:
        an instance of StaticMethodCallExpression
        Since:
        0.1.0
      • staticCallX

        public static StaticMethodCallExpression staticCallX​(Class clazz,
                                                             String methodName,
                                                             Expression... args)
        Creates a static method expression

        AST
        A.NODES.staticCallX(systemClass, "currentTimeMillis")
        Result
        System.currentTimeMillis()
        Parameters:
        clazz - the Class the method belongs
        methodName - The name of the method to invoke
        args - with different argument expressions
        Returns:
        an instance of StaticMethodCallExpression
        Since:
        0.1.0
      • fieldX

        public static FieldExpression fieldX​(FieldNode fieldNode)
        Creates a method call expression

        AST
             
                 class A {
                     Integer age
                 }
                 //...
                 FieldExpression node = A.NODES.fieldX(ageNode)
                 A.EXPR.callX(node, "toString")
             
         
        Result
        age.toString()
        Parameters:
        fieldNode - the node pointing at the field
        Returns:
        an instance of FieldExpression
        Since:
        0.1.0
      • callSuperX

        public static MethodCallExpression callSuperX​(String methodName)
        Creates a method call expression

        AST
             
                 class A {
                     public void doSomething() {
                         super.doSomething()
                     }
                 }
        
                 //...
                 MethodCallExpression = A.NODES.callSuperX("doSomething")
        
             
         
        Result
        super.doSomething()
        Parameters:
        methodName - name of the method we want to call
        Returns:
        an instance of MethodCallExpression
        Since:
        0.1.0
      • callSuperX

        public static MethodCallExpression callSuperX​(String methodName,
                                                      Expression... args)
        Creates a method call expression

        AST
             
                 class A {
                     public void doSomething(String constant) {
                         super.doSomething("1")
                     }
                 }
        
                 //...
                 MethodCallExpression = A.NODES.callSuperX("doSomething", constX("1"))
        
             
         
        Result
        super.doSomething("1")
        Parameters:
        methodName - name of the method we want to call
        args - expression representing different arguments to method call expression
        Returns:
        an instance of MethodCallExpression
        Since:
        0.1.0
      • varX

        public static VariableExpression varX​(String varName)
        Creates a reference to a given variable Creates a method call expression

        AST
             
                 class A {
                     public void doSomething(String constant) {
                         super.doSomething(constant)
                     }
                 }
        
                 //...
                 VariableExpression = A.NODES.varX("constant")
        
             
         
        Result
        constant
        Parameters:
        varName -
        Returns:
        an instance of VariableExpression
        Since:
        0.1.0
      • varX

        public static VariableExpression varX​(String varName,
                                              ClassNode type)
        Creates a reference to a given variable Creates a method call expression

        AST
             
                 class A {
                     public void doSomething(String constant) {
                         super.doSomething(constant)
                     }
                 }
        
                 //...
                 VariableExpression = A.NODES.varX("constant", A.NODES.clazz(String))
        
             
         
        Result
        constant
        Creates a reference to a given variable
        Parameters:
        varName -
        type -
        Returns:
        an instance of VariableExpression
        Since:
        0.1.0
      • closureX

        public static ClosureExpression closureX​(Statement stmt,
                                                 Parameter... params)
        Creates a closure expression. The statement passed as first parameter becomes the closure's body.

        AST
        
         closureX(returnS(constantX("hello"))) // without params
         closureX(statement, param("n",Integer)) // with a param
         
        Result
        
         { -> "hello" } // without params
         { Integer n -> n + 1 } // n + 1 is the statement
         
        Parameters:
        stmt - the body of the closure
        params - the closure parameters
        Returns:
        an instance of ClosureExpression
        Since:
        0.1.5
      • boolX

        public static BooleanExpression boolX​(String leftVarName,
                                              int tokenType,
                                              String rightVarName)
        When creating BooleanExpression in places such as IfStatement you can use this method to create a BooleanExpression out of a binary expression using the names of the compared variables. AST
        
         //...after declaring the variables somewhere
         boolX('johnAge', Types.COMPARE_GREATER_THAN, 'peterAge')
         
        Result
        
         johnAge > peterAge
         
        Parameters:
        leftVarName - name of the variable referenced in the left side of the binary expression
        tokenType - type of the comparison operator. Use any of the listed in Types
        rightVarName - name of the variable referenced in the right side of the binary expression
        Returns:
        a boolean expression as a BooleanExpression instance
        Since:
        0.2.4
      • boolX

        public static BooleanExpression boolX​(Expression leftExpr,
                                              int tokenType,
                                              Expression rightExpr)
        When creating BooleanExpression in places such as IfStatement you can use this method to create a BooleanExpression out of a binary expression using constant expressions AST
        
         //...after declaring the variables somewhere
         boolX(constX(4), Types.COMPARE_GREATER_THAN, 2)
         
        Result
        
         4 > 2
         
        Parameters:
        leftExpr - left expression
        tokenType - type of the comparison operator. Use any of the listed in Types
        rightExpr - right expression
        Returns:
        a boolean expression as a BooleanExpression instance
        Since:
        0.2.4
      • binX

        public static BinaryExpression binX​(String leftVarName,
                                            int operator,
                                            String rightVarName)
        Builds a new BinaryExpression with the names of the variables pointing at both left/right terms in the binary expression plus the operator in between. The operator can be anyone found in the Types type AST
        
         //...after declaring the variables somewhere
         boolX('johnAge', Types.PLUS, 'peterAge')
         
        Result
        
         johnAge + peterAge
         
        Parameters:
        leftVarName - name of the variable referenced in the left side of the binary expression
        operator - type of the operator. Use any of the listed in Types
        rightVarName - name of the variable referenced in the right side of the binary expression
        Returns:
        a binary expression as a BinaryExpression instance
        Since:
        0.2.4
      • binX

        public static BinaryExpression binX​(Expression leftExpr,
                                            int operator,
                                            Expression rightExpr)
        Builds a new BinaryExpression from a left expression and a right expression joined by a given operator. The operator can be anyone found in the Types type AST
        
         //...after declaring the variables somewhere
         binX('johnAge', Types.PLUS, 'peterAge')
         
        Result
        
         johnAge + peterAge
         
        Parameters:
        leftExpr - left term in the binary expression side of the binary expression
        operator - type of the comparison operator. Use any of the listed in Types
        rightExpr - right term in the binary expression
        Returns:
        a binary expression as a BinaryExpression instance
        Since:
        0.2.4
      • varDeclarationX

        public static DeclarationExpression varDeclarationX​(String varName,
                                                            ClassNode type,
                                                            Expression defaultValue)
        Creates a variable definition expression. Where, in the code a variable is defined. If could be also initialized. AST
        
         //...after declaring the variables somewhere
         varDeclarationX('johnAge',
                          A.NODES.clazz(Integer).build(),
                          A.EXPR.constX(23))
         
        Result
        
         Integer johnAge = 23
         
        Parameters:
        varName - name of the declared variable
        type - type of the declared variable as a ClassNode instance
        defaultValue - expression setting the default value of the declared variable
        Returns:
        an instance of a DeclarationExpression
        Since:
        0.2.4
      • varDeclarationX

        public static DeclarationExpression varDeclarationX​(String varName,
                                                            Class type,
                                                            Expression defaultValue)
        Creates a variable definition expression. Where, in the code a variable is defined. If could be also initialized. AST
        
         //...after declaring the variables somewhere
         varDeclarationX('johnAge', Integer, A.EXPR.constX(23))
         
        Result
        
         Integer johnAge = 23
         
        Parameters:
        varName - name of the declared variable
        type - type of the declared variable as a Class instance
        defaultValue - expression setting the default value of the declared variable
        Returns:
        an instance of a DeclarationExpression
        Since:
        0.2.4
      • notX

        public static NotExpression notX​(Expression expression)
        Negates a given expression AST
        
         notX(varX('myVariable'))
         
        Result
        
         !myVariable
         
        Parameters:
        expression - the expression to be negated
        Returns:
        the expression passed as parameter negated
        Since:
        0.2.4
      • lorX

        public static BooleanExpression lorX​(Expression leftExpr,
                                             Expression rightExpr)
        Builds a binary expression using a logical or (||) AST
        
         lorX(varX('a'), varX('b'))
         
        Result
        
         a || b
         
        Parameters:
        leftExpr -
        rightExpr -
        Returns:
        a BooleanExpression representing a logical OR (||)
        Since:
        0.2.4
      • landX

        public static BooleanExpression landX​(Expression leftExpr,
                                              Expression rightExpr)
        Builds a binary expression using a logical and (&&) AST
        
         landX(varX('a'), varX('b'))
         
        Result
        
         a && b
         
        Parameters:
        leftExpr -
        rightExpr -
        Returns:
        a BooleanExpression representing a logical AND (&&)
        Since:
        0.2.4