public class ClassNode extends AnnotatedNode
Represents a class in the AST.
A ClassNode should be created using the methods in ClassHelper. This ClassNode may be used to represent a class declaration or any other type. This class uses a proxy mechanism allowing to create a class for a plain name at AST creation time. In another phase of the compiler the real ClassNode for the plain name may be found. To avoid the need of exchanging this ClassNode with an instance of the correct ClassNode the correct ClassNode is set as redirect. Most method calls are then redirected to that ClassNode.
There are three types of ClassNodes:
To describe generic type signature see getGenericsTypes() and setGenericsTypes(GenericsType[]). These methods are not proxied, they describe the type signature used at the point of declaration or the type signatures provided by the class. If the type signatures provided by the class are needed, then a call to redirect() will help.
Modifiers | Name | Description |
---|---|---|
static ClassNode[] |
EMPTY_ARRAY |
|
static ClassNode |
SUPER |
|
static ClassNode |
THIS |
|
protected Class |
clazz |
|
protected List<InnerClassNode> |
innerClasses |
|
protected boolean |
isPrimaryNode |
|
protected Object |
lazyInitLock |
Type | Name and description |
---|---|
boolean |
annotated |
MethodNode |
enclosingMethod |
boolean |
genericsPlaceHolder |
GenericsType[] |
genericsTypes |
ClassNode[] |
interfaces |
int |
modifiers |
ModuleNode |
module |
String |
name |
boolean |
script |
boolean |
scriptBody |
boolean |
staticClass |
ClassNode |
superClass |
boolean |
syntheticPublic |
ClassNode |
unresolvedSuperClass |
boolean |
usingGenerics |
Type Params | Return Type | Name and description |
---|---|---|
|
def |
ClassNode(Class c) Creates a non-primary ClassNode from a real class. |
|
def |
ClassNode(String name, int modifiers, ClassNode superClass) @param name the fully-qualified name of the class |
|
def |
ClassNode(String name, int modifiers, ClassNode superClass, ClassNode[] interfaces, MixinNode[] mixins) @param name the fully-qualified name of the class |
|
void |
addConstructor(ConstructorNode node) |
|
ConstructorNode |
addConstructor(int modifiers, Parameter[] parameters, ClassNode[] exceptions, Statement code) |
|
void |
addField(FieldNode node) |
|
FieldNode |
addField(String name, int modifiers, ClassNode type, Expression initialValue) |
|
void |
addFieldFirst(FieldNode node) |
|
FieldNode |
addFieldFirst(String name, int modifiers, ClassNode type, Expression initialValue) |
|
void |
addInterface(ClassNode type) |
|
void |
addMethod(MethodNode node) |
|
MethodNode |
addMethod(String name, int modifiers, ClassNode returnType, Parameter[] parameters, ClassNode[] exceptions, Statement code) If a method with the given name and parameters is already defined then it is returned otherwise the given method is added to this node. |
|
void |
addMixin(MixinNode mixin) |
|
void |
addObjectInitializerStatements(Statement statements) Adds a statement to the object initializer. |
|
void |
addProperty(PropertyNode node) |
|
PropertyNode |
addProperty(String name, int modifiers, ClassNode type, Expression initialValueExpression, Statement getterBlock, Statement setterBlock) |
|
void |
addStaticInitializerStatements(List<Statement> staticStatements, boolean fieldInit) |
|
MethodNode |
addSyntheticMethod(String name, int modifiers, ClassNode returnType, Parameter[] parameters, ClassNode[] exceptions, Statement code) Adds a synthetic method as part of the compilation process. |
|
void |
addTransform(Class<? extends ASTTransformation> transform, ASTNode node) |
|
GenericsType |
asGenericsType() |
|
boolean |
declaresInterface(ClassNode classNode) @param classNode the class node for the interface |
|
boolean |
equals(Object that) |
|
List<MethodNode> |
getAbstractMethods() @return the abstract methods associated with this ClassNode |
|
List<MethodNode> |
getAllDeclaredMethods() |
|
Set<ClassNode> |
getAllInterfaces() |
|
List<AnnotationNode> |
getAnnotations() |
|
List<AnnotationNode> |
getAnnotations(ClassNode type) |
|
CompileUnit |
getCompileUnit() |
|
ClassNode |
getComponentType() |
|
ConstructorNode |
getDeclaredConstructor(Parameter[] parameters) @return the constructor matching the given parameters or null |
|
List<ConstructorNode> |
getDeclaredConstructors() |
|
FieldNode |
getDeclaredField(String name) Finds a field matching the given name in this class. |
|
MethodNode |
getDeclaredMethod(String name, Parameter[] parameters) Finds a method matching the given name and parameters in this class. |
|
List<MethodNode> |
getDeclaredMethods(String name) This methods returns a list of all methods of the given name defined in the current class |
|
Map<String, MethodNode> |
getDeclaredMethodsMap() |
|
MethodNode |
getEnclosingMethod() |
|
FieldNode |
getField(String name) Finds a field matching the given name in this class or a parent class. |
|
Map<String, FieldNode> |
getFieldIndex() |
|
List<FieldNode> |
getFields() @return the fields associated with this ClassNode |
|
GenericsType[] |
getGenericsTypes() |
|
MethodNode |
getGetterMethod(String getterName) |
|
MethodNode |
getGetterMethod(String getterName, boolean searchSuperClasses) |
|
Iterator<InnerClassNode> |
getInnerClasses() @return iterator of inner classes defined inside this one |
|
ClassNode[] |
getInterfaces() @return the array of interfaces which this ClassNode implements |
|
MethodNode |
getMethod(String name, Parameter[] parameters) Finds a method matching the given name and parameters in this class or any parent class. |
|
List<MethodNode> |
getMethods() @return the methods associated with this ClassNode |
|
List<MethodNode> |
getMethods(String name) This methods creates a list of all methods with this name of the current class and of all super classes |
|
MixinNode[] |
getMixins() @return the mixins associated with this ClassNode |
|
int |
getModifiers() |
|
ModuleNode |
getModule() |
|
String |
getName() |
|
String |
getNameWithoutPackage() |
|
List<Statement> |
getObjectInitializerStatements() |
|
ClassNode |
getOuterClass() |
|
List<ClassNode> |
getOuterClasses() |
|
FieldNode |
getOuterField(String name) @return the field on the outer class or null if this is not an inner class |
|
PackageNode |
getPackage() |
|
String |
getPackageName() |
|
ClassNode |
getPlainNodeReference() |
|
List<PropertyNode> |
getProperties() |
|
PropertyNode |
getProperty(String name) |
|
MethodNode |
getSetterMethod(String setterName) |
|
MethodNode |
getSetterMethod(String setterName, boolean voidOnly) |
|
ClassNode |
getSuperClass() @return the ClassNode of the super class of this type |
|
String |
getText() |
|
Map<Class<? extends ASTTransformation>, Set<ASTNode>> |
getTransforms(CompilePhase phase) |
|
Class |
getTypeClass() Returns the concrete class this classnode relates to. |
|
ClassNode[] |
getUnresolvedInterfaces() |
|
ClassNode[] |
getUnresolvedInterfaces(boolean useRedirect) |
|
String |
getUnresolvedName() |
|
ClassNode |
getUnresolvedSuperClass() |
|
ClassNode |
getUnresolvedSuperClass(boolean useRedirect) |
|
boolean |
hasDeclaredMethod(String name, Parameter[] parameters) @see #getDeclaredMethod(String, Parameter[]) |
|
boolean |
hasMethod(String name, Parameter[] parameters) @see #getMethod(String, Parameter[]) |
|
boolean |
hasPackageName() |
|
boolean |
hasPossibleMethod(String name, Expression arguments) Determines if the type has a possibly-matching instance method with the given name and arguments. |
|
boolean |
hasPossibleStaticMethod(String name, Expression arguments) Checks if the given method has a possibly matching static method with the given name and arguments. |
|
boolean |
hasProperty(String name) |
|
int |
hashCode() |
|
boolean |
implementsInterface(ClassNode classNode) @param classNode the class node for the interface |
|
boolean |
isAbstract() |
|
boolean |
isAnnotated() |
|
boolean |
isAnnotationDefinition() |
|
boolean |
isArray() |
|
boolean |
isDerivedFrom(ClassNode type) @param type the ClassNode of interest |
|
boolean |
isDerivedFromGroovyObject() @return true if this type implements GroovyObject |
|
boolean |
isEnum() |
|
boolean |
isGenericsPlaceHolder() |
|
boolean |
isInterface() |
|
boolean |
isPrimaryClassNode() @return true if this instance is a primary ClassNode |
|
boolean |
isRedirectNode() |
|
boolean |
isResolved() |
|
boolean |
isScript() |
|
boolean |
isScriptBody() @return true if this inner class or closure was declared inside a script body |
|
boolean |
isStaticClass() Is this class declared in a static method (such as a closure / inner class declared in a static method) |
|
boolean |
isSyntheticPublic() Indicates that this class has been "promoted" to public by Groovy when in fact there was no public modifier explicitly in the source code. |
|
boolean |
isUsingGenerics() |
|
ClassNode |
makeArray() Returns a ClassNode representing an array of the type represented
by this. |
|
protected boolean |
parametersEqual(Parameter[] a, Parameter[] b) @deprecated
|
|
void |
positionStmtsAfterEnumInitStmts(List<Statement> staticFieldStatements) |
|
ClassNode |
redirect() Returns the ClassNode this node is a proxy for or the node itself. |
|
void |
removeConstructor(ConstructorNode node) |
|
void |
removeField(String oldName) |
|
void |
removeMethod(MethodNode node) |
|
void |
renameField(String oldName, String newName) |
|
void |
setAnnotated(boolean flag) Marks if the current class uses annotations or not. |
|
protected void |
setCompileUnit(CompileUnit cu) |
|
void |
setEnclosingMethod(MethodNode enclosingMethod) |
|
void |
setGenericsPlaceHolder(boolean b) |
|
void |
setGenericsTypes(GenericsType[] genericsTypes) |
|
void |
setInterfaces(ClassNode[] interfaces) |
|
void |
setModifiers(int modifiers) |
|
void |
setModule(ModuleNode module) |
|
String |
setName(String name) |
|
void |
setRedirect(ClassNode cn) Sets this instance as proxy for the given ClassNode . |
|
void |
setScript(boolean script) |
|
void |
setScriptBody(boolean scriptBody) |
|
void |
setStaticClass(boolean staticClass) |
|
void |
setSuperClass(ClassNode superClass) Sets the superclass of this ClassNode . |
|
void |
setSyntheticPublic(boolean syntheticPublic) |
|
void |
setUnresolvedSuperClass(ClassNode sn) |
|
void |
setUsingGenerics(boolean b) |
|
String |
toString() |
|
String |
toString(boolean showRedirect) |
|
MethodNode |
tryFindPossibleMethod(String name, Expression arguments) |
|
void |
visitContents(GroovyClassVisitor visitor) |
Methods inherited from class | Name |
---|---|
class AnnotatedNode |
addAnnotation, addAnnotations, getAnnotations, getAnnotations, getDeclaringClass, hasNoRealSourcePosition, isSynthetic, setDeclaringClass, setHasNoRealSourcePosition, setSynthetic |
class ASTNode |
copyNodeMetaData, getColumnNumber, getLastColumnNumber, getLastLineNumber, getLineNumber, getMetaDataMap, getNodeMetaData, getNodeMetaData, getText, putNodeMetaData, removeNodeMetaData, setColumnNumber, setLastColumnNumber, setLastLineNumber, setLineNumber, setNodeMetaData, setSourcePosition, visit |
name
- the fully-qualified name of the classmodifiers
- the modifiers; see org.objectweb.asm.OpcodessuperClass
- the base class; use "java.lang.Object" if no direct base class
name
- the fully-qualified name of the classmodifiers
- the modifiers; see org.objectweb.asm.OpcodessuperClass
- the base class; use "java.lang.Object" if no direct base classinterfaces
- the interfaces for this classmixins
- the mixins for this classIf a method with the given name and parameters is already defined then it is returned otherwise the given method is added to this node. This method is useful for default method adding like getProperty() or invokeMethod() where there may already be a method defined in a class and so the default implementations should not be added if already present.
Adds a statement to the object initializer.
statements
- the statement to be addedAdds a synthetic method as part of the compilation process.
classNode
- the class node for the interfacetrue
if this class declares that it implements the given
interface or if one of its interfaces extends directly/indirectly the interface
NOTE: Doesn't consider an interface to implement itself.
I think this is intended to be called on ClassNodes representing
classes, not interfaces.
ClassNode
null
Finds a field matching the given name in this class.
name
- the name of the field of interestFinds a method matching the given name and parameters in this class.
This methods returns a list of all methods of the given name defined in the current class
Finds a field matching the given name in this class or a parent class.
name
- the name of the field of interest
Finds a method matching the given name and parameters in this class or any parent class.
ClassNode
This methods creates a list of all methods with this name of the current class and of all super classes
ClassNode
null
if this is not an inner class
ClassNode
of the super class of this typeReturns the concrete class this classnode relates to. However, this method is inherently unsafe as it may return null depending on the compile phase you are using. AST transformations should never use this method directly, but rather obtain a new class node using getPlainNodeReference().
Determines if the type has a possibly-matching instance method with the given name and arguments.
name
- the name of the method of interestarguments
- the arguments to match againstChecks if the given method has a possibly matching static method with the given name and arguments.
name
- the name of the method of interestarguments
- the arguments to match againsttrue
if a matching method was found
classNode
- the class node for the interfacetrue
if this type implements the given interface
type
- the ClassNode of interest
true
if this type implements GroovyObject
true
if this instance is a primary ClassNode
true
if this inner class or closure was declared inside a script bodyIs this class declared in a static method (such as a closure / inner class declared in a static method)
Indicates that this class has been "promoted" to public by Groovy when in fact there was no public modifier explicitly in the source code. That is, it remembers that it has applied Groovy's "public classes by default" rule. This property is typically only of interest to AST transform writers.
true
if node is public but had no explicit public modifier Returns a ClassNode
representing an array of the type represented
by this.
true
if the two arrays are of the same size and have the same contents Returns the ClassNode
this node is a proxy for or the node itself.
Marks if the current class uses annotations or not.
Sets this instance as proxy for the given ClassNode
.
node
- the class to redirect to; if null
the redirect is removed Sets the superclass of this ClassNode
.