Package org.codehaus.groovy.ast.tools
Class GeneralUtils
java.lang.Object
org.codehaus.groovy.ast.tools.GeneralUtils
public class GeneralUtils
extends java.lang.Object
Handy methods when working with the Groovy AST
-
Field Summary
-
Constructor Summary
Constructors Constructor Description GeneralUtils()
-
Method Summary
Modifier and Type Method Description static BinaryExpression
andX(Expression lhv, Expression rhv)
static ArgumentListExpression
args(java.lang.String... names)
static ArgumentListExpression
args(java.util.List<Expression> expressions)
static ArgumentListExpression
args(Expression... expressions)
static ArgumentListExpression
args(Parameter... parameters)
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, Expression prop)
static BinaryExpression
binX(Expression left, Token token, Expression right)
static BlockStatement
block(Statement... stmts)
static BlockStatement
block(VariableScope scope, java.util.List<Statement> stmts)
static BlockStatement
block(VariableScope scope, Statement... stmts)
static BooleanExpression
boolX(Expression expr)
static BytecodeExpression
bytecodeX(java.util.function.Consumer<org.objectweb.asm.MethodVisitor> writer)
static BytecodeExpression
bytecodeX(ClassNode type, java.util.function.Consumer<org.objectweb.asm.MethodVisitor> writer)
static MethodCallExpression
callSuperX(java.lang.String methodName)
static MethodCallExpression
callSuperX(java.lang.String methodName, Expression args)
static MethodCallExpression
callThisX(java.lang.String methodName)
static MethodCallExpression
callThisX(java.lang.String methodName, Expression args)
static StaticMethodCallExpression
callX(ClassNode receiver, java.lang.String methodName)
static StaticMethodCallExpression
callX(ClassNode receiver, java.lang.String methodName, Expression args)
static MethodCallExpression
callX(Expression receiver, java.lang.String methodName)
static MethodCallExpression
callX(Expression receiver, java.lang.String methodName, Expression args)
static MethodCallExpression
callX(Expression receiver, Expression method, Expression args)
static CastExpression
castX(ClassNode type, Expression expression)
static CastExpression
castX(ClassNode type, Expression expression, boolean ignoreAutoboxing)
static CatchStatement
catchS(Parameter variable, Statement code)
static ListExpression
classList2args(java.util.List<java.lang.String> args)
static ClassExpression
classX(java.lang.Class<?> clazz)
static ClassExpression
classX(ClassNode clazz)
static Parameter[]
cloneParams(Parameter[] parameters)
static ClosureExpression
closureX(Parameter[] params, Statement code)
static ClosureExpression
closureX(Statement code)
static BinaryExpression
cmpX(Expression lhv, Expression rhv)
Builds a binary expression that compares two values.static ConstantExpression
constX(java.lang.Object val)
static ConstantExpression
constX(java.lang.Object val, boolean keepPrimitive)
static java.lang.String
convertASTToSource(ReaderSource readerSource, ASTNode expression)
Converts an expression into the String source.static void
copyAnnotatedNodeAnnotations(AnnotatedNode annotatedNode, java.util.List<AnnotationNode> copied, java.util.List<AnnotationNode> notCopied)
Copies all candidateAnnotations with retention policyRetentionPolicy.RUNTIME
andRetentionPolicy.CLASS
.static void
copyAnnotatedNodeAnnotations(AnnotatedNode annotatedNode, java.util.List<AnnotationNode> copied, java.util.List<AnnotationNode> notCopied, boolean includeGenerated)
Copies all candidateAnnotations with retention policyRetentionPolicy.RUNTIME
andRetentionPolicy.CLASS
.static boolean
copyStatementsWithSuperAdjustment(ClosureExpression pre, BlockStatement body)
static Statement
createConstructorStatementDefault(FieldNode fNode)
static Statement
ctorSuperS()
static Statement
ctorSuperS(Expression args)
static ConstructorCallExpression
ctorSuperX()
static ConstructorCallExpression
ctorSuperX(Expression args)
static Statement
ctorThisS()
static Statement
ctorThisS(Expression args)
static ConstructorCallExpression
ctorThisX()
static ConstructorCallExpression
ctorThisX(Expression args)
static ConstructorCallExpression
ctorX(ClassNode type)
static ConstructorCallExpression
ctorX(ClassNode type, Expression args)
static Statement
declS(Expression target, Expression init)
static DeclarationExpression
declX(Expression target, Expression init)
static ElvisOperatorExpression
elvisX(Expression base, Expression otherwise)
static MapEntryExpression
entryX(Expression key, Expression value)
static BooleanExpression
equalsNullX(Expression argExpr)
static BinaryExpression
eqX(Expression lhv, Expression rhv)
static FieldExpression
fieldX(ClassNode owner, java.lang.String fieldName)
static FieldExpression
fieldX(FieldNode fieldNode)
static Expression
findArg(java.lang.String argName)
static java.util.List<MethodNode>
getAllMethods(ClassNode type)
static java.util.List<PropertyNode>
getAllProperties(java.util.Set<java.lang.String> names, ClassNode cNode, boolean includeProperties, boolean includeFields, boolean includePseudoGetters, boolean includePseudoSetters, boolean traverseSuperClasses, boolean skipReadonly)
static java.util.List<PropertyNode>
getAllProperties(java.util.Set<java.lang.String> names, ClassNode origType, ClassNode cNode, boolean includeProperties, boolean includeFields, boolean includePseudoGetters, boolean includePseudoSetters, boolean traverseSuperClasses, boolean skipReadonly)
static java.util.List<PropertyNode>
getAllProperties(java.util.Set<java.lang.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 java.util.List<PropertyNode>
getAllProperties(ClassNode type)
static java.lang.String
getGetterName(PropertyNode pNode)
static java.util.List<java.lang.String>
getInstanceNonPropertyFieldNames(ClassNode cNode)
static java.util.List<FieldNode>
getInstanceNonPropertyFields(ClassNode cNode)
static java.util.List<PropertyNode>
getInstanceProperties(ClassNode cNode)
static java.util.List<FieldNode>
getInstancePropertyFields(ClassNode cNode)
static java.util.List<java.lang.String>
getInstancePropertyNames(ClassNode cNode)
static java.util.Set<ClassNode>
getInterfacesAndSuperInterfaces(ClassNode cNode)
static java.lang.String
getSetterName(java.lang.String name)
static java.util.List<FieldNode>
getSuperNonPropertyFields(ClassNode cNode)
static java.util.List<FieldNode>
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, java.lang.String name, int argsCount)
static BinaryExpression
hasEqualFieldX(FieldNode fNode, Expression other)
static BinaryExpression
hasEqualPropertyX(ClassNode annotatedNode, 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(java.lang.Class<?> first, java.lang.Class<?> second)
static boolean
inSamePackage(ClassNode first, ClassNode second)
static boolean
isDefaultVisibility(int modifiers)
static BooleanExpression
isInstanceOfX(Expression objectExpression, ClassNode cNode)
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 ListExpression
list2args(java.util.List<?> args)
static ListExpression
listX(java.util.List<Expression> args)
static VariableExpression
localVarX(java.lang.String name)
static VariableExpression
localVarX(java.lang.String name, ClassNode type)
static BinaryExpression
ltX(Expression lhv, Expression rhv)
static java.lang.String
makeDescriptorWithoutReturnType(MethodNode mn)
Deprecated.use MethodNodeUtils#methodDescriptorWithoutReturnType(MethodNode) insteadstatic MapExpression
mapX(java.util.List<MapEntryExpression> expressions)
static BinaryExpression
neX(Expression lhv, Expression rhv)
static BinaryExpression
notIdenticalX(Expression lhv, Expression rhv)
static BooleanExpression
notNullX(Expression argExpr)
static NotExpression
notX(Expression expr)
static ConstantExpression
nullX()
static BinaryExpression
orX(Expression lhv, Expression rhv)
static Parameter
param(ClassNode type, java.lang.String name)
static Parameter
param(ClassNode type, java.lang.String name, Expression initialExpression)
static Parameter[]
params(Parameter... params)
static BinaryExpression
plusX(Expression lhv, Expression rhv)
static PropertyExpression
propX(Expression owner, java.lang.String property)
static PropertyExpression
propX(Expression owner, Expression property)
static PropertyExpression
propX(Expression owner, Expression property, boolean safe)
static Expression
propX$$bridge(Expression owner, java.lang.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 TernaryExpression
ternaryX(Expression cond, Expression trueExpr, Expression elseExpr)
static PropertyExpression
thisPropX(boolean implicit, java.lang.String property)
static ThrowStatement
throwS(Expression expr)
static TryCatchStatement
tryCatchS(Statement tryStatement)
static TryCatchStatement
tryCatchS(Statement tryStatement, Statement finallyStatement)
static TryCatchStatement
tryCatchS(Statement tryStatement, Statement finallyStatement, CatchStatement... catchStatements)
static VariableExpression
varX(java.lang.String name)
static VariableExpression
varX(java.lang.String name, ClassNode type)
static VariableExpression
varX(Variable variable)
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Field Details
-
ASSIGN
-
EQ
-
NE
-
NOT_IDENTICAL
-
LT
-
AND
-
OR
-
CMP
-
INSTANCEOF
-
-
Constructor Details
-
GeneralUtils
public GeneralUtils()
-
-
Method Details
-
andX
-
args
-
args
-
args
-
args
-
asX
-
assignS
-
assignNullS
-
assignX
-
attrX
-
binX
-
block
-
block
-
block
-
boolX
-
bytecodeX
public static BytecodeExpression bytecodeX(java.util.function.Consumer<org.objectweb.asm.MethodVisitor> writer) -
bytecodeX
public static BytecodeExpression bytecodeX(ClassNode type, java.util.function.Consumer<org.objectweb.asm.MethodVisitor> writer) -
callSuperX
-
callSuperX
-
callThisX
-
callThisX
-
callX
-
callX
public static MethodCallExpression callX(Expression receiver, java.lang.String methodName, Expression args) -
callX
-
callX
-
callX
public static StaticMethodCallExpression callX(ClassNode receiver, java.lang.String methodName, Expression args) -
castX
-
castX
public static CastExpression castX(ClassNode type, Expression expression, boolean ignoreAutoboxing) -
catchS
-
classX
-
classX
-
closureX
-
closureX
-
cmpX
Builds a binary expression that compares two values.- Parameters:
lhv
- expression for the value to compare fromrhv
- expression for the value value to compare to- Returns:
- the expression comparing two values
-
constX
-
constX
-
ctorX
-
ctorX
-
ctorSuperS
-
ctorSuperX
-
ctorThisS
-
ctorThisX
-
ctorSuperS
-
ctorSuperX
-
ctorThisS
-
ctorThisX
-
declS
-
declX
-
elvisX
- Since:
- 3.0.10
-
entryX
-
eqX
-
equalsNullX
-
fieldX
-
fieldX
-
findArg
-
getAllMethods
-
getAllProperties
-
getInstanceNonPropertyFields
-
getInstanceNonPropertyFieldNames
-
getInstanceProperties
-
getInstancePropertyNames
-
getInstancePropertyFields
-
getInterfacesAndSuperInterfaces
-
getSuperNonPropertyFields
-
getSuperPropertyFields
-
getAllProperties
public static java.util.List<PropertyNode> getAllProperties(java.util.Set<java.lang.String> names, ClassNode cNode, boolean includeProperties, boolean includeFields, boolean includePseudoGetters, boolean includePseudoSetters, boolean traverseSuperClasses, boolean skipReadonly) -
getAllProperties
public static java.util.List<PropertyNode> getAllProperties(java.util.Set<java.lang.String> names, ClassNode origType, ClassNode cNode, boolean includeProperties, boolean includeFields, boolean includePseudoGetters, boolean includePseudoSetters, boolean traverseSuperClasses, boolean skipReadonly) -
getAllProperties
public static java.util.List<PropertyNode> getAllProperties(java.util.Set<java.lang.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 annotatedNode, 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
-
isNullX
Alias forequalsNullX(Expression)
-
isOneX
-
isTrueX
-
isZeroX
-
listX
-
list2args
-
classList2args
-
localVarX
-
localVarX
-
ltX
-
mapX
-
neX
-
notIdenticalX
-
notNullX
-
notX
-
nullX
-
orX
-
param
-
param
-
params
-
plusX
-
propX
-
propX$$bridge
Deprecated. -
propX
-
propX$$bridge
Deprecated. -
propX
-
returnS
-
safeExpression
-
sameX
-
stmt
-
ternaryX
public static TernaryExpression ternaryX(Expression cond, Expression trueExpr, Expression elseExpr) -
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, java.util.List<AnnotationNode> copied, java.util.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, java.util.List<AnnotationNode> copied, java.util.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
-
getSetterName
public static java.lang.String getSetterName(java.lang.String name) -
convertASTToSource
public static java.lang.String convertASTToSource(ReaderSource readerSource, ASTNode expression) throws java.lang.ExceptionConverts 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:
java.lang.IllegalArgumentException
- when expression is nulljava.lang.Exception
- when closure can't be read from source
-
copyStatementsWithSuperAdjustment
public static boolean copyStatementsWithSuperAdjustment(ClosureExpression pre, BlockStatement body) -
hasDeclaredMethod
-
inSamePackage
-
inSamePackage
public static boolean inSamePackage(java.lang.Class<?> first, java.lang.Class<?> second) -
isDefaultVisibility
public static boolean isDefaultVisibility(int modifiers) -
isOrImplements
-
makeDescriptorWithoutReturnType
Deprecated.use MethodNodeUtils#methodDescriptorWithoutReturnType(MethodNode) instead
-