Package org.apache.groovy.ast.tools
Class ClassNodeUtils
java.lang.Object
org.apache.groovy.ast.tools.ClassNodeUtils
Utility class for working with ClassNodes
- 
Method SummaryModifier and TypeMethodDescriptionstatic voidaddDeclaredMethodsFromAllInterfaces(ClassNode cNode, Map<String, MethodNode> methodsMap) Adds methods from interfaces and parent interfaces.static voidaddDeclaredMethodsFromInterfaces(ClassNode cNode, Map<String, MethodNode> methodsMap) Adds methods from all interfaces.static ConstructorNodeaddGeneratedConstructor(ClassNode classNode, int modifiers, Parameter[] parameters, ClassNode[] exceptions, Statement code) Add a method that is marked as@Generated.static voidaddGeneratedConstructor(ClassNode classNode, ConstructorNode ctorNode) Add a method that is marked as@Generated.static voidaddGeneratedInnerClass(ClassNode cNode, ClassNode inner) Add an inner class that is marked as@Generated.static MethodNodeaddGeneratedMethod(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.static voidaddGeneratedMethod(ClassNode cNode, MethodNode mNode) Add a method and mark it as@Generated.static voidaddGeneratedMethod(ClassNode cNode, MethodNode mNode, boolean skipChecks) Add a method and mark it as@Generated.static StringformatTypeName(ClassNode cNode) Formats a type name into a human-readable version.static Map<String,MethodNode> Gets methods from all interfaces.static Map<String,MethodNode> Add methods from the super class.static FieldNodeSearches the class for a field that matches specified name.static FieldNodeSearches the class for a field that matches specified name and test.static StringgetPropNameForAccessor(String accessorName) Returns the property name, e.g.static PropertyNodegetStaticProperty(ClassNode cNode, String propName) Detect whether a static property with the given name is within the class or a super class.static booleanhasExplicitConstructor(AbstractASTTransformation xform, ClassNode cNode) Determine if an explicit (non-generated) constructor is in the class.static booleanhasNoArgConstructor(ClassNode cNode) static booleanhasPossibleStaticMethod(ClassNode cNode, 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 booleanhasPossibleStaticProperty(ClassNode cNode, String methodName) Return true if we have a static accessorstatic booleanhasStaticProperty(ClassNode cNode, String propName) static booleanisCompatibleWith(ClassNode source, ClassNode target) Check if the source ClassNode is compatible with the target ClassNodestatic booleanisInnerClass(ClassNode cNode) Detect whether a given ClassNode is an inner class (non-static).static booleanstatic booleanisValidAccessorName(String accessorName) Detect whether the given accessor name starts with "get", "set" or "is" followed by at least one character.static booleansamePackageName(ClassNode first, ClassNode second) Determine if the given ClassNode values have the same package name.
- 
Method Details- 
formatTypeNameFormats 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)
 
- 
addGeneratedMethodpublic 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.
- 
addGeneratedMethodAdd a method and mark it as@Generated.- See Also:
 
- 
addGeneratedMethodAdd a method and mark it as@Generated.- See Also:
 
- 
addGeneratedInnerClassAdd an inner class that is marked as@Generated.- See Also:
 
- 
addGeneratedConstructorpublic static ConstructorNode addGeneratedConstructor(ClassNode classNode, int modifiers, Parameter[] parameters, ClassNode[] exceptions, Statement code) Add a method that is marked as@Generated.
- 
addGeneratedConstructorAdd a method that is marked as@Generated.
- 
getDeclaredMethodsFromSuperAdd methods from the super class.- Parameters:
- cNode- The ClassNode
- Returns:
- A map of methods
 
- 
addDeclaredMethodsFromInterfacespublic 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- The ClassNode
- methodsMap- A map of existing methods to alter
 
- 
getDeclaredMethodsFromInterfacesGets methods from all interfaces. Methods from interfaces visited early take precedence over later ones.- Parameters:
- cNode- The ClassNode
- Returns:
- A map of methods
 
- 
addDeclaredMethodsFromAllInterfacespublic 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- The ClassNode
- methodsMap- A map of existing methods to alter
 
- 
hasPossibleStaticMethodpublic static boolean hasPossibleStaticMethod(ClassNode cNode, 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 interest
- name- the name of the method of interest
- arguments- the arguments to match against
- trySpread- whether to try to account for SpreadExpressions within the arguments
- Returns:
- trueif a matching method was found.
 
- 
hasPossibleStaticPropertyReturn true if we have a static accessor
- 
getPropNameForAccessorReturns 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
 
- 
isValidAccessorNameDetect 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
- 
getStaticPropertyDetect whether a static property with the given name is within the class or a super class.- Parameters:
- cNode- the ClassNode of interest
- propName- the property name
- Returns:
- the static property if found or else null
 
- 
isInnerClassDetect 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
 
- 
isCompatibleWithCheck if the source ClassNode is compatible with the target ClassNode
- 
hasNoArgConstructor
- 
hasExplicitConstructorDetermine if an explicit (non-generated) constructor is in the class.- Parameters:
- xform- if non-null, add an error if an explicit constructor is found
- cNode- the type of the containing class
- Returns:
- true if an explicit (non-generated) constructor was found
 
- 
samePackageNameDetermine if the given ClassNode values have the same package name.- Parameters:
- first- a ClassNode
- second- a ClassNode
- Returns:
- true if both given nodes have the same package name
- Throws:
- NullPointerException- if either of the given nodes are null
 
- 
getFieldSearches the class for a field that matches specified name.
- 
getFieldpublic static FieldNode getField(ClassNode classNode, String fieldName, Predicate<FieldNode> acceptability) Searches the class for a field that matches specified name and test.
- 
isSubtype
 
-