public class GeneralUtils
extends Object
Handy methods when working with the Groovy AST. Provides factory methods for creating common AST nodes and utility methods for expression and statement construction.
This utility class offers shorthand methods (often with abbreviated names) for building AST structures:
block(), expr(), assign(), ret(), etc.var(), constX(), classX(), cast(), etc.binX(), andX(), orX(), cmp(), etc.list(), map(), tuple(), array(), etc.call(), invokeMethod(), staticCall(), etc.Common patterns use abbreviated names like X suffix for expression factories
(e.g., varX() for VariableExpression) and S suffix for statement factories
(e.g., blockS() for BlockStatement).
Null Handling: Most methods handle null gracefully, often returning empty or no-op structures rather than throwing exceptions.
| Type Params | Return Type | Name and description |
|---|---|---|
|
public static BinaryExpression |
andX(Expression lhv, Expression rhv)Creates a binary expression with the AND operator joining two expressions. |
|
public static ArgumentListExpression |
args(Expression expressions)Creates an ArgumentListExpression from individual expression arguments. |
|
public static ArgumentListExpression |
args(List<Expression> expressions)Creates an ArgumentListExpression from a list of expressions. |
|
public static ArgumentListExpression |
args(Parameter parameters)Creates an ArgumentListExpression from parameter nodes, extracting their names as variable references. |
|
public static ArgumentListExpression |
args(String names)Creates an ArgumentListExpression from variable names, converting each to a VariableExpression. |
|
public static ArrayExpression |
arrayX(ClassNode elementType, List<Expression> initExpressions)Creates an ArrayExpression for the given element type and initial value expressions. |
|
public static ArrayExpression |
arrayX(ClassNode elementType, List<Expression> initExpressions, List<Expression> sizeExpressions)Creates an ArrayExpression for the given element type, dimensions, and initial values. |
|
public static CastExpression |
asX(ClassNode type, Expression expression)Creates a CastExpression converting the given expression to the specified type. |
|
public static Statement |
assignNullS(Expression target)Creates a statement that assigns a null/empty value to the target expression. |
|
public static Statement |
assignS(Expression target, Expression value) |
|
public static Expression |
assignX(Expression target, Expression value) |
|
public static AttributeExpression |
attrX(Expression owner, String attribute)
|
|
public static AttributeExpression |
attrX(Expression owner, Expression attribute) |
|
public static BinaryExpression |
binX(Expression left, Token token, Expression right) |
|
public static BlockStatement |
block(VariableScope scope, Statement stmts) |
|
public static BlockStatement |
block(VariableScope scope, List<Statement> stmts) |
|
public static BlockStatement |
block(Statement stmts) |
|
public static BooleanExpression |
boolX(Expression expr) |
|
public static BytecodeExpression |
bytecodeX(Consumer<org.objectweb.asm.MethodVisitor> writer) |
|
public static BytecodeExpression |
bytecodeX(ClassNode type, Consumer<org.objectweb.asm.MethodVisitor> writer) |
|
public static MethodCallExpression |
callSuperX(String methodName) |
|
public static MethodCallExpression |
callSuperX(String methodName, Expression args) |
|
public static MethodCallExpression |
callThisX(String methodName) |
|
public static MethodCallExpression |
callThisX(String methodName, Expression args) |
|
public static MethodCallExpression |
callX(Expression receiver, String methodName) |
|
public static MethodCallExpression |
callX(Expression receiver, String methodName, Expression args) |
|
public static MethodCallExpression |
callX(Expression receiver, Expression method, Expression args) |
|
public static StaticMethodCallExpression |
callX(ClassNode receiver, String methodName) |
|
public static StaticMethodCallExpression |
callX(ClassNode receiver, String methodName, Expression args) |
|
public static CaseStatement |
caseS(Expression expression, Statement code) |
|
public static CastExpression |
castX(ClassNode type, Expression expression) |
|
public static CastExpression |
castX(ClassNode type, Expression expression, boolean ignoreAutoboxing) |
|
public static CatchStatement |
catchS(Parameter variable, Statement code) |
|
public static ListExpression |
classList2args(List<String> args) |
|
public static ClassExpression |
classX(ClassNode clazz) |
|
public static ClassExpression |
classX(Class<?> clazz) |
|
public static Parameter[] |
cloneParams(Parameter[] parameters) |
|
public static ClosureExpression |
closureX(Parameter[] params, Statement code) |
|
public static ClosureExpression |
closureX(Statement code) |
|
public static BinaryExpression |
cmpX(Expression lhv, Expression rhv)Builds a binary expression that compares two values. |
|
public static ConstantExpression |
constX(Object val) |
|
public static ConstantExpression |
constX(Object val, boolean keepPrimitive) |
|
public static String |
convertASTToSource(ReaderSource readerSource, ASTNode expression)Converts an expression into the String source. |
|
public static void |
copyAnnotatedNodeAnnotations(AnnotatedNode annotatedNode, List<AnnotationNode> copied, List<AnnotationNode> notCopied)Copies all candidateAnnotations with retention policy RetentionPolicy.RUNTIME and RetentionPolicy.CLASS. |
|
public static void |
copyAnnotatedNodeAnnotations(AnnotatedNode annotatedNode, List<AnnotationNode> copied, List<AnnotationNode> notCopied, boolean includeGenerated)Copies all candidateAnnotations with retention policy RetentionPolicy.RUNTIME and RetentionPolicy.CLASS. |
|
public static boolean |
copyStatementsWithSuperAdjustment(ClosureExpression pre, BlockStatement body) |
|
public static Statement |
createConstructorStatementDefault(FieldNode fNode) |
|
public static Statement |
ctorSuperS(Expression args) |
|
public static Statement |
ctorSuperS() |
|
public static ConstructorCallExpression |
ctorSuperX(Expression args) |
|
public static ConstructorCallExpression |
ctorSuperX() |
|
public static Statement |
ctorThisS(Expression args) |
|
public static Statement |
ctorThisS() |
|
public static ConstructorCallExpression |
ctorThisX(Expression args) |
|
public static ConstructorCallExpression |
ctorThisX() |
|
public static ConstructorCallExpression |
ctorX(ClassNode type, Expression args) |
|
public static ConstructorCallExpression |
ctorX(ClassNode type) |
|
public static Statement |
declS(Expression target, Expression init) |
|
public static DeclarationExpression |
declX(Expression target, Expression init) |
|
public static ConstantExpression |
defaultValueX(ClassNode type)Returns a constant expression with the default value for the given type (i.e., false for boolean, 0 for numbers or null. |
|
public static ElvisOperatorExpression |
elvisX(Expression base, Expression otherwise) |
|
public static MapEntryExpression |
entryX(Expression key, Expression value) |
|
public static BinaryExpression |
eqX(Expression left, Expression right) |
|
public static BooleanExpression |
equalsNullX(Expression expr) |
|
public static FieldExpression |
fieldX(FieldNode fieldNode) |
|
public static FieldExpression |
fieldX(ClassNode owner, String fieldName) |
|
public static Expression |
findArg(String argName) |
|
public static MethodNode |
findDeclaredMethod(ClassNode cNode, String name, int argsCount) |
|
public static ForStatement |
forS(Parameter variable, Expression collectionExpression, Statement loopS) |
|
public static BinaryExpression |
geX(Expression lhv, Expression rhv) |
|
public static List<MethodNode> |
getAllMethods(ClassNode type) |
|
public static List<PropertyNode> |
getAllProperties(ClassNode type) |
|
public static List<PropertyNode> |
getAllProperties(Set<String> names, ClassNode cNode, boolean includeProperties, boolean includeFields, boolean includePseudoGetters, boolean includePseudoSetters, boolean traverseSuperClasses, boolean skipReadonly) |
|
public static List<PropertyNode> |
getAllProperties(Set<String> names, ClassNode origType, ClassNode cNode, boolean includeProperties, boolean includeFields, boolean includePseudoGetters, boolean includePseudoSetters, boolean traverseSuperClasses, boolean skipReadonly) |
|
public static List<PropertyNode> |
getAllProperties(Set<String> names, ClassNode origType, ClassNode cNode, boolean includeProperties, boolean includeFields, boolean includePseudoGetters, boolean includePseudoSetters, boolean traverseSuperClasses, boolean skipReadonly, boolean reverse, boolean allNames, boolean includeStatic) |
|
public static String |
getGetterName(PropertyNode pNode)Generally preferred to use PropertyNode.getGetterNameOrDefault directly. |
|
public static String |
getGetterName(String name, Class<?> type)WARNING: Avoid this method unless just the name and type are available. |
|
public static String |
getGetterName(String name)WARNING: Avoid this method unless just the name is available. |
|
public static List<String> |
getInstanceNonPropertyFieldNames(ClassNode cNode) |
|
public static List<FieldNode> |
getInstanceNonPropertyFields(ClassNode cNode) |
|
public static List<PropertyNode> |
getInstanceProperties(ClassNode cNode) |
|
public static List<FieldNode> |
getInstancePropertyFields(ClassNode cNode) |
|
public static List<String> |
getInstancePropertyNames(ClassNode cNode) |
|
public static Set<ClassNode> |
getInterfacesAndSuperInterfaces(ClassNode cNode) |
|
public static String |
getSetterName(String name) |
|
public static List<FieldNode> |
getSuperNonPropertyFields(ClassNode cNode) |
|
public static List<FieldNode> |
getSuperPropertyFields(ClassNode cNode) |
|
public static Expression |
getterThisX(ClassNode annotatedNode, PropertyNode pNode)This method is similar to propX(Expression, Expression) but will make sure that if the property being accessed is defined inside the classnode provided as a parameter, then a getter call is generated instead of a field access. |
|
public static Expression |
getterX(ClassNode annotatedNode, Expression receiver, PropertyNode pNode)This method is similar to propX(Expression, Expression) but will make sure that if the property being accessed is defined inside the classnode provided as a parameter, then a getter call is generated instead of a field access. |
|
public static BinaryExpression |
gtX(Expression lhv, Expression rhv) |
|
public static BinaryExpression |
hasClassX(Expression instance, ClassNode cNode) |
|
public static boolean |
hasDeclaredMethod(ClassNode cNode, String name, int argsCount) |
|
public static BinaryExpression |
hasEqualFieldX(FieldNode fNode, Expression other) |
|
public static BinaryExpression |
hasEqualPropertyX(ClassNode cNode, PropertyNode pNode, VariableExpression other) |
|
public static BinaryExpression |
hasEqualPropertyX(PropertyNode pNode, Expression other) |
|
public static BooleanExpression |
hasSameFieldX(FieldNode fNode, Expression other) |
|
public static BooleanExpression |
hasSamePropertyX(PropertyNode pNode, Expression other) |
|
public static IfStatement |
ifElseS(Expression cond, Statement thenStmt, Statement elseStmt) |
|
public static IfStatement |
ifS(Expression cond, Expression trueExpr) |
|
public static IfStatement |
ifS(Expression cond, Statement trueStmt) |
|
public static boolean |
inSamePackage(ClassNode first, ClassNode second) |
|
public static boolean |
inSamePackage(Class<?> first, Class<?> second) |
|
public static Expression |
indexX(Expression target, Expression value) |
|
public static boolean |
isDefaultVisibility(int modifiers) |
|
public static BooleanExpression |
isInstanceOfX(Expression expr, ClassNode type) |
|
public static BooleanExpression |
isNullOrInstanceOfX(Expression expr, ClassNode type)
|
|
public static BooleanExpression |
isNullX(Expression expr)Alias for equalsNullX(Expression) |
|
public static BooleanExpression |
isOneX(Expression expr) |
|
public static boolean |
isOrImplements(ClassNode type, ClassNode interfaceType) |
|
public static BooleanExpression |
isTrueX(Expression argExpr) |
|
public static BooleanExpression |
isZeroX(Expression expr) |
|
public static LambdaExpression |
lambdaX(Parameter[] params, Statement code)Builds a lambda expression |
|
public static LambdaExpression |
lambdaX(Statement code)Builds a lambda expression with no parameters |
|
public static BinaryExpression |
leX(Expression lhv, Expression rhv) |
|
public static ListExpression |
list2args(List<?> args) |
|
public static ListExpression |
listX(List<Expression> args) |
|
public static VariableExpression |
localVarX(String name) |
|
public static VariableExpression |
localVarX(String name, ClassNode type) |
|
public static BinaryExpression |
ltX(Expression lhv, Expression rhv) |
|
public static String |
makeDescriptorWithoutReturnType(MethodNode mn)
|
|
public static MapEntryExpression |
mapEntryX(Expression keyExpr, Expression valueExpr) |
|
public static MapEntryExpression |
mapEntryX(String key, Expression valueExpr) |
|
public static MapExpression |
mapX() |
|
public static MapExpression |
mapX(List<MapEntryExpression> expressions) |
|
public static boolean |
maybeFallsThrough(Statement statement) |
|
public static BinaryExpression |
minusX(Expression lhv, Expression rhv) |
|
public static BinaryExpression |
neX(Expression lhv, Expression rhv) |
|
public static BinaryExpression |
notIdenticalX(Expression lhv, Expression rhv) |
|
public static BooleanExpression |
notNullX(Expression expr) |
|
public static NotExpression |
notX(Expression expr) |
|
public static ConstantExpression |
nullX() |
|
public static BinaryExpression |
orX(Expression lhv, Expression rhv) |
|
public static Parameter |
param(ClassNode type, String name) |
|
public static Parameter |
param(ClassNode type, String name, Expression initialExpression) |
|
public static Parameter[] |
params(Parameter params) |
|
public static BinaryExpression |
plusX(Expression lhv, Expression rhv) |
|
public static PropertyExpression |
propX(Expression owner, String property) |
|
public static PropertyExpression |
propX(Expression owner, Expression property) |
|
public static PropertyExpression |
propX(Expression owner, Expression property, boolean safe) |
|
public static Statement |
returnS(Expression expr) |
|
public static Statement |
safeExpression(Expression fieldExpr, Expression expression) |
|
public static BooleanExpression |
sameX(Expression self, Expression other) |
|
public static SpreadExpression |
spreadX(Expression expr) |
|
public static Statement |
stmt(Expression expr) |
|
public static SwitchStatement |
switchS(Expression expr) |
|
public static SwitchStatement |
switchS(Expression expr, Statement defaultStatement) |
|
public static SwitchStatement |
switchS(Expression expr, List<CaseStatement> caseStatements, Statement defaultStatement) |
|
public static TernaryExpression |
ternaryX(Expression cond, Expression trueExpr, Expression elseExpr) |
|
public static PropertyExpression |
thisPropX(boolean implicit, String property) |
|
public static ThrowStatement |
throwS(Expression expr) |
|
public static TryCatchStatement |
tryCatchS(Statement tryStatement) |
|
public static TryCatchStatement |
tryCatchS(Statement tryStatement, Statement finallyStatement) |
|
public static TryCatchStatement |
tryCatchS(Statement tryStatement, Statement finallyStatement, CatchStatement catchStatements) |
|
public static VariableExpression |
varX(String name) |
|
public static VariableExpression |
varX(Variable variable) |
|
public static VariableExpression |
varX(String name, ClassNode type) |
Creates a binary expression with the AND operator joining two expressions.
lhv - the left-hand-side operandrhv - the right-hand-side operandCreates an ArgumentListExpression from individual expression arguments.
expressions - the argument expressions (may be empty)Creates an ArgumentListExpression from a list of expressions.
expressions - the list of argument expressions (may be empty)Creates an ArgumentListExpression from parameter nodes, extracting their names as variable references.
parameters - the parameter nodesCreates an ArgumentListExpression from variable names, converting each to a VariableExpression.
names - the variable names (non-null strings)Creates an ArrayExpression for the given element type and initial value expressions.
elementType - the ClassNode representing the array element typeinitExpressions - the expressions providing initial array valuesCreates an ArrayExpression for the given element type, dimensions, and initial values.
elementType - the ClassNode representing the array element typeinitExpressions - the expressions providing initial array valuessizeExpressions - the expressions providing array dimensionsCreates a CastExpression converting the given expression to the specified type.
type - the ClassNode to cast toexpression - the expression to castCreates a statement that assigns a null/empty value to the target expression. Used to clear or reset variable values.
target - the expression to assign to
Builds a binary expression that compares two values.
lhv - expression for the value to compare fromrhv - expression for the value to compare toConverts an expression into the String source. Only some specific expressions like closure expression support this.
readerSource - a sourceexpression - an expression. Can't be nullCopies all candidateAnnotations with retention policy RetentionPolicy.RUNTIME and RetentionPolicy.CLASS.
Annotations with GeneratedClosure members are not supported at present.
Copies all candidateAnnotations with retention policy RetentionPolicy.RUNTIME
and RetentionPolicy.CLASS.
Generated annotations will be copied if includeGenerated is true.
Annotations with GeneratedClosure members are not supported at present.
Returns a constant expression with the default value for the given type
(i.e., false for boolean, 0 for numbers or null.
Generally preferred to use PropertyNode.getGetterNameOrDefault directly.
WARNING: Avoid this method unless just the name and type are available. Use getGetterName(PropertyNode) if the propertyNode is available.
WARNING: Avoid this method unless just the name is available. Use getGetterName(PropertyNode) if the propertyNode is available. Use getGetterName(String, Class) if the type is available.
This method is similar to propX(Expression, Expression) but will make sure that if the property being accessed is defined inside the classnode provided as a parameter, then a getter call is generated instead of a field access.
annotatedNode - the class node where the property node is accessed frompNode - the property being accessedThis method is similar to propX(Expression, Expression) but will make sure that if the property being accessed is defined inside the classnode provided as a parameter, then a getter call is generated instead of a field access.
annotatedNode - the class node where the property node is accessed fromreceiver - the object having the propertypNode - the property being accessed
Alias for equalsNullX(Expression)
Builds a lambda expression
params - lambda parameterscode - lambda codeBuilds a lambda expression with no parameters
code - lambda code