Groovy Documentation

org.codehaus.groovy.grails.compiler.injection
[Java] Class GrailsASTUtils

java.lang.Object
  org.codehaus.groovy.grails.compiler.injection.GrailsASTUtils

public class GrailsASTUtils
extends java.lang.Object

Helper methods for working with Groovy AST trees.

Authors:
Graeme Rocher
Since:
0.3


Field Summary
static Token ASSIGNMENT_OPERATOR

static Token EQUALS_OPERATOR

static java.lang.String METHOD_MISSING_METHOD_NAME

static org.codehaus.groovy.ast.ClassNode MISSING_METHOD_EXCEPTION

static Token NOT_EQUALS_OPERATOR

static org.codehaus.groovy.ast.expr.ConstantExpression NULL_EXPRESSION

static java.lang.String STATIC_METHOD_MISSING_METHOD_NAME

 
Method Summary
static void addAnnotationIfNecessary(org.codehaus.groovy.ast.ClassNode classNode, java.lang.Class entityClass)

static void addDelegateConstructor(org.codehaus.groovy.ast.ClassNode classNode, org.codehaus.groovy.ast.MethodNode constructorMethod)

Adds or modifies an existing constructor to delegate to the given static constructor method for initialization logic.

static org.codehaus.groovy.ast.MethodNode addDelegateInstanceMethod(org.codehaus.groovy.ast.ClassNode classNode, org.codehaus.groovy.ast.expr.Expression delegate, org.codehaus.groovy.ast.MethodNode declaredMethod)

Adds a delegate method to the target class node where the first argument is to the delegate method is 'this'.

static org.codehaus.groovy.ast.MethodNode addDelegateInstanceMethod(org.codehaus.groovy.ast.ClassNode classNode, org.codehaus.groovy.ast.expr.Expression delegate, org.codehaus.groovy.ast.MethodNode declaredMethod, boolean thisAsFirstArgument)

Adds a delegate method to the target class node where the first argument is to the delegate method is 'this'.

static void addDelegateInstanceMethods(org.codehaus.groovy.ast.ClassNode classNode, org.codehaus.groovy.ast.ClassNode delegateNode, org.codehaus.groovy.ast.expr.Expression delegateInstance)

static void addDelegateInstanceMethods(org.codehaus.groovy.ast.ClassNode supportedSuperType, org.codehaus.groovy.ast.ClassNode classNode, org.codehaus.groovy.ast.ClassNode delegateNode, org.codehaus.groovy.ast.expr.Expression delegateInstance)

static org.codehaus.groovy.ast.MethodNode addDelegateStaticMethod(org.codehaus.groovy.ast.ClassNode classNode, org.codehaus.groovy.ast.MethodNode delegateMethod)

Adds a static method call to given class node that delegates to the given method

static org.codehaus.groovy.ast.MethodNode addDelegateStaticMethod(org.codehaus.groovy.ast.expr.Expression expression, org.codehaus.groovy.ast.ClassNode classNode, org.codehaus.groovy.ast.MethodNode delegateMethod)

Adds a static method to the given class node that delegates to the given method and resolves the object to invoke the method on from the given expression.

static void addFieldIfNonExistent(org.codehaus.groovy.ast.ClassNode classNode, org.codehaus.groovy.ast.ClassNode fieldType, java.lang.String fieldName)

static void addMethodIfNotPresent(org.codehaus.groovy.ast.ClassNode controllerClassNode, org.codehaus.groovy.ast.MethodNode methodNode)

static org.codehaus.groovy.ast.expr.ArgumentListExpression createArgumentListFromParameters(org.codehaus.groovy.ast.Parameter[] parameterTypes, boolean thisAsFirstArgument)

Creates an argument list from the given parameter types.

static org.codehaus.groovy.ast.stmt.ExpressionStatement createPrintlnStatement(java.lang.String message)

static org.codehaus.groovy.ast.stmt.ExpressionStatement createPrintlnStatement(java.lang.String message, java.lang.String variable)

static org.codehaus.groovy.ast.AnnotationNode findAnnotation(org.codehaus.groovy.ast.ClassNode annotationClassNode, java.util.List annotations)

static org.codehaus.groovy.ast.ConstructorNode findConstructor(org.codehaus.groovy.ast.ClassNode classNode, org.codehaus.groovy.ast.Parameter[] constructorParams)

Finds a constructor for the given class node and parameter types

static org.codehaus.groovy.ast.ConstructorNode getDefaultConstructor(org.codehaus.groovy.ast.ClassNode classNode)

Obtains the default constructor for the given class node.

static java.lang.String getFullName(org.codehaus.groovy.ast.ClassNode classNode)

Gets the full name of a ClassNode.

static org.codehaus.groovy.ast.ClassNode getFurthestParent(org.codehaus.groovy.ast.ClassNode classNode)

static org.codehaus.groovy.ast.ClassNode getFurthestUnresolvedParent(org.codehaus.groovy.ast.ClassNode classNode)

static org.codehaus.groovy.ast.Parameter[] getRemainingParameterTypes(org.codehaus.groovy.ast.Parameter[] parameters)

Gets the remaining parameters excluding the first parameter in the given list

static boolean hasOrInheritsProperty(org.codehaus.groovy.ast.ClassNode classNode, java.lang.String propertyName)

static boolean hasProperty(org.codehaus.groovy.ast.ClassNode classNode, java.lang.String propertyName)

Returns whether a classNode has the specified property or not

static boolean implementsOrInheritsZeroArgMethod(org.codehaus.groovy.ast.ClassNode classNode, java.lang.String methodName, java.util.List ignoreClasses)

static boolean implementsZeroArgMethod(org.codehaus.groovy.ast.ClassNode classNode, java.lang.String methodName)

Tests whether the ClasNode implements the specified method name.

static boolean isCandidateInstanceMethod(org.codehaus.groovy.ast.ClassNode classNode, org.codehaus.groovy.ast.MethodNode declaredMethod)

static boolean isCandidateMethod(org.codehaus.groovy.ast.MethodNode declaredMethod)

static boolean isConstructorMethod(org.codehaus.groovy.ast.MethodNode declaredMethod)

static org.codehaus.groovy.ast.ClassNode nonGeneric(org.codehaus.groovy.ast.ClassNode type)

static boolean parametersEqual(org.codehaus.groovy.ast.Parameter[] a, org.codehaus.groovy.ast.Parameter[] b)

@return true if the two arrays are of the same size and have the same contents

static void wrapMethodBodyInTryCatchDebugStatements(org.codehaus.groovy.ast.MethodNode methodNode)

Wraps a method body in try / catch logic that catches any errors and logs an error, but does not rethrow!

 
Methods inherited from class java.lang.Object
java.lang.Object#wait(), java.lang.Object#wait(long), java.lang.Object#wait(long, int), 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()
 

Field Detail

ASSIGNMENT_OPERATOR

public static final Token ASSIGNMENT_OPERATOR


EQUALS_OPERATOR

public static final Token EQUALS_OPERATOR


METHOD_MISSING_METHOD_NAME

public static final java.lang.String METHOD_MISSING_METHOD_NAME


MISSING_METHOD_EXCEPTION

public static final org.codehaus.groovy.ast.ClassNode MISSING_METHOD_EXCEPTION


NOT_EQUALS_OPERATOR

public static final Token NOT_EQUALS_OPERATOR


NULL_EXPRESSION

public static final org.codehaus.groovy.ast.expr.ConstantExpression NULL_EXPRESSION


STATIC_METHOD_MISSING_METHOD_NAME

public static final java.lang.String STATIC_METHOD_MISSING_METHOD_NAME


 
Method Detail

addAnnotationIfNecessary

public static void addAnnotationIfNecessary(org.codehaus.groovy.ast.ClassNode classNode, @SuppressWarnings("unused") java.lang.Class entityClass)


addDelegateConstructor

public static void addDelegateConstructor(org.codehaus.groovy.ast.ClassNode classNode, org.codehaus.groovy.ast.MethodNode constructorMethod)
Adds or modifies an existing constructor to delegate to the given static constructor method for initialization logic.
Parameters:
classNode - The class node
constructorMethod - The constructor static method


addDelegateInstanceMethod

public static org.codehaus.groovy.ast.MethodNode addDelegateInstanceMethod(org.codehaus.groovy.ast.ClassNode classNode, org.codehaus.groovy.ast.expr.Expression delegate, org.codehaus.groovy.ast.MethodNode declaredMethod)
Adds a delegate method to the target class node where the first argument is to the delegate method is 'this'. In other words a method such as foo(Object instance, String bar) would be added with a signature of foo(String) and 'this' is passed to the delegate instance
Parameters:
classNode - The class node
delegate - The expression that looks up the delegate
declaredMethod - The declared method
Returns:
The added method node or null if it couldn't be added


addDelegateInstanceMethod

public static org.codehaus.groovy.ast.MethodNode addDelegateInstanceMethod(org.codehaus.groovy.ast.ClassNode classNode, org.codehaus.groovy.ast.expr.Expression delegate, org.codehaus.groovy.ast.MethodNode declaredMethod, boolean thisAsFirstArgument)
Adds a delegate method to the target class node where the first argument is to the delegate method is 'this'. In other words a method such as foo(Object instance, String bar) would be added with a signature of foo(String) and 'this' is passed to the delegate instance
Parameters:
classNode - The class node
delegate - The expression that looks up the delegate
declaredMethod - The declared method
thisAsFirstArgument - Whether 'this' should be passed as the first argument to the method
Returns:
The added method node or null if it couldn't be added


addDelegateInstanceMethods

public static void addDelegateInstanceMethods(org.codehaus.groovy.ast.ClassNode classNode, org.codehaus.groovy.ast.ClassNode delegateNode, org.codehaus.groovy.ast.expr.Expression delegateInstance)


addDelegateInstanceMethods

public static void addDelegateInstanceMethods(org.codehaus.groovy.ast.ClassNode supportedSuperType, org.codehaus.groovy.ast.ClassNode classNode, org.codehaus.groovy.ast.ClassNode delegateNode, org.codehaus.groovy.ast.expr.Expression delegateInstance)


addDelegateStaticMethod

public static org.codehaus.groovy.ast.MethodNode addDelegateStaticMethod(org.codehaus.groovy.ast.ClassNode classNode, org.codehaus.groovy.ast.MethodNode delegateMethod)
Adds a static method call to given class node that delegates to the given method
Parameters:
classNode - The class node
delegateMethod - The delegate method
Returns:
The added method node or null if it couldn't be added


addDelegateStaticMethod

public static org.codehaus.groovy.ast.MethodNode addDelegateStaticMethod(org.codehaus.groovy.ast.expr.Expression expression, org.codehaus.groovy.ast.ClassNode classNode, org.codehaus.groovy.ast.MethodNode delegateMethod)
Adds a static method to the given class node that delegates to the given method and resolves the object to invoke the method on from the given expression.
Parameters:
expression - The expression
classNode - The class node
delegateMethod - The delegate method
Returns:
The added method node or null if it couldn't be added


addFieldIfNonExistent

public static void addFieldIfNonExistent(org.codehaus.groovy.ast.ClassNode classNode, org.codehaus.groovy.ast.ClassNode fieldType, java.lang.String fieldName)


addMethodIfNotPresent

public static void addMethodIfNotPresent(org.codehaus.groovy.ast.ClassNode controllerClassNode, org.codehaus.groovy.ast.MethodNode methodNode)


createArgumentListFromParameters

public static org.codehaus.groovy.ast.expr.ArgumentListExpression createArgumentListFromParameters(org.codehaus.groovy.ast.Parameter[] parameterTypes, boolean thisAsFirstArgument)
Creates an argument list from the given parameter types.
Parameters:
parameterTypes - The parameter types
thisAsFirstArgument - Whether to include a reference to 'this' as the first argument
Returns:
the arguments


createPrintlnStatement

public static org.codehaus.groovy.ast.stmt.ExpressionStatement createPrintlnStatement(java.lang.String message)


createPrintlnStatement

public static org.codehaus.groovy.ast.stmt.ExpressionStatement createPrintlnStatement(java.lang.String message, java.lang.String variable)


findAnnotation

public static org.codehaus.groovy.ast.AnnotationNode findAnnotation(org.codehaus.groovy.ast.ClassNode annotationClassNode, java.util.List annotations)


findConstructor

public static org.codehaus.groovy.ast.ConstructorNode findConstructor(org.codehaus.groovy.ast.ClassNode classNode, org.codehaus.groovy.ast.Parameter[] constructorParams)
Finds a constructor for the given class node and parameter types
Parameters:
classNode - The class node
constructorParams - The parameter types
Returns:
The located constructor or null


getDefaultConstructor

public static org.codehaus.groovy.ast.ConstructorNode getDefaultConstructor(org.codehaus.groovy.ast.ClassNode classNode)
Obtains the default constructor for the given class node.
Parameters:
classNode - The class node
Returns:
The default constructor or null if there isn't one


getFullName

public static java.lang.String getFullName(org.codehaus.groovy.ast.ClassNode classNode)
Gets the full name of a ClassNode.
Parameters:
classNode - The class node
Returns:
The full name


getFurthestParent

public static org.codehaus.groovy.ast.ClassNode getFurthestParent(org.codehaus.groovy.ast.ClassNode classNode)


getFurthestUnresolvedParent

public static org.codehaus.groovy.ast.ClassNode getFurthestUnresolvedParent(org.codehaus.groovy.ast.ClassNode classNode)


getRemainingParameterTypes

public static org.codehaus.groovy.ast.Parameter[] getRemainingParameterTypes(org.codehaus.groovy.ast.Parameter[] parameters)
Gets the remaining parameters excluding the first parameter in the given list
Parameters:
parameters - The parameters
Returns:
A new array with the first parameter removed


hasOrInheritsProperty

public static boolean hasOrInheritsProperty(org.codehaus.groovy.ast.ClassNode classNode, java.lang.String propertyName)


hasProperty

public static boolean hasProperty(org.codehaus.groovy.ast.ClassNode classNode, java.lang.String propertyName)
Returns whether a classNode has the specified property or not
Parameters:
classNode - The ClassNode
propertyName - The name of the property
Returns:
True if the property exists in the ClassNode


implementsOrInheritsZeroArgMethod

@SuppressWarnings("rawtypes")
public static boolean implementsOrInheritsZeroArgMethod(org.codehaus.groovy.ast.ClassNode classNode, java.lang.String methodName, java.util.List ignoreClasses)


implementsZeroArgMethod

public static boolean implementsZeroArgMethod(org.codehaus.groovy.ast.ClassNode classNode, java.lang.String methodName)
Tests whether the ClasNode implements the specified method name.
Parameters:
classNode - The ClassNode
methodName - The method name
Returns:
True if it does implement the method


isCandidateInstanceMethod

public static boolean isCandidateInstanceMethod(org.codehaus.groovy.ast.ClassNode classNode, org.codehaus.groovy.ast.MethodNode declaredMethod)


isCandidateMethod

public static boolean isCandidateMethod(org.codehaus.groovy.ast.MethodNode declaredMethod)


isConstructorMethod

public static boolean isConstructorMethod(org.codehaus.groovy.ast.MethodNode declaredMethod)


nonGeneric

public static org.codehaus.groovy.ast.ClassNode nonGeneric(org.codehaus.groovy.ast.ClassNode type)


parametersEqual

public static boolean parametersEqual(org.codehaus.groovy.ast.Parameter[] a, org.codehaus.groovy.ast.Parameter[] b)
Returns:
true if the two arrays are of the same size and have the same contents


wrapMethodBodyInTryCatchDebugStatements

public static void wrapMethodBodyInTryCatchDebugStatements(org.codehaus.groovy.ast.MethodNode methodNode)
Wraps a method body in try / catch logic that catches any errors and logs an error, but does not rethrow!
Parameters:
methodNode - The method node


 

Groovy Documentation