Package org.codehaus.groovy.ast.tools
Class GeneralUtils
java.lang.Object
org.codehaus.groovy.ast.tools.GeneralUtils
Handy methods when working with the Groovy AST
-
Field Summary
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic BinaryExpression
andX
(Expression lhv, Expression rhv) static ArgumentListExpression
static ArgumentListExpression
args
(List<Expression> expressions) static ArgumentListExpression
args
(Expression... expressions) static ArgumentListExpression
static Statement
assignNullS
(Expression target) static Statement
assignS
(Expression target, Expression value) static Expression
assignX
(Expression target, Expression value) static CastExpression
asX
(ClassNode type, Expression expression) static Expression
attrX
(Expression oe, String propName) static Expression
attrX
(Expression oe, Expression prop) static BinaryExpression
binX
(Expression left, Token token, Expression right) static BlockStatement
static BlockStatement
block
(VariableScope scope, List<Statement> stmts) static BlockStatement
block
(VariableScope scope, Statement... stmts) static BooleanExpression
boolX
(Expression expr) static BytecodeExpression
static BytecodeExpression
static MethodCallExpression
callSuperX
(String methodName) static MethodCallExpression
callSuperX
(String methodName, Expression args) static MethodCallExpression
static MethodCallExpression
callThisX
(String methodName, Expression args) static StaticMethodCallExpression
static StaticMethodCallExpression
callX
(ClassNode receiver, String methodName, Expression args) static MethodCallExpression
callX
(Expression receiver, String methodName) static MethodCallExpression
callX
(Expression receiver, String methodName, Expression args) static MethodCallExpression
callX
(Expression receiver, Expression method, Expression args) static CaseStatement
caseS
(Expression expression, Statement code) static CastExpression
castX
(ClassNode type, Expression expression) static CastExpression
castX
(ClassNode type, Expression expression, boolean ignoreAutoboxing) static CatchStatement
static ListExpression
classList2args
(List<String> args) static ClassExpression
static ClassExpression
static Parameter[]
cloneParams
(Parameter[] parameters) static ClosureExpression
static ClosureExpression
static BinaryExpression
cmpX
(Expression lhv, Expression rhv) Builds a binary expression that compares two values.static ConstantExpression
static ConstantExpression
static String
convertASTToSource
(ReaderSource readerSource, ASTNode expression) Converts an expression into the String source.static void
copyAnnotatedNodeAnnotations
(AnnotatedNode annotatedNode, List<AnnotationNode> copied, List<AnnotationNode> notCopied) Copies all candidateAnnotations with retention policyRetentionPolicy.RUNTIME
andRetentionPolicy.CLASS
.static void
copyAnnotatedNodeAnnotations
(AnnotatedNode annotatedNode, List<AnnotationNode> copied, List<AnnotationNode> notCopied, boolean includeGenerated) Copies all candidateAnnotations with retention policyRetentionPolicy.RUNTIME
andRetentionPolicy.CLASS
.static boolean
static Statement
static Statement
static Statement
ctorSuperS
(Expression args) static ConstructorCallExpression
static ConstructorCallExpression
ctorSuperX
(Expression args) static Statement
static Statement
ctorThisS
(Expression args) static ConstructorCallExpression
static ConstructorCallExpression
ctorThisX
(Expression args) static ConstructorCallExpression
static ConstructorCallExpression
ctorX
(ClassNode type, Expression args) static Statement
declS
(Expression target, Expression init) static DeclarationExpression
declX
(Expression target, Expression init) 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 ornull
.static ElvisOperatorExpression
elvisX
(Expression base, Expression otherwise) static MapEntryExpression
entryX
(Expression key, Expression value) static BooleanExpression
equalsNullX
(Expression expr) static BinaryExpression
eqX
(Expression left, Expression right) static FieldExpression
static FieldExpression
static Expression
static MethodNode
findDeclaredMethod
(ClassNode cNode, String name, int argsCount) static ForStatement
forS
(Parameter variable, Expression collectionExpression, Statement loopS) static List<MethodNode>
getAllMethods
(ClassNode type) static List<PropertyNode>
getAllProperties
(Set<String> names, ClassNode cNode, boolean includeProperties, boolean includeFields, boolean includePseudoGetters, boolean includePseudoSetters, boolean traverseSuperClasses, boolean skipReadonly) static List<PropertyNode>
getAllProperties
(Set<String> names, ClassNode origType, ClassNode cNode, boolean includeProperties, boolean includeFields, boolean includePseudoGetters, boolean includePseudoSetters, boolean traverseSuperClasses, boolean skipReadonly) 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) static List<PropertyNode>
getAllProperties
(ClassNode type) static String
getGetterName
(String name) WARNING: Avoid this method unless just the name is available.static String
getGetterName
(String name, Class<?> type) WARNING: Avoid this method unless just the name and type are available.static String
getGetterName
(PropertyNode pNode) Generally preferred to usePropertyNode.getGetterNameOrDefault()
directly.static List<PropertyNode>
getInstanceProperties
(ClassNode cNode) static String
getSetterName
(String name) getSuperPropertyFields
(ClassNode cNode) static Expression
getterThisX
(ClassNode annotatedNode, PropertyNode pNode) This method is similar topropX(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.static Expression
getterX
(ClassNode annotatedNode, Expression receiver, PropertyNode pNode) This method is similar topropX(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.static BinaryExpression
hasClassX
(Expression instance, ClassNode cNode) static boolean
hasDeclaredMethod
(ClassNode cNode, String name, int argsCount) static BinaryExpression
hasEqualFieldX
(FieldNode fNode, Expression other) static BinaryExpression
hasEqualPropertyX
(ClassNode cNode, PropertyNode pNode, VariableExpression other) static BinaryExpression
hasEqualPropertyX
(PropertyNode pNode, Expression other) Deprecated.static BooleanExpression
hasSameFieldX
(FieldNode fNode, Expression other) static BooleanExpression
hasSamePropertyX
(PropertyNode pNode, Expression other) static IfStatement
ifElseS
(Expression cond, Statement thenStmt, Statement elseStmt) static Statement
ifElseS$$bridge
(Expression cond, Statement thenStmt, Statement elseStmt) Deprecated.static IfStatement
ifS
(Expression cond, Expression trueExpr) static IfStatement
ifS
(Expression cond, Statement trueStmt) static Statement
ifS$$bridge
(Expression cond, Expression trueExpr) Deprecated.static Statement
ifS$$bridge
(Expression cond, Statement trueStmt) Deprecated.static Expression
indexX
(Expression target, Expression value) static boolean
inSamePackage
(Class<?> first, Class<?> second) static boolean
inSamePackage
(ClassNode first, ClassNode second) static boolean
isDefaultVisibility
(int modifiers) static BooleanExpression
isInstanceOfX
(Expression expr, ClassNode type) static BooleanExpression
isNullOrInstanceOfX
(Expression expr, ClassNode type) static BooleanExpression
isNullX
(Expression expr) Alias forequalsNullX(Expression)
static BooleanExpression
isOneX
(Expression expr) static boolean
isOrImplements
(ClassNode type, ClassNode interfaceType) static BooleanExpression
isTrueX
(Expression argExpr) static BooleanExpression
isZeroX
(Expression expr) static LambdaExpression
Builds a lambda expressionstatic LambdaExpression
Builds a lambda expression with no parametersstatic ListExpression
static ListExpression
listX
(List<Expression> args) static VariableExpression
static VariableExpression
static BinaryExpression
ltX
(Expression lhv, Expression rhv) static String
Deprecated.use MethodNodeUtils#methodDescriptorWithoutReturnType(MethodNode) insteadstatic MapEntryExpression
mapEntryX
(String key, Expression valueExpr) static MapEntryExpression
mapEntryX
(Expression keyExpr, Expression valueExpr) static MapExpression
mapX
(List<MapEntryExpression> expressions) static boolean
maybeFallsThrough
(Statement statement) static BinaryExpression
neX
(Expression lhv, Expression rhv) static BinaryExpression
notIdenticalX
(Expression lhv, Expression rhv) static BooleanExpression
notNullX
(Expression expr) static NotExpression
notX
(Expression expr) static ConstantExpression
nullX()
static BinaryExpression
orX
(Expression lhv, Expression rhv) static Parameter
static Parameter
param
(ClassNode type, String name, Expression initialExpression) static Parameter[]
static BinaryExpression
plusX
(Expression lhv, Expression rhv) static PropertyExpression
propX
(Expression owner, String property) static PropertyExpression
propX
(Expression owner, Expression property) static PropertyExpression
propX
(Expression owner, Expression property, boolean safe) static Expression
propX$$bridge
(Expression owner, String property) Deprecated.static Expression
propX$$bridge
(Expression owner, Expression property) Deprecated.static Statement
returnS
(Expression expr) static Statement
safeExpression
(Expression fieldExpr, Expression expression) static BooleanExpression
sameX
(Expression self, Expression other) static Statement
stmt
(Expression expr) static SwitchStatement
switchS
(Expression expr) static SwitchStatement
switchS
(Expression expr, List<CaseStatement> caseStatements, Statement defaultStatement) static SwitchStatement
switchS
(Expression expr, Statement defaultStatement) static TernaryExpression
ternaryX
(Expression cond, Expression trueExpr, Expression elseExpr) static PropertyExpression
static ThrowStatement
throwS
(Expression expr) static TryCatchStatement
static TryCatchStatement
static TryCatchStatement
tryCatchS
(Statement tryStatement, Statement finallyStatement, CatchStatement... catchStatements) static VariableExpression
static VariableExpression
static VariableExpression
-
Field Details
-
ASSIGN
-
EQ
-
NE
-
NOT_IDENTICAL
-
LT
-
AND
-
OR
-
CMP
-
INSTANCEOF
-
PLUS
-
-
Constructor Details
-
GeneralUtils
public GeneralUtils()
-
-
Method Details
-
andX
-
args
-
args
-
args
-
args
-
asX
-
assignS
-
assignNullS
-
assignX
-
attrX
-
attrX
-
binX
-
block
-
block
-
block
-
boolX
-
bytecodeX
-
bytecodeX
public static BytecodeExpression bytecodeX(ClassNode type, Consumer<org.objectweb.asm.MethodVisitor> writer) -
callSuperX
-
callSuperX
-
callThisX
-
callThisX
-
callX
-
callX
-
callX
-
callX
-
callX
public static StaticMethodCallExpression callX(ClassNode receiver, String methodName, Expression args) -
caseS
-
castX
-
castX
-
catchS
-
classX
-
classX
-
closureX
-
closureX
-
lambdaX
Builds a lambda expression- Parameters:
params
- lambda parameterscode
- lambda code- Returns:
- the lambda expression
-
lambdaX
Builds a lambda expression with no parameters- Parameters:
code
- lambda code- Returns:
- the lambda expression
-
cmpX
Builds a binary expression that compares two values.- Parameters:
lhv
- expression for the value to compare fromrhv
- expression for the value to compare to- Returns:
- the expression comparing two values
-
constX
-
constX
-
ctorX
-
ctorX
-
ctorSuperS
-
ctorSuperX
-
ctorThisS
-
ctorThisX
-
ctorSuperS
-
ctorSuperX
-
ctorThisS
-
ctorThisX
-
declS
-
declX
-
defaultValueX
Returns a constant expression with the default value for the given type (i.e.,false
for boolean,0
for numbers ornull
.- Since:
- 4.0.0
-
elvisX
-
entryX
-
eqX
-
equalsNullX
-
fieldX
-
fieldX
-
findArg
-
forS
public static ForStatement forS(Parameter variable, Expression collectionExpression, Statement loopS) -
getAllMethods
-
getAllProperties
-
getInstanceNonPropertyFields
-
getInstanceNonPropertyFieldNames
-
getInstanceProperties
-
getInstancePropertyNames
-
getInstancePropertyFields
-
getInterfacesAndSuperInterfaces
-
getSuperNonPropertyFields
-
getSuperPropertyFields
-
getAllProperties
public static List<PropertyNode> getAllProperties(Set<String> names, ClassNode cNode, boolean includeProperties, boolean includeFields, boolean includePseudoGetters, boolean includePseudoSetters, boolean traverseSuperClasses, boolean skipReadonly) -
getAllProperties
-
getAllProperties
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) -
getterThisX
This method is similar topropX(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.- Parameters:
annotatedNode
- the class node where the property node is accessed frompNode
- the property being accessed- Returns:
- a method call expression or a property expression
-
getterX
This method is similar topropX(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.- Parameters:
annotatedNode
- the class node where the property node is accessed fromreceiver
- the object having the propertypNode
- the property being accessed- Returns:
- a method call expression or a property expression
-
hasClassX
-
hasEqualFieldX
-
hasEqualPropertyX
public static BinaryExpression hasEqualPropertyX(ClassNode cNode, PropertyNode pNode, VariableExpression other) -
hasEqualPropertyX
Deprecated. -
hasSameFieldX
-
hasSamePropertyX
-
ifElseS$$bridge
@Deprecated public static Statement ifElseS$$bridge(Expression cond, Statement thenStmt, Statement elseStmt) Deprecated. -
ifElseS
-
ifS$$bridge
Deprecated. -
ifS
-
ifS$$bridge
Deprecated. -
ifS
-
indexX
-
isInstanceOfX
-
isNullOrInstanceOfX
- Since:
- 4.0.8
-
isNullX
Alias forequalsNullX(Expression)
-
isOneX
-
isTrueX
-
isZeroX
-
listX
-
list2args
-
classList2args
-
localVarX
-
localVarX
-
ltX
-
mapEntryX
-
mapEntryX
-
mapX
-
neX
-
notIdenticalX
-
notNullX
-
notX
-
nullX
-
orX
-
param
-
param
-
params
-
plusX
-
propX
-
propX$$bridge
Deprecated. -
propX
-
propX$$bridge
Deprecated. -
propX
-
returnS
-
safeExpression
-
sameX
-
stmt
-
switchS
-
switchS
-
switchS
public static SwitchStatement switchS(Expression expr, List<CaseStatement> caseStatements, Statement defaultStatement) -
ternaryX
-
thisPropX
-
throwS
-
tryCatchS
-
tryCatchS
-
tryCatchS
public static TryCatchStatement tryCatchS(Statement tryStatement, Statement finallyStatement, CatchStatement... catchStatements) -
varX
-
varX
-
varX
-
cloneParams
-
copyAnnotatedNodeAnnotations
public static void copyAnnotatedNodeAnnotations(AnnotatedNode annotatedNode, List<AnnotationNode> copied, List<AnnotationNode> notCopied) Copies all candidateAnnotations with retention policyRetentionPolicy.RUNTIME
andRetentionPolicy.CLASS
.Annotations with
GeneratedClosure
members are not supported at present. -
copyAnnotatedNodeAnnotations
public static void copyAnnotatedNodeAnnotations(AnnotatedNode annotatedNode, List<AnnotationNode> copied, List<AnnotationNode> notCopied, boolean includeGenerated) Copies all candidateAnnotations with retention policyRetentionPolicy.RUNTIME
andRetentionPolicy.CLASS
.Generated
annotations will be copied ifincludeGenerated
is true.Annotations with
GeneratedClosure
members are not supported at present. -
createConstructorStatementDefault
-
getGetterName
Generally preferred to usePropertyNode.getGetterNameOrDefault()
directly. -
getGetterName
WARNING: Avoid this method unless just the name and type are available. UsegetGetterName(PropertyNode)
if the propertyNode is available. -
getGetterName
WARNING: Avoid this method unless just the name is available. UsegetGetterName(PropertyNode)
if the propertyNode is available. UsegetGetterName(String, Class)
if the type is available. -
getSetterName
-
convertASTToSource
public static String convertASTToSource(ReaderSource readerSource, ASTNode expression) throws Exception Converts an expression into the String source. Only some specific expressions like closure expression support this.- Parameters:
readerSource
- a sourceexpression
- an expression. Can't be null- Returns:
- the source the closure was created from
- Throws:
IllegalArgumentException
- when expression is nullException
- when closure can't be read from source
-
copyStatementsWithSuperAdjustment
-
hasDeclaredMethod
-
findDeclaredMethod
-
inSamePackage
-
inSamePackage
-
isDefaultVisibility
public static boolean isDefaultVisibility(int modifiers) -
isOrImplements
-
makeDescriptorWithoutReturnType
Deprecated.use MethodNodeUtils#methodDescriptorWithoutReturnType(MethodNode) instead -
maybeFallsThrough
-