Groovy Documentation

org.codenarc.util
[Groovy] Class AstUtil

java.lang.Object
  org.codenarc.util.AstUtil

@SuppressWarnings(['DuplicateLiteral', 'MethodCount'])
class AstUtil

Contains static utility methods related to Groovy AST.

This is an internal class and its API is subject to change.

Authors:
Chris Mair
Hamlet D'Arcy
Version:
\$Revision: 609 \$ - \$Date: 2011-02-24 13:48:43 -0500 (Thu, 24 Feb 2011) \$


Method Summary
static boolean classNodeImplementsType(org.codehaus.groovy.ast.ClassNode node, Class target)

This method tells you if a ClassNode implements or extends a certain class.

static org.codehaus.groovy.ast.AnnotationNode getAnnotation(org.codehaus.groovy.ast.AnnotatedNode node, String name)

Return the AnnotationNode for the named annotation, or else null.

static List getArgumentNames(org.codehaus.groovy.ast.expr.MethodCallExpression methodCall)

Gets the argument names of a method call.

static org.codehaus.groovy.ast.ASTNode getEmptyBlock(org.codehaus.groovy.ast.stmt.Statement origStatement)

static Class getFieldType(org.codehaus.groovy.ast.ClassNode node, String fieldName)

Supports discovering many common JDK types, but not all.

static Class getFieldType(org.codehaus.groovy.ast.FieldNode field)

Supports discovering many common JDK types, but not all.

static String getInstanceOfTarget(def expression)

static List getMethodArguments(org.codehaus.groovy.ast.expr.MethodCallExpression methodCall)

Return the List of Arguments for the specified MethodCallExpression.

static String getNullComparisonTarget(def expression)

static List getParameterNames(org.codehaus.groovy.ast.MethodNode node)

Gets the parameter names of a method node.

static List getVariableExpressions(org.codehaus.groovy.ast.expr.DeclarationExpression declarationExpression)

Return the List of VariableExpression objects referenced by the specified DeclarationExpression.

static boolean isBinaryExpressionType(org.codehaus.groovy.ast.expr.Expression expression, String token)

Returns true if the expression is a binary expression with the specified token.

static boolean isBlock(org.codehaus.groovy.ast.stmt.Statement statement)

Return true if the Statement is a block

static boolean isBoolean(org.codehaus.groovy.ast.expr.Expression expression)

Tells you if the expression is either the true or false literal.

static boolean isClosureDeclaration(org.codehaus.groovy.ast.ASTNode expression)

Returns true if the ASTNode is a declaration of a closure, either as a declaration or a field.

static boolean isConstant(org.codehaus.groovy.ast.expr.Expression expression, int expected)

Tells you if an expression is the constant integer.

static boolean isConstantOrLiteral(org.codehaus.groovy.ast.expr.Expression expression)

Tells you if an expression is a constant or literal.

static boolean isEmptyBlock(org.codehaus.groovy.ast.stmt.Statement origStatement)

Return true if the Statement is a block and it is empty (contains no "meaningful" statements).

static boolean isFalse(org.codehaus.groovy.ast.expr.Expression expression)

Tells you if the expression is the false expression, either literal or contant.

static boolean isFinalVariable(org.codehaus.groovy.ast.expr.DeclarationExpression declarationExpression, SourceCode sourceCode)

Return true if the DeclarationExpression represents a 'final' variable declaration.

static boolean isFromGeneratedSourceCode(org.codehaus.groovy.ast.ASTNode node)

@return true if the ASTNode was generated (synthetic) rather than from the "real" input source code.

static boolean isInstanceOfCheck(def expression)

static boolean isMethodCall(org.codehaus.groovy.ast.stmt.Statement stmt, String methodObject, 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.

static boolean isMethodCall(org.codehaus.groovy.ast.expr.MethodCallExpression methodCall, String methodObject, 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.

static boolean isMethodCall(org.codehaus.groovy.ast.expr.Expression expression, String methodObject, 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.

static boolean isMethodCall(org.codehaus.groovy.ast.expr.MethodCallExpression methodCall, String methodObjectPattern, String methodNamePattern)

Return true only if the MethodCallExpression represents a method call for the specified method object (receiver) and method name.

static boolean isMethodCall(org.codehaus.groovy.ast.expr.MethodCallExpression methodCall, List methodObjects, List methodNames, def numArguments = null)

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.

static boolean isMethodCall(org.codehaus.groovy.ast.expr.Expression expression, String methodName, int numArguments)

Tells you if the expression is a method call for a certain method name with a certain number of arguments.

static boolean isMethodCallOnObject(org.codehaus.groovy.ast.expr.Expression expression, String methodObjectPattern)

Tells you if the expression is a method call on a particual object (which is represented as a String).

static boolean isMethodNamed(org.codehaus.groovy.ast.expr.MethodCallExpression methodCall, String methodNamePattern, Integer numArguments = null)

Return true only if the MethodCallExpression represents a method call for the specified method name

static boolean isMethodNode(org.codehaus.groovy.ast.ASTNode node, String methodName, Integer numArguments = null, Class returnType = null)

Tells you if the ASTNode is a method node for the given name, arity, and return type.

static boolean isNotNullCheck(def expression)

static boolean isNull(org.codehaus.groovy.ast.ASTNode expression)

Tells you if the expression is the null literal.

static boolean isPublic(org.codehaus.groovy.ast.ASTNode node)

Tells you if the ASTNode has a public modifier on it.

static boolean isSafe(org.codehaus.groovy.ast.expr.Expression expression)

Tells you if the expression is a null safe dereference.

static boolean isSpreadSafe(org.codehaus.groovy.ast.expr.Expression expression)

Tells you if the expression is a spread operator call

static boolean isTrue(org.codehaus.groovy.ast.expr.Expression expression)

Tells you if the expression is true, which can be true or Boolean.TRUE.

static boolean isVariable(org.codehaus.groovy.ast.ASTNode expression, String name)

Tells you if the given ASTNode is a VariableExpression with the given name.

static boolean respondsTo(Object object, String methodName)

Return true only if the specified object responds to the named method

 
Methods inherited from class Object
wait, wait, wait, equals, toString, hashCode, getClass, notify, notifyAll
 

Method Detail

classNodeImplementsType

static boolean classNodeImplementsType(org.codehaus.groovy.ast.ClassNode node, Class target)
This method tells you if a ClassNode implements or extends a certain class.
Parameters:
node - the node
target - the class
Returns:
true if the class node 'is a' target


getAnnotation

static org.codehaus.groovy.ast.AnnotationNode getAnnotation(org.codehaus.groovy.ast.AnnotatedNode node, String name)
Return the AnnotationNode for the named annotation, or else null. Supports Groovy 1.5 and Groovy 1.6.
Parameters:
node - - the AnnotatedNode
name - - the name of the annotation
Returns:
the AnnotationNode or else null


getArgumentNames

static List getArgumentNames(org.codehaus.groovy.ast.expr.MethodCallExpression methodCall)
Gets the argument names of a method call. If the arguments are not VariableExpressions then a null will be returned.
Parameters:
methodCall - the method call to search
Returns:
a list of strings, never null, but some elements may be null


getEmptyBlock

static org.codehaus.groovy.ast.ASTNode getEmptyBlock(org.codehaus.groovy.ast.stmt.Statement origStatement)


getFieldType

static Class getFieldType(org.codehaus.groovy.ast.ClassNode node, String fieldName)
Supports discovering many common JDK types, but not all.
Parameters:
node
fieldName
Returns:


getFieldType

static Class getFieldType(org.codehaus.groovy.ast.FieldNode field)
Supports discovering many common JDK types, but not all.


getInstanceOfTarget

static String getInstanceOfTarget(def expression)


getMethodArguments

static List getMethodArguments(org.codehaus.groovy.ast.expr.MethodCallExpression methodCall)
Return the List of Arguments for the specified MethodCallExpression. The returned List contains either ConstantExpression or MapEntryExpression objects.
Parameters:
methodCall - - the AST MethodCallExpression
Returns:
the List of argument objects


getNullComparisonTarget

static String getNullComparisonTarget(def expression)


getParameterNames

static List getParameterNames(org.codehaus.groovy.ast.MethodNode node)
Gets the parameter names of a method node.
Parameters:
node - the node to search parameter names on
Returns:
argument names, never null


getVariableExpressions

static List getVariableExpressions(org.codehaus.groovy.ast.expr.DeclarationExpression declarationExpression)
Return the List of VariableExpression objects referenced by the specified DeclarationExpression.
Parameters:
declarationExpression - - the DeclarationExpression
Returns:
the List of VariableExpression objects


isBinaryExpressionType

static boolean isBinaryExpressionType(org.codehaus.groovy.ast.expr.Expression expression, String token)
Returns true if the expression is a binary expression with the specified token.
Parameters:
expression - expression
token - token
Returns:
as described


isBlock

static boolean isBlock(org.codehaus.groovy.ast.stmt.Statement statement)
Return true if the Statement is a block
Parameters:
statement - - the Statement to check
Returns:
true if the Statement is a block


isBoolean

static boolean isBoolean(org.codehaus.groovy.ast.expr.Expression expression)
Tells you if the expression is either the true or false literal.
Parameters:
expression - expression
Returns:
as described


isClosureDeclaration

static boolean isClosureDeclaration(org.codehaus.groovy.ast.ASTNode expression)
Returns true if the ASTNode is a declaration of a closure, either as a declaration or a field.
Parameters:
expression - the target expression
Returns:
as described


isConstant

static boolean isConstant(org.codehaus.groovy.ast.expr.Expression expression, int expected)
Tells you if an expression is the constant integer.
Parameters:
expression - any expression
expected - the expected int
Returns:
as described


isConstantOrLiteral

static boolean isConstantOrLiteral(org.codehaus.groovy.ast.expr.Expression expression)
Tells you if an expression is a constant or literal. Basically, is it a map, list, constant, or a predefined constant like true/false.
Parameters:
expression - any expression
Returns:
as described


isEmptyBlock

static boolean isEmptyBlock(org.codehaus.groovy.ast.stmt.Statement origStatement)
Return 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.
Parameters:
statement - - the Statement to check
Returns:
true if the BlockStatement is empty


isFalse

static boolean isFalse(org.codehaus.groovy.ast.expr.Expression expression)
Tells you if the expression is the false expression, either literal or contant.
Parameters:
expression - expression
Returns:
as described


isFinalVariable

static boolean isFinalVariable(org.codehaus.groovy.ast.expr.DeclarationExpression declarationExpression, SourceCode sourceCode)
Return 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.


isFromGeneratedSourceCode

static boolean isFromGeneratedSourceCode(org.codehaus.groovy.ast.ASTNode node)
Returns:
true if the ASTNode was generated (synthetic) rather than from the "real" input source code.


isInstanceOfCheck

static boolean isInstanceOfCheck(def expression)


isMethodCall

static boolean isMethodCall(org.codehaus.groovy.ast.stmt.Statement stmt, String methodObject, 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.
Parameters:
stmt - - the AST Statement
methodObject - - the name of the method object (receiver)
methodName - - the name of the method being called
numArguments - - the number of arguments passed into the method
Returns:
true only if the Statement is a method call matching the specified criteria


isMethodCall

static boolean isMethodCall(org.codehaus.groovy.ast.expr.MethodCallExpression methodCall, String methodObject, 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.
Parameters:
methodCall - - the AST MethodCallExpression
methodObject - - the name of the method object (receiver)
methodPattern - - the name of the method being called
numArguments - - the number of arguments passed into the method
Returns:
true only if the method call matches the specified criteria


isMethodCall

static boolean isMethodCall(org.codehaus.groovy.ast.expr.Expression expression, String methodObject, 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.
Parameters:
expression - - the AST expression
methodObject - - the name of the method object (receiver)
methodName - - the name of the method being called
numArguments - - the number of arguments passed into the method
Returns:
true only if the method call matches the specified criteria


isMethodCall

static boolean isMethodCall(org.codehaus.groovy.ast.expr.MethodCallExpression methodCall, String methodObjectPattern, String methodNamePattern)
Return true only if the MethodCallExpression represents a method call for the specified method object (receiver) and method name.
Parameters:
methodCall - - the AST MethodCallExpression
methodObjectPattern - - the name of the method object (receiver)
methodNamePattern - - the name of the method being called
Returns:
true only if the method call matches the specified criteria


isMethodCall

static boolean isMethodCall(org.codehaus.groovy.ast.expr.MethodCallExpression methodCall, List methodObjects, List methodNames, def numArguments = null)
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. Optionally, you can restrict it to a method call with a certain number of arguments.
Parameters:
methodCall - the method call object
methodObjects - a list of receivers, such as ['this', 'super']
methodNames - a list of method names
numArguments - optionally, require a certain number of arguments
Returns:
as described


isMethodCall

static boolean isMethodCall(org.codehaus.groovy.ast.expr.Expression expression, String methodName, int numArguments)
Tells you if the expression is a method call for a certain method name with a certain number of arguments.
Parameters:
expression - the (potentially) method call
methodName - the name of the method expected
numArguments - number of expected arguments
Returns:
as described


isMethodCallOnObject

static boolean isMethodCallOnObject(org.codehaus.groovy.ast.expr.Expression expression, String methodObjectPattern)
Tells you if the expression is a method call on a particual object (which is represented as a String). For instance, you may ask isMethodCallOnObject(e, 'this') to find a this reference.
Parameters:
expression - the expression
methodObjectPattern
methodObjectPattern - - the name of the method object (receiver) such as 'this'
Returns:
as described


isMethodNamed

static boolean isMethodNamed(org.codehaus.groovy.ast.expr.MethodCallExpression methodCall, String methodNamePattern, Integer numArguments = null)
Return true only if the MethodCallExpression represents a method call for the specified method name
Parameters:
methodCall - - the AST MethodCallExpression
methodNamePattern - - the expected name of the method being called
Returns:
true only if the method call name matches


isMethodNode

static boolean isMethodNode(org.codehaus.groovy.ast.ASTNode node, String methodName, Integer numArguments = null, Class returnType = null)
Tells you if the ASTNode is a method node for the given name, arity, and return type.
Parameters:
node - the node to inspect
methodName - the expected name of the method
numArguments - the expected number of arguments, optional
returnType - the expected return type, optional
Returns:
true if this node is a MethodNode meeting the parameters. false otherwise


isNotNullCheck

static boolean isNotNullCheck(def expression)


isNull

static boolean isNull(org.codehaus.groovy.ast.ASTNode expression)
Tells you if the expression is the null literal.
Parameters:
expression - expression.
Returns:
as described


isPublic

static boolean isPublic(org.codehaus.groovy.ast.ASTNode node)
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.
Parameters:
node - node to query
Returns:
true if definitely public, false if not public or unknown


isSafe

static boolean isSafe(org.codehaus.groovy.ast.expr.Expression expression)
Tells you if the expression is a null safe dereference.
Parameters:
expression - expression
Returns:
true if is null safe dereference.


isSpreadSafe

static boolean isSpreadSafe(org.codehaus.groovy.ast.expr.Expression expression)
Tells you if the expression is a spread operator call
Parameters:
expression - expression
Returns:
true if is spread expression


isTrue

static boolean isTrue(org.codehaus.groovy.ast.expr.Expression expression)
Tells you if the expression is true, which can be true or Boolean.TRUE.
Parameters:
expression - expression
Returns:
as described


isVariable

static boolean isVariable(org.codehaus.groovy.ast.ASTNode expression, String name)
Tells you if the given ASTNode is a VariableExpression with the given name.
Parameters:
expression - any AST Node
name - a string name
Returns:
true if the node is a variable with the specified name


respondsTo

static boolean respondsTo(Object object, String methodName)
Return true only if the specified object responds to the named method
Parameters:
object - - the object to check
methodName - - the name of the method
Returns:
true if the object responds to the named method


 

Groovy Documentation