org.gmetrics.util
Class AstUtil

java.lang.Object
  extended by org.gmetrics.util.AstUtil
All Implemented Interfaces:
groovy.lang.GroovyObject

public class AstUtil
extends java.lang.Object
implements groovy.lang.GroovyObject

Contains static utility methods related to Groovy AST.

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

Version:
$Revision: 24 $ - $Date: 2009-12-10 21:17:05 -0500 (Thu, 10 Dec 2009) $
Author:
Chris Mair

Method Summary
static org.codehaus.groovy.ast.AnnotationNode getAnnotation(org.codehaus.groovy.ast.AnnotatedNode node, java.lang.String name)
          Return the AnnotationNode for the named annotation, or else null.
 groovy.lang.MetaClass getMetaClass()
           
static java.util.List getMethodArguments(org.codehaus.groovy.ast.expr.MethodCallExpression methodCall)
          Return the List of Arguments for the specified MethodCallExpression.
 java.lang.Object getProperty(java.lang.String name)
           
static java.util.List getVariableExpressions(org.codehaus.groovy.ast.expr.DeclarationExpression declarationExpression)
          Return the List of VariableExpression objects referenced by the specified DeclarationExpression.
 java.lang.Object invokeMethod(java.lang.String name, java.lang.Object args)
           
static boolean isBlock(org.codehaus.groovy.ast.stmt.Statement statement)
          Return true if the Statement is a block
static boolean isEmptyBlock(org.codehaus.groovy.ast.stmt.Statement statement)
          Return true if the Statement is a block and it is empty (contains no "meaningful" statements).
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)
           
static boolean isMethodCall(org.codehaus.groovy.ast.expr.MethodCallExpression methodCall, java.lang.String methodObject, java.lang.String methodName)
          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, java.lang.String methodObject, java.lang.String methodName, 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.stmt.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.
static boolean isMethodNamed(org.codehaus.groovy.ast.expr.MethodCallExpression methodCall, java.lang.String methodName)
          Return true only if the MethodCallExpression represents a method call for the specified method name
 void setMetaClass(groovy.lang.MetaClass metaClass)
           
 void setProperty(java.lang.String name, java.lang.Object value)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

isBlock

public 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

isEmptyBlock

public static boolean isEmptyBlock(org.codehaus.groovy.ast.stmt.Statement statement)
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

getMethodArguments

public static java.util.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

isMethodCall

public static boolean isMethodCall(org.codehaus.groovy.ast.stmt.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.

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

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

public static boolean isMethodCall(org.codehaus.groovy.ast.expr.MethodCallExpression methodCall,
                                   java.lang.String methodObject,
                                   java.lang.String methodName)
Return true only if the MethodCallExpression represents a method call for the specified method object (receiver) and method name.

Parameters:
methodCall - - the AST MethodCallExpression
methodObject - - the name of the method object (receiver)
methodName - - the name of the method being called
Returns:
true only if the method call matches the specified criteria

isMethodNamed

public static boolean isMethodNamed(org.codehaus.groovy.ast.expr.MethodCallExpression methodCall,
                                    java.lang.String methodName)
Return true only if the MethodCallExpression represents a method call for the specified method name

Parameters:
methodCall - - the AST MethodCallExpression
methodName - - the expected name of the method being called
Returns:
true only if the method call name matches

getAnnotation

public static org.codehaus.groovy.ast.AnnotationNode getAnnotation(org.codehaus.groovy.ast.AnnotatedNode node,
                                                                   java.lang.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

getVariableExpressions

public static java.util.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

isFinalVariable

public 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

public 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.

getMetaClass

public groovy.lang.MetaClass getMetaClass()
Specified by:
getMetaClass in interface groovy.lang.GroovyObject

setMetaClass

public void setMetaClass(groovy.lang.MetaClass metaClass)
Specified by:
setMetaClass in interface groovy.lang.GroovyObject

invokeMethod

public java.lang.Object invokeMethod(java.lang.String name,
                                     java.lang.Object args)
Specified by:
invokeMethod in interface groovy.lang.GroovyObject

getProperty

public java.lang.Object getProperty(java.lang.String name)
Specified by:
getProperty in interface groovy.lang.GroovyObject

setProperty

public void setProperty(java.lang.String name,
                        java.lang.Object value)
Specified by:
setProperty in interface groovy.lang.GroovyObject


Copyright © 2010. All Rights Reserved.