Package org.apache.groovy.ast.tools
Class ClassNodeUtils
java.lang.Object
org.apache.groovy.ast.tools.ClassNodeUtils
public class ClassNodeUtils
extends java.lang.Object
Utility class for working with ClassNodes
-
Method Summary
Modifier and Type Method Description static void
addDeclaredMethodsFromAllInterfaces(ClassNode cNode, java.util.Map<java.lang.String,MethodNode> methodsMap)
Adds methods from interfaces and parent interfaces.static void
addDeclaredMethodsFromInterfaces(ClassNode cNode, java.util.Map<java.lang.String,MethodNode> methodsMap)
Adds methods from all interfaces.static ConstructorNode
addGeneratedConstructor(ClassNode classNode, int modifiers, Parameter[] parameters, ClassNode[] exceptions, Statement code)
Add a method that is marked as@Generated
.static void
addGeneratedConstructor(ClassNode classNode, ConstructorNode consNode)
Add a method that is marked as@Generated
.static void
addGeneratedInnerClass(ClassNode cNode, ClassNode inner)
Add an inner class that is marked as@Generated
.static MethodNode
addGeneratedMethod(ClassNode cNode, java.lang.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
.static void
addGeneratedMethod(ClassNode cNode, MethodNode mNode)
Add a method and mark it as@Generated
.static void
addGeneratedMethod(ClassNode cNode, MethodNode mNode, boolean skipChecks)
Add a method and mark it as@Generated
.static java.lang.String
formatTypeName(ClassNode cNode)
Formats a type name into a human readable version.static java.util.Map<java.lang.String,MethodNode>
getDeclaredMethodsFromInterfaces(ClassNode cNode)
Gets methods from all interfaces.static java.util.Map<java.lang.String,MethodNode>
getDeclaredMethodsFromSuper(ClassNode cNode)
Add methods from the super class.static FieldNode
getField(ClassNode classNode, java.lang.String fieldName)
Return the (potentially inherited) field of the classnode.static java.lang.String
getPropNameForAccessor(java.lang.String accessorName)
Returns the property name, e.g.static PropertyNode
getStaticProperty(ClassNode cNode, java.lang.String propName)
Detect whether a static property with the given name is within the class or a super class.static boolean
hasExplicitConstructor(AbstractASTTransformation xform, ClassNode cNode)
Determine if an explicit (non-generated) constructor is in the class.static boolean
hasNoArgConstructor(ClassNode cNode)
static boolean
hasPossibleStaticMethod(ClassNode cNode, java.lang.String name, Expression arguments, boolean trySpread)
Returns true if the given method has a possibly matching static method with the given name and arguments.static boolean
hasPossibleStaticProperty(ClassNode cNode, java.lang.String methodName)
Return true if we have a static accessorstatic boolean
hasStaticProperty(ClassNode cNode, java.lang.String propName)
static boolean
isInnerClass(ClassNode cNode)
Detect whether a given ClassNode is an inner class (non-static).static boolean
isSubtype(ClassNode maybeSuperclassOrInterface, ClassNode candidateChild)
static boolean
isValidAccessorName(java.lang.String accessorName)
Detect whether the given accessor name starts with "get", "set" or "is" followed by at least one character.static boolean
samePackageName(ClassNode first, ClassNode second)
Determine if the given ClassNode values have the same package name.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Method Details
-
formatTypeName
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
- the type to format- Returns:
- a human readable version of the type name (java.lang.String[] for example)
-
addGeneratedMethod
public static MethodNode addGeneratedMethod(ClassNode cNode, java.lang.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
. -
addGeneratedMethod
Add a method and mark it as@Generated
.- See Also:
ClassNode.addMethod(MethodNode)
-
addGeneratedMethod
Add a method and mark it as@Generated
.- See Also:
ClassNode.addMethod(MethodNode)
-
addGeneratedInnerClass
Add an inner class that is marked as@Generated
.- See Also:
ModuleNode.addClass(ClassNode)
-
addGeneratedConstructor
public static ConstructorNode addGeneratedConstructor(ClassNode classNode, int modifiers, Parameter[] parameters, ClassNode[] exceptions, Statement code)Add a method that is marked as@Generated
. -
addGeneratedConstructor
Add a method that is marked as@Generated
. -
getDeclaredMethodsFromSuper
public static java.util.Map<java.lang.String,MethodNode> getDeclaredMethodsFromSuper(ClassNode cNode)Add methods from the super class.- Parameters:
cNode
- The ClassNode- Returns:
- A map of methods
-
addDeclaredMethodsFromInterfaces
public static void addDeclaredMethodsFromInterfaces(ClassNode cNode, java.util.Map<java.lang.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
- The ClassNodemethodsMap
- A map of existing methods to alter
-
getDeclaredMethodsFromInterfaces
public static java.util.Map<java.lang.String,MethodNode> getDeclaredMethodsFromInterfaces(ClassNode cNode)Gets methods from all interfaces. Methods from interfaces visited early take precedence over later ones.- Parameters:
cNode
- The ClassNode- Returns:
- A map of methods
-
addDeclaredMethodsFromAllInterfaces
public static void addDeclaredMethodsFromAllInterfaces(ClassNode cNode, java.util.Map<java.lang.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
- The ClassNodemethodsMap
- A map of existing methods to alter
-
hasPossibleStaticMethod
public static boolean hasPossibleStaticMethod(ClassNode cNode, java.lang.String name, Expression arguments, 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
- the ClassNode of interestname
- the name of the method of interestarguments
- the arguments to match againsttrySpread
- whether to try to account for SpreadExpressions within the arguments- Returns:
true
if a matching method was found.
-
hasPossibleStaticProperty
Return true if we have a static accessor -
getPropNameForAccessor
public static java.lang.String getPropNameForAccessor(java.lang.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
- the accessor name of interest, e.g. getAge- Returns:
- the property name, e.g. age, or original if not a valid property accessor name
-
isValidAccessorName
public static boolean isValidAccessorName(java.lang.String accessorName)Detect whether the given accessor name starts with "get", "set" or "is" followed by at least one character.- Parameters:
accessorName
- the accessor name of interest, e.g. getAge- Returns:
- true if a valid prefix is found
-
hasStaticProperty
-
getStaticProperty
Detect whether a static property with the given name is within the class or a super class.- Parameters:
cNode
- the ClassNode of interestpropName
- the property name- Returns:
- the static property if found or else null
-
isInnerClass
Detect whether a given ClassNode is an inner class (non-static).- Parameters:
cNode
- the ClassNode of interest- Returns:
- true if the given node is a (non-static) inner class, else false
-
hasNoArgConstructor
-
hasExplicitConstructor
Determine if an explicit (non-generated) constructor is in the class.- Parameters:
xform
- if non null, add an error if an explicit constructor is foundcNode
- the type of the containing class- Returns:
- true if an explicit (non-generated) constructor was found
-
samePackageName
Determine if the given ClassNode values have the same package name.- Parameters:
first
- a ClassNodesecond
- a ClassNode- Returns:
- true if both given nodes have the same package name
- Throws:
java.lang.NullPointerException
- if either of the given nodes are null
-
getField
Return the (potentially inherited) field of the classnode.- Parameters:
classNode
- the classnodefieldName
- the name of the field- Returns:
- the field or null if not found
-
isSubtype
-