public final class Expressions
extends java.lang.Object
A.EXPR:
A.EXPR.propX(owner, property)
A.EXPR.listX(expression1, expression2)
// ...etc
| Constructor and Description |
|---|
Expressions() |
| Modifier and Type | Method and Description |
|---|---|
static BinaryExpression |
binEqX(Expression leftExpr,
Expression rightExpr)
|
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. |
static BinaryExpression |
binX(java.lang.String leftVarName,
int operator,
java.lang.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. |
BooleanExpression |
boolEqualsNullX(Expression argExpr)
|
BooleanExpression |
boolHasSameFieldX(FieldNode fNode,
Expression other)
|
BooleanExpression |
boolHasSamePropertyX(PropertyNode pNode,
Expression other)
|
BooleanExpression |
boolIsInstanceOfX(Expression objectExpression,
java.lang.Class cNode)
Use it to create an "instanceof" expression to know whether an
instance is of a given type or not.
|
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.
|
BooleanExpression |
boolIsOneX(Expression expr)
|
BooleanExpression |
boolIsTrueX(Expression argExpr)
|
BooleanExpression |
boolIsZeroX(Expression expr)
|
BooleanExpression |
boolNotNullX(Expression argExpr)
|
BooleanExpression |
boolSameX(Expression self,
Expression other)
|
static BooleanExpression |
boolX(Expression expr)
Builds an instance of
BooleanExpression from a Expression
passed as argument. |
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 |
static BooleanExpression |
boolX(java.lang.String leftVarName,
int tokenType,
java.lang.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. |
static MethodCallExpression |
callSuperX(java.lang.String methodName)
Creates a method call expression
AST |
static MethodCallExpression |
callSuperX(java.lang.String methodName,
Expression... args)
Creates a method call expression
AST |
static MethodCallExpression |
callThisX(java.lang.String methodName,
Expression... args)
Creates a method call expression using `this` as target instance
AST |
static MethodCallExpression |
callX(Expression receiver,
java.lang.String methodName,
Expression... args)
Creates a method call expression
AST |
static ClassExpression |
classX(java.lang.Class clazz)
|
static ClassExpression |
classX(ClassNode classNode)
|
static ClosureExpression |
closureX(Statement stmt,
Parameter... params)
Creates a closure expression.
|
static ConstantExpression |
constX(java.lang.Object constant)
|
static FieldExpression |
fieldX(FieldNode fieldNode)
Creates a method call expression
AST |
static BooleanExpression |
landX(Expression leftExpr,
Expression rightExpr)
Builds a binary expression using a logical and (&&)
AST
|
static ListExpression |
listX(Expression... expressions)
|
static BooleanExpression |
lorX(Expression leftExpr,
Expression rightExpr)
Builds a binary expression using a logical or (||)
AST
|
static MapEntryExpression |
mapEntryX(Expression key,
Expression value)
Creates an instance of type
MapEntryExpression |
static MapExpression |
mapX(java.util.List<MapEntryExpression> expressions)
|
static MapExpression |
mapX(MapEntryExpression... expressions)
|
static ConstructorCallExpression |
newX(java.lang.Class type)
AST
|
static ConstructorCallExpression |
newX(java.lang.Class type,
Expression... expressions)
AST
|
static NotExpression |
notX(Expression expression)
Negates a given expression
AST
|
static PropertyExpression |
propX(Expression owner,
Expression property)
|
static MethodCallExpression |
safeCallX(Expression receiver,
java.lang.String methodName,
Expression... args)
Creates a safe method call expression.
|
static StaticMethodCallExpression |
staticCallX(ClassNode clazz,
java.lang.String methodName,
Expression... args)
Creates a static method expression
AST |
static StaticMethodCallExpression |
staticCallX(java.lang.Class clazz,
java.lang.String methodName,
Expression... args)
Creates a static method expression
AST |
static DeclarationExpression |
varDeclarationX(java.lang.String varName,
java.lang.Class type,
Expression defaultValue)
Creates a variable definition expression.
|
static DeclarationExpression |
varDeclarationX(java.lang.String varName,
ClassNode type,
Expression defaultValue)
Creates a variable definition expression.
|
static VariableExpression |
varX(java.lang.String varName)
Creates a reference to a given variable
Creates a method call expression
AST |
static VariableExpression |
varX(java.lang.String varName,
ClassNode type)
Creates a reference to a given variable
Creates a method call expression
AST |
public static BooleanExpression boolX(Expression expr)
BooleanExpression from a Expression
passed as argument.
boolX(expr); // imagine expression is 1 == 1
Result
1 == 1 // as a boolean expression
expr - the expression we want to evaluate as true or falseBooleanExpressionbinEqX(org.codehaus.groovy.ast.expr.Expression, org.codehaus.groovy.ast.expr.Expression),
Types.COMPARE_EQUALpublic BooleanExpression boolEqualsNullX(Expression argExpr)
BooleanExpression of type:
boolEqualsNullX(constX("some"));
Result
"some" == null // as a boolean expression
argExpr - the operand we want to checkBooleanExpressionTypes.COMPARE_EQUALpublic BooleanExpression boolHasSameFieldX(FieldNode fNode, Expression other)
BooleanExpression of type:
boolHasSameFieldX(personNameFieldNode, anotherPersonRef);
Result
name is (peter.name) // as a boolean expression
fNode - the node we would like to checkother - Another expression having as a result another
field we would like to check againstBooleanExpressionpublic BooleanExpression boolHasSamePropertyX(PropertyNode pNode, Expression other)
BooleanExpression of type:
boolHasSamePropertyX(personNamePropertyNode, anotherPersonRef);
Result
name is (peter.name) // as a boolean expression
pNode - the node we would like to checkother - Another expression having as a result another
property we would like to check againstBooleanExpressionpublic BooleanExpression boolIsInstanceOfX(Expression objectExpression, ClassNode cNode)
BooleanExpression of type:
boolIsInstanceOfX(stringExpression, stringClassNode);
Result
"hello" instanceof String // as a boolean expression
objectExpression - expression to evaluatecNode - a ClassNodeBooleanExpressionpublic BooleanExpression boolIsInstanceOfX(Expression objectExpression, java.lang.Class cNode)
BooleanExpression of type:
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.objectExpression - expression to evaluatecNode - a Class. ThisBooleanExpressionpublic BooleanExpression boolIsOneX(Expression expr)
BooleanExpression of type:
boolIsOneX(numberExpression);
Result
10 == 1 // as a boolean expression
Utility class to check a given expression equals to the constant
expression 1.expr - expression to check against 1BooleanExpressionTypes.COMPARE_EQUALpublic BooleanExpression boolIsTrueX(Expression argExpr)
BooleanExpression of type:
boolIsTrueX(anyExpression);
Result
"something" == Boolean.TRUE // as a boolean expression or...
(1 == 1) == Boolean.TRUE
Utility class to check a given expression equals to trueargExpr - expression checked to be trueBooleanExpressionpublic BooleanExpression boolIsZeroX(Expression expr)
BooleanExpression of type:
boolIsZeroX(numberExpression);
Result
10 == 0 // as a boolean expression
Utility class to check a given expression equals to the constant
expression 0.expr - expression to check against 0BooleanExpressionTypes.COMPARE_EQUALpublic BooleanExpression boolNotNullX(Expression argExpr)
BooleanExpression of type:
boolEqualsNotNullX(constX("some"));
Result
"some" != null // as a boolean expression
argExpr - the expression we want to checkBooleanExpressionTypes.COMPARE_EQUALpublic BooleanExpression boolSameX(Expression self, Expression other)
BooleanExpression of type:
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).self - the expression we want to check againstother - another expressionBooleanExpression answering
whether a given instance is the same as another.Typespublic static BinaryExpression binEqX(Expression leftExpr, Expression rightExpr)
leftExpr - left hand side operandrightExpr - right hand side operandBinaryExpression representing an
equals expressionTypes.COMPARE_EQUALpublic static ConstantExpression constX(java.lang.Object constant)
ConstantExpression from the constant value
passed as argument
callThisX("println",
constX("1") // constant value "1"
)
Result
println "1" // print constant value "1"constant - the constant valueConstantExpressionpublic static PropertyExpression propX(Expression owner, Expression property)
owner - the instance the property belongs toproperty - PropertyExpressionpublic static ListExpression listX(Expression... expressions)
expressions - list items as expressionsListExpressionpublic static MapExpression mapX(MapEntryExpression... expressions)
MapExpression:
mapX(mapEntryX(constX('key'), constX('value')))
Result
[key: 'value']expressions - different map entries as MapEntryExpressionMapExpressionpublic static MapExpression mapX(java.util.List<MapEntryExpression> expressions)
MapExpression:
mapX(mapEntryX(constX('key'), constX('value')))
Result
[key: 'value']expressions - different map entries as MapEntryExpressionMapExpressionpublic static MapEntryExpression mapEntryX(Expression key, Expression value)
MapEntryExpressionkey - an Expression as the map entry keyvalue - an Expression as the map entry valueMapEntryExpressionpublic static ClassExpression classX(java.lang.Class clazz)
clazz - ClassExpressionpublic static ClassExpression classX(ClassNode classNode)
ClassNode
classX(A.NODES.clazz(String.class))
Result
String.classclassNode - ClassNodepublic static MethodCallExpression callThisX(java.lang.String methodName, Expression... args)
A.NODES.callThisX("println", A.EXPR.constX("hello"))
Result
println "hello"methodName - The name of the method to invokeargs - with different argument expressionsMethodCallExpressionpublic static MethodCallExpression callX(Expression receiver, java.lang.String methodName, Expression... args)
A.NODES.callX(ownerExpression, "toMD5", A.EXPR.constX("hello"))
Result
owner.toMD5("hello")receiver - target instance expressionmethodName - The name of the method to invokeargs - with different argument expressionsMethodCallExpressionpublic static MethodCallExpression safeCallX(Expression receiver, java.lang.String methodName, Expression... args)
A.NODES.safeCallX(ownerExpression, "toMD5", A.EXPR.constX("hello"))
Result
owner?.toMD5("hello")receiver - target instance expressionmethodName - The name of the method to invokeargs - with different argument expressionsMethodCallExpressionpublic static StaticMethodCallExpression staticCallX(ClassNode clazz, java.lang.String methodName, Expression... args)
A.NODES.staticCallX(systemClassNode, "currentTimeMillis")
Result
System.currentTimeMillis()clazz - the ClassNode the method belongsmethodName - The name of the method to invokeargs - with different argument expressionsStaticMethodCallExpressionpublic static StaticMethodCallExpression staticCallX(java.lang.Class clazz, java.lang.String methodName, Expression... args)
A.NODES.staticCallX(systemClass, "currentTimeMillis")
Result
System.currentTimeMillis()clazz - the Class the method belongsmethodName - The name of the method to invokeargs - with different argument expressionsStaticMethodCallExpressionpublic static FieldExpression fieldX(FieldNode fieldNode)
class A {
Integer age
}
//...
FieldExpression node = A.NODES.fieldX(ageNode)
A.EXPR.callX(node, "toString")
Result
age.toString()fieldNode - the node pointing at the fieldFieldExpressionpublic static MethodCallExpression callSuperX(java.lang.String methodName)
class A {
public void doSomething() {
super.doSomething()
}
}
//...
MethodCallExpression = A.NODES.callSuperX("doSomething")
Result
super.doSomething()methodName - name of the method we want to callMethodCallExpressionpublic static MethodCallExpression callSuperX(java.lang.String methodName, Expression... args)
class A {
public void doSomething(String constant) {
super.doSomething("1")
}
}
//...
MethodCallExpression = A.NODES.callSuperX("doSomething", constX("1"))
Result
super.doSomething("1")methodName - name of the method we want to callargs - expression representing different arguments to method call expressionMethodCallExpressionpublic static VariableExpression varX(java.lang.String varName)
class A {
public void doSomething(String constant) {
super.doSomething(constant)
}
}
//...
VariableExpression = A.NODES.varX("constant")
Result
constantvarName - VariableExpressionpublic static VariableExpression varX(java.lang.String varName, ClassNode type)
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 variablevarName - type - VariableExpressionpublic static ClosureExpression closureX(Statement stmt, Parameter... params)
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
stmt - the body of the closureparams - the closure parametersClosureExpressionpublic static BooleanExpression boolX(java.lang.String leftVarName, int tokenType, java.lang.String rightVarName)
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
leftVarName - name of the variable referenced in the left
side of the binary expressiontokenType - type of the comparison operator. Use any of
the listed in TypesrightVarName - name of the variable referenced in the
right side of the binary expressionBooleanExpression instancepublic static BooleanExpression boolX(Expression leftExpr, int tokenType, Expression rightExpr)
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
leftExpr - left expressiontokenType - type of the comparison operator. Use any of
the listed in TypesrightExpr - right expressionBooleanExpression instancepublic static BinaryExpression binX(java.lang.String leftVarName, int operator, java.lang.String rightVarName)
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
leftVarName - name of the variable referenced in the left
side of the binary expressionoperator - type of the operator. Use any of the listed in
TypesrightVarName - name of the variable referenced in the
right side of the binary expressionBinaryExpression instancepublic static BinaryExpression binX(Expression leftExpr, int operator, Expression rightExpr)
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
leftExpr - left term in the binary expression
side of the binary expressionoperator - type of the comparison operator. Use any of
the listed in TypesrightExpr - right term in the binary expressionBinaryExpression instancepublic static DeclarationExpression varDeclarationX(java.lang.String varName, ClassNode type, Expression defaultValue)
//...after declaring the variables somewhere
varDeclarationX('johnAge',
A.NODES.clazz(Integer).build(),
A.EXPR.constX(23))
Result
Integer johnAge = 23
varName - name of the declared variabletype - type of the declared variable as a ClassNode instancedefaultValue - expression setting the default value of the declared variableDeclarationExpressionpublic static DeclarationExpression varDeclarationX(java.lang.String varName, java.lang.Class type, Expression defaultValue)
//...after declaring the variables somewhere
varDeclarationX('johnAge', Integer, A.EXPR.constX(23))
Result
Integer johnAge = 23
varName - name of the declared variabletype - type of the declared variable as a Class instancedefaultValue - expression setting the default value of the declared variableDeclarationExpressionpublic static ConstructorCallExpression newX(java.lang.Class type)
newX(ArrayList)
Result
new ArrayList()
type - type of the target objectConstructorCallExpressionpublic static ConstructorCallExpression newX(java.lang.Class type, Expression... expressions)
newX(File, varX('path'), constX('myfile.txt'))
Result
new File(path, 'myfile.txt')
type - type of the target objectexpressions - a varargs of type ExpressionConstructorCallExpressionpublic static NotExpression notX(Expression expression)
notX(varX('myVariable'))
Result
!myVariable
expression - the expression to be negatedpublic static BooleanExpression lorX(Expression leftExpr, Expression rightExpr)
lorX(varX('a'), varX('b'))
Result
a || b
leftExpr - rightExpr - BooleanExpression representing a logical OR (||)public static BooleanExpression landX(Expression leftExpr, Expression rightExpr)
landX(varX('a'), varX('b'))
Result
a && b
leftExpr - rightExpr - BooleanExpression representing a logical AND (&&)