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
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic BinaryExpressionandX(Expression lhv, Expression rhv) static ArgumentListExpressionstatic ArgumentListExpressionargs(List<Expression> expressions) static ArgumentListExpressionargs(Expression... expressions) static ArgumentListExpressionstatic StatementassignNullS(Expression target) static StatementassignS(Expression target, Expression value) static ExpressionassignX(Expression target, Expression value) static CastExpressionasX(ClassNode type, Expression expression) static ExpressionattrX(Expression oe, String propName) Deprecated.static ExpressionattrX(Expression oe, Expression prop) static BinaryExpressionbinX(Expression left, Token token, Expression right) static BlockStatementstatic BlockStatementblock(VariableScope scope, List<Statement> stmts) static BlockStatementblock(VariableScope scope, Statement... stmts) static BooleanExpressionboolX(Expression expr) static BytecodeExpressionstatic BytecodeExpressionstatic MethodCallExpressioncallSuperX(String methodName) static MethodCallExpressioncallSuperX(String methodName, Expression args) static MethodCallExpressionstatic MethodCallExpressioncallThisX(String methodName, Expression args) static StaticMethodCallExpressionstatic StaticMethodCallExpressioncallX(ClassNode receiver, String methodName, Expression args) static MethodCallExpressioncallX(Expression receiver, String methodName) static MethodCallExpressioncallX(Expression receiver, String methodName, Expression args) static MethodCallExpressioncallX(Expression receiver, Expression method, Expression args) static CaseStatementcaseS(Expression expression, Statement code) static CastExpressioncastX(ClassNode type, Expression expression) static CastExpressioncastX(ClassNode type, Expression expression, boolean ignoreAutoboxing) static CatchStatementstatic ListExpressionclassList2args(List<String> args) static ClassExpressionstatic ClassExpressionstatic Parameter[]cloneParams(Parameter[] parameters) static ClosureExpressionstatic ClosureExpressionstatic BinaryExpressioncmpX(Expression lhv, Expression rhv) Builds a binary expression that compares two values.static ConstantExpressionstatic ConstantExpressionstatic StringconvertASTToSource(ReaderSource readerSource, ASTNode expression) Converts an expression into the String source.static voidcopyAnnotatedNodeAnnotations(AnnotatedNode annotatedNode, List<AnnotationNode> copied, List<AnnotationNode> notCopied) Copies all candidateAnnotations with retention policyRetentionPolicy.RUNTIMEandRetentionPolicy.CLASS.static voidcopyAnnotatedNodeAnnotations(AnnotatedNode annotatedNode, List<AnnotationNode> copied, List<AnnotationNode> notCopied, boolean includeGenerated) Copies all candidateAnnotations with retention policyRetentionPolicy.RUNTIMEandRetentionPolicy.CLASS.static booleanstatic Statementstatic Statementstatic StatementctorSuperS(Expression args) static ConstructorCallExpressionstatic ConstructorCallExpressionctorSuperX(Expression args) static Statementstatic StatementctorThisS(Expression args) static ConstructorCallExpressionstatic ConstructorCallExpressionctorThisX(Expression args) static ConstructorCallExpressionstatic ConstructorCallExpressionctorX(ClassNode type, Expression args) static StatementdeclS(Expression target, Expression init) static DeclarationExpressiondeclX(Expression target, Expression init) static ConstantExpressiondefaultValueX(ClassNode type) Returns a constant expression with the default value for the given type (i.e.,falsefor boolean,0for numbers ornull.static ElvisOperatorExpressionelvisX(Expression base, Expression otherwise) static MapEntryExpressionentryX(Expression key, Expression value) static BooleanExpressionequalsNullX(Expression expr) static BinaryExpressioneqX(Expression left, Expression right) static FieldExpressionstatic FieldExpressionstatic Expressionstatic MethodNodefindDeclaredMethod(ClassNode cNode, String name, int argsCount) static ForStatementforS(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 StringgetGetterName(String name) WARNING: Avoid this method unless just the name is available.static StringgetGetterName(String name, Class<?> type) WARNING: Avoid this method unless just the name and type are available.static StringgetGetterName(PropertyNode pNode) Generally preferred to usePropertyNode.getGetterNameOrDefault()directly.static List<PropertyNode>getInstanceProperties(ClassNode cNode) static StringgetSetterName(String name) getSuperPropertyFields(ClassNode cNode) static ExpressiongetterThisX(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 ExpressiongetterX(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 BinaryExpressionhasClassX(Expression instance, ClassNode cNode) static booleanhasDeclaredMethod(ClassNode cNode, String name, int argsCount) static BinaryExpressionhasEqualFieldX(FieldNode fNode, Expression other) static BinaryExpressionhasEqualPropertyX(ClassNode cNode, PropertyNode pNode, VariableExpression other) static BinaryExpressionhasEqualPropertyX(PropertyNode pNode, Expression other) Deprecated.static BooleanExpressionhasSameFieldX(FieldNode fNode, Expression other) static BooleanExpressionhasSamePropertyX(PropertyNode pNode, Expression other) static IfStatementifElseS(Expression cond, Statement thenStmt, Statement elseStmt) static StatementifElseS$$bridge(Expression cond, Statement thenStmt, Statement elseStmt) Deprecated.static IfStatementifS(Expression cond, Expression trueExpr) static IfStatementifS(Expression cond, Statement trueStmt) static StatementifS$$bridge(Expression cond, Expression trueExpr) Deprecated.static StatementifS$$bridge(Expression cond, Statement trueStmt) Deprecated.static ExpressionindexX(Expression target, Expression value) static booleaninSamePackage(Class<?> first, Class<?> second) static booleaninSamePackage(ClassNode first, ClassNode second) static booleanisDefaultVisibility(int modifiers) static BooleanExpressionisInstanceOfX(Expression expr, ClassNode type) static BooleanExpressionisNullOrInstanceOfX(Expression expr, ClassNode type) static BooleanExpressionisNullX(Expression expr) Alias forequalsNullX(Expression)static BooleanExpressionisOneX(Expression expr) static booleanisOrImplements(ClassNode type, ClassNode interfaceType) static BooleanExpressionisTrueX(Expression argExpr) static BooleanExpressionisZeroX(Expression expr) static LambdaExpressionBuilds a lambda expressionstatic LambdaExpressionBuilds a lambda expression with no parametersstatic ListExpressionstatic ListExpressionlistX(List<Expression> args) static VariableExpressionstatic VariableExpressionstatic BinaryExpressionltX(Expression lhv, Expression rhv) static StringDeprecated.use MethodNodeUtils#methodDescriptorWithoutReturnType(MethodNode) insteadstatic MapEntryExpressionmapEntryX(String key, Expression valueExpr) static MapEntryExpressionmapEntryX(Expression keyExpr, Expression valueExpr) static MapExpressionmapX(List<MapEntryExpression> expressions) static booleanmaybeFallsThrough(Statement statement) static BinaryExpressionneX(Expression lhv, Expression rhv) static BinaryExpressionnotIdenticalX(Expression lhv, Expression rhv) static BooleanExpressionnotNullX(Expression expr) static NotExpressionnotX(Expression expr) static ConstantExpressionnullX()static BinaryExpressionorX(Expression lhv, Expression rhv) static Parameterstatic Parameterparam(ClassNode type, String name, Expression initialExpression) static Parameter[]static BinaryExpressionplusX(Expression lhv, Expression rhv) static PropertyExpressionpropX(Expression owner, String property) static PropertyExpressionpropX(Expression owner, Expression property) static PropertyExpressionpropX(Expression owner, Expression property, boolean safe) static ExpressionpropX$$bridge(Expression owner, String property) Deprecated.static ExpressionpropX$$bridge(Expression owner, Expression property) Deprecated.static StatementreturnS(Expression expr) static StatementsafeExpression(Expression fieldExpr, Expression expression) static BooleanExpressionsameX(Expression self, Expression other) static Statementstmt(Expression expr) static SwitchStatementswitchS(Expression expr) static SwitchStatementswitchS(Expression expr, List<CaseStatement> caseStatements, Statement defaultStatement) static SwitchStatementswitchS(Expression expr, Statement defaultStatement) static TernaryExpressionternaryX(Expression cond, Expression trueExpr, Expression elseExpr) static PropertyExpressionstatic ThrowStatementthrowS(Expression expr) static TryCatchStatementstatic TryCatchStatementstatic TryCatchStatementtryCatchS(Statement tryStatement, Statement finallyStatement, CatchStatement... catchStatements) static VariableExpressionstatic VariableExpressionstatic 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
Deprecated. -
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.,falsefor boolean,0for 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.RUNTIMEandRetentionPolicy.CLASS.Annotations with
GeneratedClosuremembers 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.RUNTIMEandRetentionPolicy.CLASS.Generatedannotations will be copied ifincludeGeneratedis true.Annotations with
GeneratedClosuremembers 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
-