@java.lang.SuppressWarnings("PMD.CollapsibleIfStatements") public class AstUtil extends java.lang.Object
Contains static utility methods and constants related to Groovy AST.
This is an internal class and its API is subject to change.Modifiers | Name | Description |
---|---|---|
static java.util.List<java.lang.String> |
AUTO_IMPORTED_CLASSES |
|
static java.util.List<java.lang.String> |
AUTO_IMPORTED_PACKAGES |
|
static java.util.List<java.lang.String> |
COMPARISON_OPERATORS |
Type Params | Return Type | Name and description |
---|---|---|
|
public static boolean |
classNodeHasProperty(ClassNode classNode, java.lang.String propertyName) |
|
public static boolean |
classNodeImplementsType(ClassNode node, java.lang.Class target) This method tells you if a ClassNode implements or extends a certain class. |
|
public static java.lang.String |
createPrettyExpression(ASTNode expression) |
|
public static boolean |
expressionIsAssignment(ASTNode node, java.lang.String variableName) |
|
public static boolean |
expressionIsNullCheck(ASTNode node) |
|
public static int |
findClassDeclarationLineNumber(ClassNode node, SourceCode sourceCode) |
|
public static int |
findFirstNonAnnotationLine(ASTNode node, SourceCode sourceCode) Gets the first non annotation line number of a node, taking into account annotations. |
|
public static AnnotationNode |
getAnnotation(AnnotatedNode node, java.lang.String name) Return the AnnotationNode for the named annotation, or else null. |
|
public static java.util.List<java.lang.String> |
getArgumentNames(MethodCallExpression methodCall) Gets the argument names of a method call. |
|
public static java.lang.String |
getDeclaration(ASTNode node, SourceCode sourceCode) |
|
public static ASTNode |
getEmptyBlock(Statement origStatement) |
|
public static java.lang.Class |
getFieldType(ClassNode node, java.lang.String fieldName) Supports discovering many common JDK types, but not all. |
|
public static java.lang.Class |
getFieldType(FieldNode field) Supports discovering many common JDK types, but not all. |
|
public static java.lang.String |
getInstanceOfTarget(java.lang.Object expression) |
|
public static java.lang.String |
getLastLineOfNodeText(ASTNode expression, SourceCode sourceCode) |
|
public static java.util.List<? extends Expression> |
getMethodArguments(ASTNode methodCall) Return the List of Arguments for the specified MethodCallExpression or a ConstructorCallExpression. |
|
public static java.lang.String |
getNodeText(ASTNode expression, SourceCode sourceCode) |
|
public static java.lang.String |
getNullComparisonTarget(java.lang.Object expression) |
|
public static java.util.List<java.lang.String> |
getParameterNames(MethodNode node) Gets the parameter names of a method node. |
|
public static java.lang.String |
getRawLine(SourceCode sourceCode, int lineNumber) |
|
public static java.lang.String |
getSourceBetweenNodes(ASTNode beforeNode, ASTNode afterNode, SourceCode sourceCode) |
|
public static java.util.List<java.lang.String> |
getSourceLinesForNode(ASTNode node, SourceCode sourceCode) |
|
public static java.util.List<Expression> |
getVariableExpressions(org.codehaus.groovy.ast.expr.DeclarationExpression declarationExpression) Return the List of VariableExpression objects referenced by the specified DeclarationExpression. |
|
public static boolean |
hasAnnotation(AnnotatedNode node, java.lang.String name) Return true only if the node has the named annotation |
|
public static boolean |
hasAnyAnnotation(AnnotatedNode node, java.lang.String names) Return true only if the node has any of the named annotations |
|
public static boolean |
isBinaryExpressionType(org.codehaus.groovy.ast.expr.Expression expression, java.lang.String token) Returns true if the expression is a binary expression with the specified token. |
|
public static boolean |
isBinaryExpressionType(org.codehaus.groovy.ast.expr.Expression expression, java.util.List<java.lang.String> tokens) Returns true if the expression is a binary expression with the specified token. |
|
public static boolean |
isBlock(Statement statement) Return true if the Statement is a block |
|
public static boolean |
isBoolean(org.codehaus.groovy.ast.expr.Expression expression) Tells you if the expression is either the true or false literal. |
|
public static boolean |
isClosureDeclaration(ASTNode expression) Returns true if the ASTNode is a declaration of a closure, either as a declaration or a field. |
|
public static boolean |
isConstant(org.codehaus.groovy.ast.expr.Expression expression, java.lang.Object expected) Tells you if an expression is the expected constant. |
|
public static boolean |
isConstantOrConstantLiteral(org.codehaus.groovy.ast.expr.Expression expression) Returns true if an expression is a constant or else a literal that contains only constant values. |
|
public static boolean |
isConstantOrLiteral(org.codehaus.groovy.ast.expr.Expression expression) Tells you if an expression is a constant or literal. |
|
public static boolean |
isConstructorCall(org.codehaus.groovy.ast.expr.Expression expression, java.util.List<java.lang.String> classNames) Return true if the expression is a constructor call on any of the named classes, with any number of parameters. |
|
public static boolean |
isConstructorCall(org.codehaus.groovy.ast.expr.Expression expression, java.lang.String classNamePattern) Return true if the expression is a constructor call on a class that matches the supplied. |
|
public static boolean |
isEmptyBlock(Statement origStatement) Return true if the Statement is a block and it is empty (contains no "meaningful" statements). |
|
public static boolean |
isFalse(org.codehaus.groovy.ast.expr.Expression expression) Tells you if the expression is the false expression, either literal or constant. |
|
public static boolean |
isFinalVariable(org.codehaus.groovy.ast.expr.DeclarationExpression declarationExpression, SourceCode sourceCode) Return true if the DeclarationExpression represents a 'final' variable declaration. |
|
public static boolean |
isFromGeneratedSourceCode(ASTNode node)
|
|
public static boolean |
isInstanceOfCheck(java.lang.Object expression) |
|
public static boolean |
isListLiteralWithOnlyConstantValues(org.codehaus.groovy.ast.expr.Expression expression) Returns true if a List literal that contains only entries that are constants. |
|
public static boolean |
isMapLiteralWithOnlyConstantValues(org.codehaus.groovy.ast.expr.Expression expression) Returns true if a Map literal that contains only entries where both key and value are constants. |
|
public static boolean |
isMethodCall(Statement stmt, java.lang.String methodObject, java.lang.String methodName, int numArguments) Return true only if the Statement represents a method call for the specified method object (receiver), method name, and with the specified number of arguments. |
|
public static boolean |
isMethodCall(MethodCallExpression methodCall, java.lang.String methodObjectPattern, java.lang.String methodPattern, int numArguments) Return true only if the MethodCallExpression represents a method call for the specified method object (receiver), method name, and with the specified number of arguments. |
|
public static boolean |
isMethodCall(org.codehaus.groovy.ast.expr.Expression expression, java.lang.String methodObject, java.lang.String methodName, int numArguments) Return true only if the expression is a MethodCallExpression representing a method call for the specified method object (receiver), method name, and with the specified number of arguments. |
|
public static boolean |
isMethodCall(org.codehaus.groovy.ast.expr.Expression expression, java.lang.String methodObjectPattern, java.lang.String methodNamePattern) Return true only if the expression represents a method call (MethodCallExpression) for the specified method object (receiver) and method name. |
|
public static boolean |
isMethodCall(MethodCallExpression methodCall, java.util.List<java.lang.String> methodObjects, java.util.List<java.lang.String> methodNames, java.lang.Integer numArguments) Return true only if the MethodCallExpression represents a method call for any one of the specified method objects (receivers) and any one of the method names. |
|
public static boolean |
isMethodCall(MethodCallExpression methodCall, java.util.List<java.lang.String> methodObjects, java.util.List<java.lang.String> methodNames) |
|
public static boolean |
isMethodCall(org.codehaus.groovy.ast.expr.Expression expression, java.lang.String methodName, int numArguments) Tells you if the expression is a method call for a certain method name with a certain number of arguments. |
|
public static boolean |
isMethodCall(org.codehaus.groovy.ast.expr.Expression expression, java.lang.String methodName, groovy.lang.Range numArguments) Tells you if the expression is a method call for a certain method name with a certain number of arguments. |
|
public static boolean |
isMethodCallOnObject(org.codehaus.groovy.ast.expr.Expression expression, java.lang.String methodObjectPattern) Tells you if the expression is a method call on a particular object (which is represented as a String). |
|
public static boolean |
isMethodNamed(MethodCallExpression methodCall, java.lang.String methodNamePattern, java.lang.Integer numArguments) Return true only if the MethodCallExpression represents a method call for the specified method name |
|
public static boolean |
isMethodNamed(MethodCallExpression methodCall, java.lang.String methodNamePattern) |
|
public static boolean |
isMethodNode(ASTNode node, java.lang.String methodNamePattern, java.lang.Integer numArguments, java.lang.Class returnType) Tells you if the ASTNode is a method node for the given name, arity, and return type. |
|
public static boolean |
isMethodNode(ASTNode node, java.lang.String methodNamePattern, java.lang.Integer numArguments) |
|
public static boolean |
isMethodNode(ASTNode node, java.lang.String methodNamePattern) |
|
public static boolean |
isNotNullCheck(java.lang.Object expression) |
|
public static boolean |
isNull(ASTNode expression) Tells you if the expression is the null literal. |
|
public static boolean |
isNullCheck(java.lang.Object expression) |
|
public static boolean |
isOneLiner(java.lang.Object statement) |
|
public static boolean |
isPropertyNamed(org.codehaus.groovy.ast.expr.Expression property, java.lang.Object expectedName) |
|
public static boolean |
isPublic(ASTNode node) Tells you if the ASTNode has a public modifier on it. |
|
public static boolean |
isSafe(org.codehaus.groovy.ast.expr.Expression expression) Tells you if the expression is a null safe dereference. |
|
public static boolean |
isSpreadSafe(org.codehaus.groovy.ast.expr.Expression expression) Tells you if the expression is a spread operator call |
|
public static boolean |
isSuperReference(org.codehaus.groovy.ast.expr.Expression expression) |
|
public static boolean |
isThisReference(org.codehaus.groovy.ast.expr.Expression expression) |
|
public static boolean |
isTrue(org.codehaus.groovy.ast.expr.Expression expression) Tells you if the expression is true, which can be true or Boolean.TRUE. |
|
public static boolean |
isVariable(ASTNode expression, java.lang.String pattern) Tells you if the given ASTNode is a VariableExpression with the given name. |
|
public static boolean |
respondsTo(java.lang.Object object, java.lang.String methodName) Return true only if the specified object responds to the named method |
Methods inherited from class | Name |
---|---|
class java.lang.Object |
java.lang.Object#wait(long, int), java.lang.Object#wait(), java.lang.Object#wait(long), java.lang.Object#equals(java.lang.Object), java.lang.Object#toString(), java.lang.Object#hashCode(), java.lang.Object#getClass(), java.lang.Object#notify(), java.lang.Object#notifyAll() |
This method tells you if a ClassNode implements or extends a certain class.
node
- the nodetarget
- the classGets the first non annotation line number of a node, taking into account annotations.
Return the AnnotationNode for the named annotation, or else null.
node
- - the AnnotatedNodename
- - the name of the annotationGets the argument names of a method call. If the arguments are not VariableExpressions then a null will be returned.
methodCall
- the method call to searchSupports discovering many common JDK types, but not all.
Supports discovering many common JDK types, but not all.
Return the List of Arguments for the specified MethodCallExpression or a ConstructorCallExpression. The returned List contains either ConstantExpression or MapEntryExpression objects.
methodCall
- - the AST MethodCallExpression or ConstructorCalLExpressionGets the parameter names of a method node.
node
- the node to search parameter names onReturn the List of VariableExpression objects referenced by the specified DeclarationExpression.
declarationExpression
- - the DeclarationExpressionReturn true only if the node has the named annotation
node
- - the AST Node to checkname
- - the name of the annotationReturn true only if the node has any of the named annotations
node
- - the AST Node to checknames
- - the names of the annotationsReturns true if the expression is a binary expression with the specified token.
expression
- expressiontoken
- tokenReturns true if the expression is a binary expression with the specified token.
expression
- - the expression nodetokens
- - the List of allowable (operator) tokensReturn true if the Statement is a block
statement
- - the Statement to checkTells you if the expression is either the true or false literal.
expression
- expressionReturns true if the ASTNode is a declaration of a closure, either as a declaration or a field.
expression
- the target expressionTells you if an expression is the expected constant.
expression
- any expressionexpected
- the expected int or StringReturns true if an expression is a constant or else a literal that contains only constant values. Basically, is it a constant, or else a map like [a:1, b:99, c:true], or a list like ['abc', 99.0, false]
expression
- - any expressionTells you if an expression is a constant or literal. Basically, is it a map, list, constant, or a predefined constant like true/false.
expression
- any expressionReturn true if the expression is a constructor call on any of the named classes, with any number of parameters.
expression
- - the expressionclassNames
- - the possible List of class namesReturn true if the expression is a constructor call on a class that matches the supplied.
expression
- - the expressionclassNamePattern
- - the possible List of class namesReturn true if the Statement is a block and it is empty (contains no "meaningful" statements). This implementation also addresses some "weirdness" around some statement types (specifically finally) where the BlockStatement answered false to isEmpty() even if it was.
origStatement
- - the Statement to checkTells you if the expression is the false expression, either literal or constant.
expression
- expressionReturn true if the DeclarationExpression represents a 'final' variable declaration. NOTE: THIS IS A WORKAROUND. There does not seem to be an easy way to determine whether the 'final' modifier has been specified for a variable declaration. Return true if the 'final' is present before the variable name.
Returns true if a List literal that contains only entries that are constants.
expression
- - any expressionReturns true if a Map literal that contains only entries where both key and value are constants.
expression
- - any expressionReturn true only if the Statement represents a method call for the specified method object (receiver), method name, and with the specified number of arguments.
stmt
- - the AST StatementmethodObject
- - the name of the method object (receiver)methodName
- - the name of the method being callednumArguments
- - the number of arguments passed into the methodReturn true only if the MethodCallExpression represents a method call for the specified method object (receiver), method name, and with the specified number of arguments.
methodCall
- - the AST MethodCallExpressionmethodObjectPattern
- - the name of the method object (receiver)methodPattern
- - the name of the method being callednumArguments
- - the number of arguments passed into the methodReturn true only if the expression is a MethodCallExpression representing a method call for the specified method object (receiver), method name, and with the specified number of arguments.
expression
- - the AST expressionmethodObject
- - the name of the method object (receiver)methodName
- - the name of the method being callednumArguments
- - the number of arguments passed into the methodReturn true only if the expression represents a method call (MethodCallExpression) for the specified method object (receiver) and method name.
expression
- - the AST expression to be checkedmethodObjectPattern
- - the name of the method object (receiver)methodNamePattern
- - the name of the method being calledReturn true only if the MethodCallExpression represents a method call for any one of the specified method objects (receivers) and any one of the method names. Optionally, you can restrict it to a method call with a certain number of arguments.
methodCall
- the method call objectmethodObjects
- a list of receivers, such as ['this', 'super']methodNames
- a list of method namesnumArguments
- optionally, require a certain number of argumentsTells you if the expression is a method call for a certain method name with a certain number of arguments.
expression
- the (potentially) method callmethodName
- the name of the method expectednumArguments
- number of expected argumentsTells you if the expression is a method call for a certain method name with a certain number of arguments.
expression
- the (potentially) method callmethodName
- the name of the method expectednumArguments
- number of expected argumentsTells you if the expression is a method call on a particular object (which is represented as a String). For instance, you may ask isMethodCallOnObject(e, 'this') to find a this reference.
expression
- - the expressionmethodObjectPattern
- - the name of the method object (receiver) such as 'this'Return true only if the MethodCallExpression represents a method call for the specified method name
methodCall
- - the AST MethodCallExpressionmethodNamePattern
- - the expected name of the method being callednumArguments
- - The number of expected argumentsTells you if the ASTNode is a method node for the given name, arity, and return type.
node
- the node to inspectmethodNamePattern
- the expected name of the methodnumArguments
- the expected number of arguments, optionalreturnType
- the expected return type, optionalTells you if the expression is the null literal.
expression
- expression.Tells you if the ASTNode has a public modifier on it. If the node does not have modifiers at all (like a variable expression) then false is returned.
node
- node to queryTells you if the expression is a null safe dereference.
expression
- expressionTells you if the expression is a spread operator call
expression
- expressionTells you if the expression is true, which can be true or Boolean.TRUE.
expression
- expressionTells you if the given ASTNode is a VariableExpression with the given name.
expression
- any AST Nodepattern
- a string pattern to matchReturn true only if the specified object responds to the named method
object
- - the object to checkmethodName
- - the name of the method