Package asteroid
Class Expressions
- java.lang.Object
-
- asteroid.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 invokingA.EXPR:A.EXPR.propX(owner, property) A.EXPR.listX(expression1, expression2) // ...etc- Since:
- 0.1.0
-
-
Constructor Summary
Constructors Constructor Description Expressions()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static BinaryExpressionbinEqX(Expression leftExpr, Expression rightExpr)static BinaryExpressionbinX(String leftVarName, int operator, String rightVarName)Builds a newBinaryExpressionwith the names of the variables pointing at both left/right terms in the binary expression plus the operator in between.static BinaryExpressionbinX(Expression leftExpr, int operator, Expression rightExpr)Builds a newBinaryExpressionfrom a left expression and a right expression joined by a given operator.BooleanExpressionboolEqualsNullX(Expression argExpr)BooleanExpressionboolHasSameFieldX(FieldNode fNode, Expression other)BooleanExpressionboolHasSamePropertyX(PropertyNode pNode, Expression other)BooleanExpressionboolIsInstanceOfX(Expression objectExpression, Class cNode)Use it to create an "instanceof" expression to know whether an instance is of a given type or not.BooleanExpressionboolIsInstanceOfX(Expression objectExpression, ClassNode cNode)Use it to create an "instanceof" expression to know whether an instance is of a given type or not.BooleanExpressionboolIsOneX(Expression expr)BooleanExpressionboolIsTrueX(Expression argExpr)BooleanExpressionboolIsZeroX(Expression expr)BooleanExpressionboolNotNullX(Expression argExpr)BooleanExpressionboolSameX(Expression self, Expression other)static BooleanExpressionboolX(String leftVarName, int tokenType, String rightVarName)When creatingBooleanExpressionin places such asIfStatementyou can use this method to create aBooleanExpressionout of a binary expression using the names of the compared variables.static BooleanExpressionboolX(Expression expr)Builds an instance ofBooleanExpressionfrom aExpressionpassed as argument.static BooleanExpressionboolX(Expression leftExpr, int tokenType, Expression rightExpr)When creatingBooleanExpressionin places such asIfStatementyou can use this method to create aBooleanExpressionout of a binary expression using constant expressions ASTstatic MethodCallExpressioncallSuperX(String methodName)Creates a method call expression
ASTstatic MethodCallExpressioncallSuperX(String methodName, Expression... args)Creates a method call expression
ASTstatic MethodCallExpressioncallThisX(String methodName, Expression... args)Creates a method call expression using `this` as target instance
ASTstatic MethodCallExpressioncallX(Expression receiver, String methodName, Expression... args)Creates a method call expression
ASTstatic ClassExpressionclassX(Class clazz)static ClassExpressionclassX(ClassNode classNode)static ClosureExpressionclosureX(Statement stmt, Parameter... params)Creates a closure expression.static ConstantExpressionconstX(Object constant)static ConstantExpressionconstX(Object constant, Boolean keepPrimitive)Builds an instance ofConstantExpressionfrom the constant value passed as argument, and it keeps the type as primitive if it has to.static FieldExpressionfieldX(FieldNode fieldNode)Creates a method call expression
ASTstatic BooleanExpressionlandX(Expression leftExpr, Expression rightExpr)Builds a binary expression using a logical and (&&) ASTstatic ListExpressionlistX(Expression... expressions)static BooleanExpressionlorX(Expression leftExpr, Expression rightExpr)Builds a binary expression using a logical or (||) ASTstatic MapEntryExpressionmapEntryX(String key, Expression value)Creates an instance of typeMapEntryExpressionwith the key of typeStringstatic MapEntryExpressionmapEntryX(Expression key, Expression value)Creates an instance of typeMapEntryExpressionstatic MapExpressionmapX(List<MapEntryExpression> expressions)static MapExpressionmapX(MapEntryExpression... expressions)static ConstructorCallExpressionnewX(Class type)ASTstatic ConstructorCallExpressionnewX(Class type, Expression... expressions)ASTstatic NotExpressionnotX(Expression expression)Negates a given expression ASTstatic PropertyExpressionpropX(Expression owner, Expression property)static MethodCallExpressionsafeCallX(Expression receiver, String methodName, Expression... args)Creates a safe method call expression.static StaticMethodCallExpressionstaticCallX(Class clazz, String methodName, Expression... args)Creates a static method expression
ASTstatic StaticMethodCallExpressionstaticCallX(ClassNode clazz, String methodName, Expression... args)Creates a static method expression
ASTstatic DeclarationExpressionvarDeclarationX(String varName, Class type, Expression defaultValue)Creates a variable definition expression.static DeclarationExpressionvarDeclarationX(String varName, ClassNode type, Expression defaultValue)Creates a variable definition expression.static VariableExpressionvarX(String varName)Creates a reference to a given variable Creates a method call expression
ASTstatic VariableExpressionvarX(String varName, ClassNode type)Creates a reference to a given variable Creates a method call expression
AST
-
-
-
Method Detail
-
boolX
public static BooleanExpression boolX(Expression expr)
Builds an instance ofBooleanExpressionfrom aExpressionpassed as argument.
AST
ResultboolX(expr); // imagine expression is 1 == 11 == 1 // as a boolean expression- Parameters:
expr- the expression we want to evaluate as true or false- Returns:
- an instance of
BooleanExpression - Since:
- 0.1.5
- See Also:
binEqX(org.codehaus.groovy.ast.expr.Expression, org.codehaus.groovy.ast.expr.Expression),Types.COMPARE_EQUAL
-
boolEqualsNullX
public BooleanExpression boolEqualsNullX(Expression argExpr)
Builds an instance ofBooleanExpressionof type:
AST
ResultboolEqualsNullX(constX("some"));"some" == null // as a boolean expression- Parameters:
argExpr- the operand we want to check- Returns:
- an instance of
BooleanExpression - Since:
- 0.1.5
- See Also:
Types.COMPARE_EQUAL
-
boolHasSameFieldX
public BooleanExpression boolHasSameFieldX(FieldNode fNode, Expression other)
Builds an instance ofBooleanExpressionof type:
AST
ResultboolHasSameFieldX(personNameFieldNode, anotherPersonRef);name is (peter.name) // as a boolean expression- Parameters:
fNode- the node we would like to checkother- 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 ofBooleanExpressionof type:
AST
ResultboolHasSamePropertyX(personNamePropertyNode, anotherPersonRef);name is (peter.name) // as a boolean expression- Parameters:
pNode- the node we would like to checkother- 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 ofBooleanExpressionof type:
AST
ResultboolIsInstanceOfX(stringExpression, stringClassNode);"hello" instanceof String // as a boolean expression- Parameters:
objectExpression- expression to evaluatecNode- aClassNode- 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 ofBooleanExpressionof type:
AST
ResultboolIsInstanceOfX(stringExpression, stringClass)
WARNING!: Bear in mind that a given class might not be available when using it in an AST transformation."hello" instanceof String // as a boolean expression- Parameters:
objectExpression- expression to evaluatecNode- aClass. This- Returns:
- an instance of
BooleanExpression - Since:
- 0.1.5
-
boolIsOneX
public BooleanExpression boolIsOneX(Expression expr)
Builds an instance ofBooleanExpressionof type:
AST
ResultboolIsOneX(numberExpression);
Utility class to check a given expression equals to the constant expression 1.10 == 1 // as a boolean expression- 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 ofBooleanExpressionof type:
AST
ResultboolIsTrueX(anyExpression);
Utility class to check a given expression equals to true"something" == Boolean.TRUE // as a boolean expression or... (1 == 1) == Boolean.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 ofBooleanExpressionof type:
AST
ResultboolIsZeroX(numberExpression);
Utility class to check a given expression equals to the constant expression 0.10 == 0 // as a boolean expression- Parameters:
expr- expression to check against 0- Returns:
- an instance of
BooleanExpression - Since:
- 0.1.5
- See Also:
Types.COMPARE_EQUAL
-
boolNotNullX
public BooleanExpression boolNotNullX(Expression argExpr)
Builds an instance ofBooleanExpressionof type:
AST
ResultboolEqualsNotNullX(constX("some"));"some" != null // as a boolean expression- Parameters:
argExpr- the expression we want to check- 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 ofBooleanExpressionof type:
AST
ResultboolSameX(self, another)
Use it to create expressions asking whether a given instance is the same as another (like the == operator used in Java).thisInstance is otherInstance- Parameters:
self- the expression we want to check againstother- another expression- Returns:
- an instance of
BooleanExpressionanswering whether a given instance is the same as another. - Since:
- 0.1.5
- See Also:
Types
-
binEqX
public static BinaryExpression binEqX(Expression leftExpr, Expression rightExpr)
- Parameters:
leftExpr- left hand side operandrightExpr- right hand side operand- Returns:
- an instance of
BinaryExpressionrepresenting an equals expression - Since:
- 0.1.5
- See Also:
Types.COMPARE_EQUAL
-
constX
public static ConstantExpression constX(Object constant)
Builds an instance ofConstantExpressionfrom the constant value passed as argument
AST
ResultcallThisX("println", constX("1") // constant value "1" )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 ofConstantExpressionfrom the constant value passed as argument, and it keeps the type as primitive if it has to.
AST
ResultcallThisX("println", constX(1, true) // constant value 1 of type primitive int )println 1 // print constant primitive type 1- Parameters:
constant- the constant value- Returns:
- an instance of
ConstantExpression - Since:
- 0.4.3
-
propX
public static PropertyExpression propX(Expression owner, Expression property)
- Parameters:
owner- the instance the property belongs toproperty-- Returns:
- an instance of
PropertyExpression - Since:
- 0.1.0
-
listX
public static ListExpression listX(Expression... expressions)
- Parameters:
expressions- list items as expressions- Returns:
- an instance of
ListExpression - Since:
- 0.1.0
-
mapX
public static MapExpression mapX(MapEntryExpression... expressions)
Creates an instance ofMapExpression:
AST
ResultmapX(mapEntryX(constX('key'), constX('value')))[key: 'value']- Parameters:
expressions- different map entries asMapEntryExpression- Returns:
- an instance of
MapExpression - Since:
- 0.2.8
-
mapX
public static MapExpression mapX(List<MapEntryExpression> expressions)
Creates an instance ofMapExpression:
AST
ResultmapX(mapEntryX(constX('key'), constX('value')))[key: 'value']- Parameters:
expressions- different map entries asMapEntryExpression- Returns:
- an instance of
MapExpression - Since:
- 0.2.8
-
mapEntryX
public static MapEntryExpression mapEntryX(Expression key, Expression value)
Creates an instance of typeMapEntryExpression- Parameters:
key- anExpressionas the map entry keyvalue- anExpressionas the map entry value- Returns:
- an instance of
MapEntryExpression - Since:
- 0.2.8
-
mapEntryX
public static MapEntryExpression mapEntryX(String key, Expression value)
Creates an instance of typeMapEntryExpressionwith the key of typeString- Parameters:
key- anStringas the map entry keyvalue- anExpressionas the map entry value- Returns:
- an instance of
MapEntryExpression - Since:
- 0.4.3
-
classX
public static ClassExpression classX(Class clazz)
- Parameters:
clazz-- Returns:
- an instance of
ClassExpression - Since:
- 0.1.0
-
classX
public static ClassExpression classX(ClassNode classNode)
Creates a class expression from aClassNode
AST
ResultclassX(A.NODES.clazz(String.class))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
ResultA.NODES.callThisX("println", A.EXPR.constX("hello"))println "hello"- Parameters:
methodName- The name of the method to invokeargs- 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
ResultA.NODES.callX(ownerExpression, "toMD5", A.EXPR.constX("hello"))owner.toMD5("hello")- Parameters:
receiver- target instance expressionmethodName- The name of the method to invokeargs- 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
ResultA.NODES.safeCallX(ownerExpression, "toMD5", A.EXPR.constX("hello"))owner?.toMD5("hello")- Parameters:
receiver- target instance expressionmethodName- The name of the method to invokeargs- 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
ResultA.NODES.staticCallX(systemClassNode, "currentTimeMillis")System.currentTimeMillis()- Parameters:
clazz- theClassNodethe method belongsmethodName- The name of the method to invokeargs- 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
ResultA.NODES.staticCallX(systemClass, "currentTimeMillis")System.currentTimeMillis()- Parameters:
clazz- theClassthe method belongsmethodName- The name of the method to invokeargs- 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
ASTResultclass A { Integer age } //... FieldExpression node = A.NODES.fieldX(ageNode) A.EXPR.callX(node, "toString")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
ASTResultclass A { public void doSomething() { super.doSomething() } } //... MethodCallExpression = A.NODES.callSuperX("doSomething")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
ASTResultclass A { public void doSomething(String constant) { super.doSomething("1") } } //... MethodCallExpression = A.NODES.callSuperX("doSomething", constX("1"))super.doSomething("1")- Parameters:
methodName- name of the method we want to callargs- 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
ASTResultclass A { public void doSomething(String constant) { super.doSomething(constant) } } //... VariableExpression = A.NODES.varX("constant")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
ASTResultclass A { public void doSomething(String constant) { super.doSomething(constant) } } //... VariableExpression = A.NODES.varX("constant", A.NODES.clazz(String))
Creates a reference to a given variableconstant- 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
ResultclosureX(returnS(constantX("hello"))) // without params closureX(statement, param("n",Integer)) // with a param{ -> "hello" } // without params { Integer n -> n + 1 } // n + 1 is the statement- Parameters:
stmt- the body of the closureparams- the closure parameters- Returns:
- an instance of
ClosureExpression - Since:
- 0.1.5
-
boolX
public static BooleanExpression boolX(String leftVarName, int tokenType, String rightVarName)
When creatingBooleanExpressionin places such asIfStatementyou can use this method to create aBooleanExpressionout of a binary expression using the names of the compared variables. AST
Result//...after declaring the variables somewhere boolX('johnAge', Types.COMPARE_GREATER_THAN, 'peterAge')johnAge > peterAge- Parameters:
leftVarName- name of the variable referenced in the left side of the binary expressiontokenType- type of the comparison operator. Use any of the listed inTypesrightVarName- name of the variable referenced in the right side of the binary expression- Returns:
- a boolean expression as a
BooleanExpressioninstance - Since:
- 0.2.4
-
boolX
public static BooleanExpression boolX(Expression leftExpr, int tokenType, Expression rightExpr)
When creatingBooleanExpressionin places such asIfStatementyou can use this method to create aBooleanExpressionout of a binary expression using constant expressions AST
Result//...after declaring the variables somewhere boolX(constX(4), Types.COMPARE_GREATER_THAN, 2)4 > 2- Parameters:
leftExpr- left expressiontokenType- type of the comparison operator. Use any of the listed inTypesrightExpr- right expression- Returns:
- a boolean expression as a
BooleanExpressioninstance - Since:
- 0.2.4
-
binX
public static BinaryExpression binX(String leftVarName, int operator, String rightVarName)
Builds a newBinaryExpressionwith 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 theTypestype AST
Result//...after declaring the variables somewhere boolX('johnAge', Types.PLUS, 'peterAge')johnAge + peterAge- Parameters:
leftVarName- name of the variable referenced in the left side of the binary expressionoperator- type of the operator. Use any of the listed inTypesrightVarName- name of the variable referenced in the right side of the binary expression- Returns:
- a binary expression as a
BinaryExpressioninstance - Since:
- 0.2.4
-
binX
public static BinaryExpression binX(Expression leftExpr, int operator, Expression rightExpr)
Builds a newBinaryExpressionfrom a left expression and a right expression joined by a given operator. The operator can be anyone found in theTypestype AST
Result//...after declaring the variables somewhere binX('johnAge', Types.PLUS, 'peterAge')johnAge + peterAge- Parameters:
leftExpr- left term in the binary expression side of the binary expressionoperator- type of the comparison operator. Use any of the listed inTypesrightExpr- right term in the binary expression- Returns:
- a binary expression as a
BinaryExpressioninstance - 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
Result//...after declaring the variables somewhere varDeclarationX('johnAge', A.NODES.clazz(Integer).build(), A.EXPR.constX(23))Integer johnAge = 23- Parameters:
varName- name of the declared variabletype- type of the declared variable as aClassNodeinstancedefaultValue- 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
Result//...after declaring the variables somewhere varDeclarationX('johnAge', Integer, A.EXPR.constX(23))Integer johnAge = 23- Parameters:
varName- name of the declared variabletype- type of the declared variable as aClassinstancedefaultValue- expression setting the default value of the declared variable- Returns:
- an instance of a
DeclarationExpression - Since:
- 0.2.4
-
newX
public static ConstructorCallExpression newX(Class type)
AST
ResultnewX(ArrayList)new ArrayList()- Parameters:
type- type of the target object- Returns:
- an instance of
ConstructorCallExpression - Since:
- 0.2.4
-
newX
public static ConstructorCallExpression newX(Class type, Expression... expressions)
AST
ResultnewX(File, varX('path'), constX('myfile.txt'))new File(path, 'myfile.txt')- Parameters:
type- type of the target objectexpressions- a varargs of typeExpression- Returns:
- an instance of
ConstructorCallExpression - Since:
- 0.2.4
-
notX
public static NotExpression notX(Expression expression)
Negates a given expression AST
ResultnotX(varX('myVariable'))!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
ResultlorX(varX('a'), varX('b'))a || b- Parameters:
leftExpr-rightExpr-- Returns:
- a
BooleanExpressionrepresenting 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
ResultlandX(varX('a'), varX('b'))a && b- Parameters:
leftExpr-rightExpr-- Returns:
- a
BooleanExpressionrepresenting a logical AND (&&) - Since:
- 0.2.4
-
-