Top Description Constructors Methods
org.apache.groovy.ast.tools

public Class ClassNodeUtils

extends Object
Class Inheritance
Imports
org.apache.groovy.util.BeanUtils, org.codehaus.groovy.ast.ClassHelper, .ClassNode, .ConstructorNode, .FieldNode, .MethodNode, .Parameter, .PropertyNode, org.codehaus.groovy.ast.expr.Expression, .MapExpression, .SpreadExpression, .TupleExpression, org.codehaus.groovy.ast.stmt.Statement, org.codehaus.groovy.transform.AbstractASTTransformation, java.lang.reflect.Modifier, java.util.Arrays, .HashSet, .LinkedHashMap, .List, .Map, .Objects, .Set

Utility class for working with ClassNodes

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public static void
addDeclaredMethodsFromAllInterfaces(ClassNode
The ClassNode
cNode
,
Map<String, MethodNode>
A map of existing methods to alter
methodsMap
)

Adds methods from interfaces and parent interfaces.

public static void
addDeclaredMethodsFromInterfaces(ClassNode
The ClassNode
cNode
,
Map<String, MethodNode>
A map of existing methods to alter
methodsMap
)

Adds methods from all interfaces.

public static ConstructorNode
addGeneratedConstructor(ClassNode classNode, int modifiers, Parameter[] parameters, ClassNode[] exceptions, Statement code)

Add a method that is marked as @Generated.

public static void
addGeneratedConstructor(ClassNode classNode, ConstructorNode consNode)

Add a method that is marked as @Generated.

public static void
addGeneratedInnerClass(ClassNode cNode, ClassNode inner)

Add an inner class that is marked as @Generated.

public static MethodNode
addGeneratedMethod(ClassNode cNode, String name, int modifiers, ClassNode returnType, Parameter[] parameters, ClassNode[] exceptions, Statement code)

Return an existing method if one exists or else create a new method and mark it as @Generated.

public static void
addGeneratedMethod(ClassNode cNode, MethodNode mNode)

Add a method and mark it as @Generated.

public static void
addGeneratedMethod(final ClassNode cNode, final MethodNode mNode, final boolean skipChecks)

Add a method and mark it as @Generated.

public static String

Returns:

a human readable version of the type name (java.lang.String[] for example)
formatTypeName
(ClassNode
the type to format
cNode
)

Formats a type name into a human readable version.

public static Map<String, MethodNode>

Returns:

A map of methods
getDeclaredMethodsFromInterfaces
(final ClassNode
The ClassNode
cNode
)

Gets methods from all interfaces.

public static Map<String, MethodNode>

Returns:

A map of methods
getDeclaredMethodsFromSuper
(ClassNode
The ClassNode
cNode
)

Add methods from the super class.

public static FieldNode

Returns:

the field or null if not found
getField
(ClassNode
the classnode
classNode
,
String
the name of the field
fieldName
)

Return the (potentially inherited) field of the classnode.

public static String

Returns:

the property name, e.g. age, or original if not a valid property accessor name
getPropNameForAccessor
(String
the accessor name of interest, e.g. getAge
accessorName
)

Returns the property name, e.g. age, given an accessor name, e.g. getAge.

public static PropertyNode

Returns:

the static property if found or else null
getStaticProperty
(ClassNode
the ClassNode of interest
cNode
,
String
the property name
propName
)

Detect whether a static property with the given name is within the class or a super class.

public static boolean

Returns:

true if an explicit (non-generated) constructor was found
hasExplicitConstructor
(AbstractASTTransformation
if non null, add an error if an explicit constructor is found
xform
,
ClassNode
the type of the containing class
cNode
)

Determine if an explicit (non-generated) constructor is in the class.

public static boolean
public static boolean

Returns:

true if a matching method was found.
hasPossibleStaticMethod
(final ClassNode
the ClassNode of interest
cNode
,
final String
the name of the method of interest
name
,
final Expression
the arguments to match against
arguments
,
final boolean
whether to try to account for SpreadExpressions within the arguments
trySpread
)

Returns true if the given method has a possibly matching static method with the given name and arguments.

public static boolean
hasPossibleStaticProperty(ClassNode cNode, String methodName)

Return true if we have a static accessor

public static boolean
public static boolean

Returns:

true if the given node is a (non-static) inner class, else false
isInnerClass
(final ClassNode
the ClassNode of interest
cNode
)

Detect whether a given ClassNode is an inner class (non-static).

public static boolean
isSubtype(ClassNode maybeSuperclassOrInterface, ClassNode candidateChild)

public static boolean

Returns:

true if a valid prefix is found
isValidAccessorName
(String
the accessor name of interest, e.g. getAge
accessorName
)

Detect whether the given accessor name starts with "get", "set" or "is" followed by at least one character.

public static boolean

Returns:

true if both given nodes have the same package name
samePackageName
(ClassNode
a ClassNode
first
,
ClassNode
a ClassNode
second
)

Determine if the given ClassNode values have the same package name.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Constructor Detail

ClassNodeUtilsback to summary
private ClassNodeUtils()

Method Detail

addDeclaredMethodsFromAllInterfacesback to summary
public static void addDeclaredMethodsFromAllInterfaces(ClassNode cNode, Map<String, MethodNode> methodsMap)

Adds methods from interfaces and parent interfaces. Existing entries in the methods map take precedence. Methods from interfaces visited early take precedence over later ones.

Parameters
cNode:ClassNode

The ClassNode

methodsMap:Map<String, MethodNode>

A map of existing methods to alter

addDeclaredMethodsFromInterfacesback to summary
public static void addDeclaredMethodsFromInterfaces(ClassNode cNode, Map<String, MethodNode> methodsMap)

Adds methods from all interfaces. Existing entries in the methods map take precedence. Methods from interfaces visited early take precedence over later ones.

Parameters
cNode:ClassNode

The ClassNode

methodsMap:Map<String, MethodNode>

A map of existing methods to alter

addGeneratedConstructorback to summary
public static ConstructorNode addGeneratedConstructor(ClassNode classNode, int modifiers, Parameter[] parameters, ClassNode[] exceptions, Statement code)

Add a method that is marked as @Generated.

See Also
ClassNode#addConstructor(int, Parameter[], ClassNode[], Statement)
addGeneratedConstructorback to summary
public static void addGeneratedConstructor(ClassNode classNode, ConstructorNode consNode)

Add a method that is marked as @Generated.

See Also
ClassNode#addConstructor(ConstructorNode)
addGeneratedInnerClassback to summary
public static void addGeneratedInnerClass(ClassNode cNode, ClassNode inner)

Add an inner class that is marked as @Generated.

See Also
org.codehaus.groovy.ast.ModuleNode#addClass(ClassNode)
addGeneratedMethodback to summary
public static MethodNode addGeneratedMethod(ClassNode cNode, String name, int modifiers, ClassNode returnType, Parameter[] parameters, ClassNode[] exceptions, Statement code)

Return an existing method if one exists or else create a new method and mark it as @Generated.

See Also
ClassNode#addMethod(String, int, ClassNode, Parameter[], ClassNode[], Statement)
addGeneratedMethodback to summary
public static void addGeneratedMethod(ClassNode cNode, MethodNode mNode)

Add a method and mark it as @Generated.

See Also
ClassNode#addMethod(MethodNode)
addGeneratedMethodback to summary
public static void addGeneratedMethod(final ClassNode cNode, final MethodNode mNode, final boolean skipChecks)

Add a method and mark it as @Generated.

See Also
ClassNode#addMethod(MethodNode)
formatTypeNameback to summary
public static String formatTypeName(ClassNode cNode)

Formats a type name into a human readable version. For arrays, appends "[]" to the formatted type name of the component. For unit class nodes, uses the class node name.

Parameters
cNode:ClassNode

the type to format

Returns:String

a human readable version of the type name (java.lang.String[] for example)

getDeclaredMethodsFromInterfacesback to summary
public static Map<String, MethodNode> getDeclaredMethodsFromInterfaces(final ClassNode cNode)

Gets methods from all interfaces. Methods from interfaces visited early take precedence over later ones.

Parameters
cNode:ClassNode

The ClassNode

Returns:Map<String, MethodNode>

A map of methods

getDeclaredMethodsFromSuperback to summary
public static Map<String, MethodNode> getDeclaredMethodsFromSuper(ClassNode cNode)

Add methods from the super class.

Parameters
cNode:ClassNode

The ClassNode

Returns:Map<String, MethodNode>

A map of methods

getFieldback to summary
public static FieldNode getField(ClassNode classNode, String fieldName)

Return the (potentially inherited) field of the classnode.

Parameters
classNode:ClassNode

the classnode

fieldName:String

the name of the field

Returns:FieldNode

the field or null if not found

getPropNameForAccessorback to summary
public static String getPropNameForAccessor(String accessorName)

Returns the property name, e.g. age, given an accessor name, e.g. getAge. Returns the original if a valid prefix cannot be removed.

Parameters
accessorName:String

the accessor name of interest, e.g. getAge

Returns:String

the property name, e.g. age, or original if not a valid property accessor name

getStaticPropertyback to summary
public static PropertyNode getStaticProperty(ClassNode cNode, String propName)

Detect whether a static property with the given name is within the class or a super class.

Parameters
cNode:ClassNode

the ClassNode of interest

propName:String

the property name

Returns:PropertyNode

the static property if found or else null

hasExplicitConstructorback to summary
public static boolean hasExplicitConstructor(AbstractASTTransformation xform, ClassNode cNode)

Determine if an explicit (non-generated) constructor is in the class.

Parameters
xform:AbstractASTTransformation

if non null, add an error if an explicit constructor is found

cNode:ClassNode

the type of the containing class

Returns:boolean

true if an explicit (non-generated) constructor was found

hasNoArgConstructorback to summary
public static boolean hasNoArgConstructor(ClassNode cNode)
hasPossibleStaticMethodback to summary
public static boolean hasPossibleStaticMethod(final ClassNode cNode, final String name, final Expression arguments, final boolean trySpread)

Returns true if the given method has a possibly matching static method with the given name and arguments. Handles default arguments and optionally spread expressions.

Parameters
cNode:ClassNode

the ClassNode of interest

name:String

the name of the method of interest

arguments:Expression

the arguments to match against

trySpread:boolean

whether to try to account for SpreadExpressions within the arguments

Returns:boolean

true if a matching method was found.

hasPossibleStaticPropertyback to summary
public static boolean hasPossibleStaticProperty(ClassNode cNode, String methodName)

Return true if we have a static accessor

hasStaticPropertyback to summary
public static boolean hasStaticProperty(ClassNode cNode, String propName)
isInnerClassback to summary
public static boolean isInnerClass(final ClassNode cNode)

Detect whether a given ClassNode is an inner class (non-static).

Parameters
cNode:ClassNode

the ClassNode of interest

Returns:boolean

true if the given node is a (non-static) inner class, else false

isSubtypeback to summary
public static boolean isSubtype(ClassNode maybeSuperclassOrInterface, ClassNode candidateChild)
isValidAccessorNameback to summary
public static boolean isValidAccessorName(String accessorName)

Detect whether the given accessor name starts with "get", "set" or "is" followed by at least one character.

Parameters
accessorName:String

the accessor name of interest, e.g. getAge

Returns:boolean

true if a valid prefix is found

samePackageNameback to summary
public static boolean samePackageName(ClassNode first, ClassNode second)

Determine if the given ClassNode values have the same package name.

Parameters
first:ClassNode

a ClassNode

second:ClassNode

a ClassNode

Returns:boolean

true if both given nodes have the same package name

Exceptions
NullPointerException:
if either of the given nodes are null