Package org.apache.groovy.ast.tools
Class ExpressionUtils
java.lang.Object
org.apache.groovy.ast.tools.ExpressionUtils
Utility methods for reasoning about Groovy AST expressions.
-
Method Summary
Modifier and TypeMethodDescriptionstatic booleanisNullConstant(Expression expression) Checks whether the supplied expression is thenullliteral.static booleanisNumberOrArrayOfNumber(ClassNode targetType, boolean recurse) Determines if a type is derived from Number (or array thereof).static booleanisQualifiedThisOrSuper(Expression expression) Checks whether the supplied expression is a qualifiedthisorsuper, such asOuter.this.static booleanisSuperExpression(Expression expression) Checks whether the supplied expression referencessuper.static booleanisThisExpression(Expression expression) Checks whether the supplied expression referencesthis.static booleanisThisOrSuper(Expression expression) Checks whether the supplied expression referencesthisorsuper.static booleanisTypeOrArrayOfType(ClassNode targetType, ClassNode type, boolean recurse) Determines if a type matches another type (or array thereof).static booleanmayHaveSideEffects(Expression expression) Checks whether evaluating the supplied expression could be observed.static ConstantExpressiontransformBinaryConstantExpression(BinaryExpression be, ClassNode targetType) Converts expressions like ConstantExpression(40) + ConstantExpression(2) into the simplified ConstantExpression(42) at compile time.static ExpressionTransforms constants that would appear in annotations so they aren't lost.static ExpressiontransformInlineConstants(Expression exp, ClassNode attrType) Converts simple expressions of constants into pre-evaluated simple constants.static ExpressiontransformListOfConstants(ListExpression origList, ClassNode attrType) Given a list of constants, transform each item in the list.
-
Method Details
-
isNullConstant
Checks whether the supplied expression is thenullliteral.- Parameters:
expression- the expression to inspect- Returns:
trueif the expression is a null constant
-
isThisExpression
Checks whether the supplied expression referencesthis.- Parameters:
expression- the expression to inspect- Returns:
trueif the expression is athisreference
-
isSuperExpression
Checks whether the supplied expression referencessuper.- Parameters:
expression- the expression to inspect- Returns:
trueif the expression is asuperreference
-
isThisOrSuper
Checks whether the supplied expression referencesthisorsuper.- Parameters:
expression- the expression to inspect- Returns:
trueif the expression is either athisorsuperreference
-
isQualifiedThisOrSuper
Checks whether the supplied expression is a qualifiedthisorsuper, such asOuter.this.- Parameters:
expression- the expression to inspect- Returns:
trueif the expression is a class literal followed bythisorsuper
-
mayHaveSideEffects
Checks whether evaluating the supplied expression could be observed. Conservative: only a class literal and a variable (includingthisandsuper) are known to be free of side effects; any other expression, including a property read, is assumed to have them.- Parameters:
expression- the expression to inspect- Returns:
trueunless the expression is a class literal or a variable
-
isTypeOrArrayOfType
Determines if a type matches another type (or array thereof).- Parameters:
targetType- the candidate typetype- the type we are checking againstrecurse- true if we can have multi-dimension arrays; should be false for annotation member types- Returns:
- true if the type equals the targetType or array thereof
-
isNumberOrArrayOfNumber
Determines if a type is derived from Number (or array thereof).- Parameters:
targetType- the candidate typerecurse- true if we can have multi-dimension arrays; should be false for annotation member types- Returns:
- true if the type equals the targetType or array thereof
-
transformBinaryConstantExpression
public static ConstantExpression transformBinaryConstantExpression(BinaryExpression be, ClassNode targetType) Converts expressions like ConstantExpression(40) + ConstantExpression(2) into the simplified ConstantExpression(42) at compile time.- Parameters:
be- the binary expressiontargetType- the type of the result- Returns:
- the transformed expression or the original if no transformation was performed
-
transformInlineConstants
Transforms constants that would appear in annotations so they aren't lost. Subsequent processing determines whether they are valid, this retains the constant value as a constant expression.The attribute values of annotations must be primitive, string, annotation or enumeration constants. In various places such constants can be seen during type resolution but won't be readily accessible in later phases, e.g. they might be embedded into constructor code.
- Parameters:
exp- the original expression- Returns:
- original or transformed expression
-
transformInlineConstants
Converts simple expressions of constants into pre-evaluated simple constants. Handles:- Property expressions - referencing constants
- Variable expressions - referencing constants
- Typecast expressions - referencing constants
- Binary expressions - string concatenation and numeric +, -, /, *
- List expressions - list of constants
- Parameters:
exp- the original expressionattrType- the type that the final constant should be- Returns:
- the transformed type or the original if no transformation was possible
-
transformListOfConstants
Given a list of constants, transform each item in the list.- Parameters:
origList- the list to transformattrType- the target type- Returns:
- the transformed list or the original if nothing was changed
-