[Java] Class ClassNode
- org.codehaus.groovy.ast.ClassNode
- All Implemented Interfaces and Traits:
- org.objectweb.asm.Opcodes
public class ClassNode extends AnnotatedNode implements org.objectweb.asm.Opcodes
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:
- Primary ClassNodes:
A primary ClassNode is one where we have a source representation which is to be compiled by Groovy and which we have an AST for. The groovy compiler will output one class for each such ClassNode that passes through AsmBytecodeGenerator... not more, not less. That means for example Closures become such ClassNodes too at some point. - ClassNodes create through different sources (typically created from a java.lang.reflect.Class object):
The compiler will not output classes from these, the methods usually do not contain bodies. These kind of ClassNodes will be used in different checks, but not checks that work on the method bodies. For example if such a ClassNode is a super class to a primary ClassNode, then the abstract method test and others will be done with data based on these. Theoretically it is also possible to mix both (1 and 2) kind of classes in a hierarchy, but this probably works only in the newest Groovy versions. Such ClassNodes normally have to isResolved() returning true without having a redirect.In the Groovy compiler the only version of this, that exists, is a ClassNode created through a Class instance - Labels:
ClassNodes created through ClassHelper.makeWithoutCaching. They are place holders, its redirect points to the real structure, which can be a label too, but following all redirects it should end with a ClassNode from one of the other two categories. If ResolveVisitor finds such a node, it tries to set the redirects. Any such label created after ResolveVisitor has done its work needs to have a redirect pointing to case 1 or 2. If not the compiler may react strange... this can be considered as a kind of dangling pointer.
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.
- See Also:
- ClassHelper
Field Summary
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 |
Constructor Summary
Constructor and description |
---|
ClassNode
(Class<?> c) Creates a non-primary ClassNode from a real class. |
ClassNode
(String name, int modifiers, ClassNode superClass)
|
ClassNode
(String name, int modifiers, ClassNode superClass, ClassNode[] interfaces, MixinNode[] mixins)
|
Methods Summary
Type Params | Return Type | Name and description |
---|---|---|
public void |
addConstructor(ConstructorNode node) | |
public ConstructorNode |
addConstructor(int modifiers, Parameter[] parameters, ClassNode[] exceptions, Statement code) | |
public void |
addField(FieldNode node) | |
public FieldNode |
addField(String name, int modifiers, ClassNode type, Expression initialValue) | |
public void |
addFieldFirst(FieldNode node) | |
public FieldNode |
addFieldFirst(String name, int modifiers, ClassNode type, Expression initialValue) | |
public void |
addInterface(ClassNode type) | |
public void |
addMethod(MethodNode node) | |
public 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. | |
public void |
addMixin(MixinNode mixin) | |
public void |
addObjectInitializerStatements(Statement statements) Adds a statement to the object initializer. | |
public void |
addProperty(PropertyNode node) | |
public PropertyNode |
addProperty(String name, int modifiers, ClassNode type, Expression initialValueExpression, Statement getterBlock, Statement setterBlock) | |
public void |
addStaticInitializerStatements(List<Statement> staticStatements, boolean fieldInit) | |
public MethodNode |
addSyntheticMethod(String name, int modifiers, ClassNode returnType, Parameter[] parameters, ClassNode[] exceptions, Statement code) Adds a synthetic method as part of the compilation process. | |
public void |
addTransform(Class<? extends ASTTransformation> transform, ASTNode node) | |
public GenericsType |
asGenericsType() | |
public boolean |
declaresAnyInterfaces(ClassNode classNodes)
| |
public boolean |
declaresInterface(ClassNode classNode)
| |
public boolean |
equals(Object that) | |
public List<MethodNode> |
getAbstractMethods()
| |
public List<MethodNode> |
getAllDeclaredMethods() | |
public Set<ClassNode> |
getAllInterfaces() | |
public List<AnnotationNode> |
getAnnotations() | |
public List<AnnotationNode> |
getAnnotations(ClassNode type) | |
public CompileUnit |
getCompileUnit() | |
public ClassNode |
getComponentType() | |
public ConstructorNode |
getDeclaredConstructor(Parameter[] parameters)
| |
public List<ConstructorNode> |
getDeclaredConstructors() | |
public FieldNode |
getDeclaredField(String name) Finds a field matching the given name in this class. | |
public MethodNode |
getDeclaredMethod(String name, Parameter[] parameters) Finds a method matching the given name and parameters in this class. | |
public List<MethodNode> |
getDeclaredMethods(String name) This methods returns a list of all methods of the given name defined in the current class | |
public Map<String, MethodNode> |
getDeclaredMethodsMap() | |
public MethodNode |
getEnclosingMethod() | |
public FieldNode |
getField(String name) Finds a field matching the given name in this class or a parent class. | |
public Map<String, FieldNode> |
getFieldIndex() | |
public List<FieldNode> |
getFields()
| |
public GenericsType[] |
getGenericsTypes() | |
public MethodNode |
getGetterMethod(String getterName) | |
public MethodNode |
getGetterMethod(String getterName, boolean searchSuperClasses) | |
public Iterator<InnerClassNode> |
getInnerClasses()
| |
public ClassNode[] |
getInterfaces()
| |
public MethodNode |
getMethod(String name, Parameter[] parameters) Finds a method matching the given name and parameters in this class or any parent class. | |
public List<MethodNode> |
getMethods()
| |
public 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 | |
public MixinNode[] |
getMixins()
| |
public int |
getModifiers() | |
public ModuleNode |
getModule() | |
public String |
getName() | |
public String |
getNameWithoutPackage() | |
public List<Statement> |
getObjectInitializerStatements() | |
public ClassNode |
getOuterClass() | |
public List<ClassNode> |
getOuterClasses() | |
public FieldNode |
getOuterField(String name)
| |
public PackageNode |
getPackage() | |
public String |
getPackageName() | |
public ClassNode |
getPlainNodeReference() | |
public List<PropertyNode> |
getProperties() | |
public PropertyNode |
getProperty(String name) | |
public MethodNode |
getSetterMethod(String setterName) | |
public MethodNode |
getSetterMethod(String setterName, boolean voidOnly) | |
public ClassNode |
getSuperClass()
| |
public String |
getText() | |
public Map<Class<? extends ASTTransformation>, Set<ASTNode>> |
getTransforms(CompilePhase phase) | |
public Class |
getTypeClass() Returns the concrete class this classnode relates to. | |
public ClassNode[] |
getUnresolvedInterfaces() | |
public ClassNode[] |
getUnresolvedInterfaces(boolean useRedirect) | |
public String |
getUnresolvedName() | |
public ClassNode |
getUnresolvedSuperClass() | |
public ClassNode |
getUnresolvedSuperClass(boolean useRedirect) | |
public boolean |
hasDeclaredMethod(String name, Parameter[] parameters)
| |
public boolean |
hasMethod(String name, Parameter[] parameters)
| |
public boolean |
hasPackageName() | |
public boolean |
hasPossibleMethod(String name, Expression arguments) Determines if the type has a possibly-matching instance method with the given name and arguments. | |
public boolean |
hasPossibleStaticMethod(String name, Expression arguments) Checks if the given method has a possibly matching static method with the given name and arguments. | |
public boolean |
hasProperty(String name) | |
public int |
hashCode() | |
public boolean |
implementsAnyInterfaces(ClassNode classNodes)
| |
public boolean |
implementsInterface(ClassNode classNode)
| |
public boolean |
isAbstract() | |
public boolean |
isAnnotated() | |
public boolean |
isAnnotationDefinition() | |
public boolean |
isArray() | |
public boolean |
isDerivedFrom(ClassNode type)
| |
public boolean |
isDerivedFromGroovyObject()
| |
public boolean |
isEnum() | |
public boolean |
isGenericsPlaceHolder() | |
public boolean |
isInterface() | |
public boolean |
isPrimaryClassNode()
| |
public boolean |
isRedirectNode() | |
public boolean |
isResolved() | |
public boolean |
isScript() | |
public boolean |
isScriptBody()
| |
public boolean |
isStaticClass() Is this class declared in a static method (such as a closure / inner class declared in a static method) | |
public 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. | |
public boolean |
isUsingGenerics() | |
public ClassNode |
makeArray() Returns a ClassNode representing an array of the type represented by this. | |
protected boolean |
parametersEqual(Parameter[] a, Parameter[] b)
| |
public void |
positionStmtsAfterEnumInitStmts(List<Statement> staticFieldStatements) | |
public ClassNode |
redirect() Returns the ClassNode this node is a proxy for or the node itself. | |
public void |
removeConstructor(ConstructorNode node) | |
public void |
removeField(String oldName) | |
public void |
removeMethod(MethodNode node) | |
public void |
renameField(String oldName, String newName) | |
public void |
setAnnotated(boolean annotated) Marks if the current class uses annotations or not. | |
protected void |
setCompileUnit(CompileUnit cu) | |
public void |
setEnclosingMethod(MethodNode enclosingMethod) | |
public void |
setGenericsPlaceHolder(boolean placeholder) | |
public void |
setGenericsTypes(GenericsType[] genericsTypes) | |
public void |
setInterfaces(ClassNode[] interfaces) | |
public void |
setMixins(MixinNode[] mixins) | |
public void |
setModifiers(int modifiers) | |
public void |
setModule(ModuleNode module) | |
public String |
setName(String name) | |
public void |
setRedirect(ClassNode node) Sets this instance as proxy for the given ClassNode . | |
public void |
setScript(boolean script) | |
public void |
setScriptBody(boolean scriptBody) | |
public void |
setStaticClass(boolean staticClass) | |
public void |
setSuperClass(ClassNode superClass) Sets the superclass of this ClassNode . | |
public void |
setSyntheticPublic(boolean syntheticPublic) | |
public void |
setUnresolvedSuperClass(ClassNode superClass) | |
public void |
setUsingGenerics(boolean usesGenerics) | |
public String |
toString() | |
public String |
toString(boolean showRedirect) | |
public MethodNode |
tryFindPossibleMethod(String name, Expression arguments) | |
public void |
visitContents(GroovyClassVisitor visitor) |
Inherited Methods Summary
Methods inherited from class | Name |
---|---|
class AnnotatedNode | addAnnotation, addAnnotations, getAnnotations, getAnnotations, getDeclaringClass, getGroovydoc, getInstance, hasNoRealSourcePosition, isSynthetic, setDeclaringClass, setHasNoRealSourcePosition, setSynthetic |
class ASTNode | copyNodeMetaData, getColumnNumber, getLastColumnNumber, getLastLineNumber, getLineNumber, getMetaDataMap, getText, setColumnNumber, setLastColumnNumber, setLastLineNumber, setLineNumber, setMetaDataMap, setSourcePosition, visit |
Field Detail
public static final ClassNode[] EMPTY_ARRAY
public static final ClassNode SUPER
public static final ClassNode THIS
protected Class clazz
protected List<InnerClassNode> innerClasses
protected boolean isPrimaryNode
protected final Object lazyInitLock
Constructor Detail
public ClassNode(Class<?> c)
Creates a non-primary ClassNode
from a real class.
public ClassNode(String name, int modifiers, ClassNode superClass)
- Parameters:
-
name
- the fully-qualified name of the class -
modifiers
- the modifiers; see org.objectweb.asm.Opcodes -
superClass
- the base class; use "java.lang.Object" if no direct base class
public ClassNode(String name, int modifiers, ClassNode superClass, ClassNode[] interfaces, MixinNode[] mixins)
- Parameters:
-
name
- the fully-qualified name of the class -
modifiers
- the modifiers; see org.objectweb.asm.Opcodes -
superClass
- the base class; use "java.lang.Object" if no direct base class -
interfaces
- the interfaces for this class -
mixins
- the mixins for this class
Method Detail
public void addConstructor(ConstructorNode node)
public ConstructorNode addConstructor(int modifiers, Parameter[] parameters, ClassNode[] exceptions, Statement code)
public void addField(FieldNode node)
public FieldNode addField(String name, int modifiers, ClassNode type, Expression initialValue)
public void addFieldFirst(FieldNode node)
public FieldNode addFieldFirst(String name, int modifiers, ClassNode type, Expression initialValue)
public void addInterface(ClassNode type)
public void addMethod(MethodNode node)
public 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. 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.
public void addMixin(MixinNode mixin)
public void addObjectInitializerStatements(Statement statements)
Adds a statement to the object initializer.
- Parameters:
-
statements
- the statement to be added
public void addProperty(PropertyNode node)
public PropertyNode addProperty(String name, int modifiers, ClassNode type, Expression initialValueExpression, Statement getterBlock, Statement setterBlock)
public void addStaticInitializerStatements(List<Statement> staticStatements, boolean fieldInit)
public MethodNode addSyntheticMethod(String name, int modifiers, ClassNode returnType, Parameter[] parameters, ClassNode[] exceptions, Statement code)
Adds a synthetic method as part of the compilation process.
public void addTransform(Class<? extends ASTTransformation> transform, ASTNode node)
public GenericsType asGenericsType()
public boolean declaresAnyInterfaces(ClassNode classNodes)
- Parameters:
-
classNodes
- the class nodes for the interfaces
- Returns:
-
true
if this type declares that it implements any of the given interfaces or if one of its interfaces extends directly/indirectly any of the given interfaces
public boolean declaresInterface(ClassNode classNode)
- Parameters:
-
classNode
- the class node for the interface
- Returns:
-
true
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.
public boolean equals(Object that)
public List<MethodNode> getAbstractMethods()
- Returns:
- the abstract methods associated with this
ClassNode
public List<MethodNode> getAllDeclaredMethods()
public Set<ClassNode> getAllInterfaces()
public List<AnnotationNode> getAnnotations()
public List<AnnotationNode> getAnnotations(ClassNode type)
public CompileUnit getCompileUnit()
public ClassNode getComponentType()
public ConstructorNode getDeclaredConstructor(Parameter[] parameters)
- Returns:
- the constructor matching the given parameters or
null
public List<ConstructorNode> getDeclaredConstructors()
public FieldNode getDeclaredField(String name)
Finds a field matching the given name in this class.
- Parameters:
-
name
- the name of the field of interest
- Returns:
- the method matching the given name and parameters or null
public MethodNode getDeclaredMethod(String name, Parameter[] parameters)
Finds a method matching the given name and parameters in this class.
- Returns:
- the method matching the given name and parameters or null
public List<MethodNode> getDeclaredMethods(String name)
This methods returns a list of all methods of the given name defined in the current class
- Returns:
- the method list
- See Also:
- getMethods(String)
public Map<String, MethodNode> getDeclaredMethodsMap()
public MethodNode getEnclosingMethod()
public FieldNode getField(String name)
Finds a field matching the given name in this class or a parent class.
- Parameters:
-
name
- the name of the field of interest
- Returns:
- the method matching the given name and parameters or null
public Map<String, FieldNode> getFieldIndex()
public List<FieldNode> getFields()
- Returns:
- the fields associated with this
ClassNode
public GenericsType[] getGenericsTypes()
public MethodNode getGetterMethod(String getterName)
public MethodNode getGetterMethod(String getterName, boolean searchSuperClasses)
public Iterator<InnerClassNode> getInnerClasses()
- Returns:
- iterator of inner classes defined inside this one
public ClassNode[] getInterfaces()
- Returns:
- the interfaces implemented by this
ClassNode
public MethodNode getMethod(String name, Parameter[] parameters)
Finds a method matching the given name and parameters in this class or any parent class.
- Returns:
- the method matching the given name and parameters or null
public List<MethodNode> getMethods()
- Returns:
- the methods associated with this
ClassNode
public 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
- Returns:
- the methods list
- See Also:
- getDeclaredMethods(String)
public MixinNode[] getMixins()
- Returns:
- the mixins associated with this
ClassNode
public int getModifiers()
public ModuleNode getModule()
public String getName()
public String getNameWithoutPackage()
public List<Statement> getObjectInitializerStatements()
public ClassNode getOuterClass()
public List<ClassNode> getOuterClasses()
public FieldNode getOuterField(String name)
- Returns:
- the field on the outer class or
null
if this is not an inner class
public PackageNode getPackage()
public String getPackageName()
public ClassNode getPlainNodeReference()
public List<PropertyNode> getProperties()
public PropertyNode getProperty(String name)
public MethodNode getSetterMethod(String setterName)
public MethodNode getSetterMethod(String setterName, boolean voidOnly)
public ClassNode getSuperClass()
- Returns:
- the
ClassNode
of the super class of this type
@Override public String getText()
public Map<Class<? extends ASTTransformation>, Set<ASTNode>> getTransforms(CompilePhase phase)
public Class getTypeClass()
Returns 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().
- Returns:
- the class this classnode relates to. May return null.
public ClassNode[] getUnresolvedInterfaces()
public ClassNode[] getUnresolvedInterfaces(boolean useRedirect)
public String getUnresolvedName()
public ClassNode getUnresolvedSuperClass()
public ClassNode getUnresolvedSuperClass(boolean useRedirect)
public boolean hasDeclaredMethod(String name, Parameter[] parameters)
- See Also:
- getDeclaredMethod(String, Parameter[])
public boolean hasMethod(String name, Parameter[] parameters)
- See Also:
- getMethod(String, Parameter[])
public boolean hasPackageName()
public boolean hasPossibleMethod(String name, Expression arguments)
Determines if the type has a possibly-matching instance method with the given name and arguments.
- Parameters:
-
name
- the name of the method of interest -
arguments
- the arguments to match against
- Returns:
- true if a matching method was found
public boolean hasPossibleStaticMethod(String name, Expression arguments)
Checks if the given method has a possibly matching static method with the given name and arguments.
- Parameters:
-
name
- the name of the method of interest -
arguments
- the arguments to match against
- Returns:
-
true
if a matching method was found
public boolean hasProperty(String name)
public int hashCode()
public boolean implementsAnyInterfaces(ClassNode classNodes)
- Parameters:
-
classNodes
- the class nodes for the interfaces
- Returns:
-
true
if this type implements any of the given interfaces
public boolean implementsInterface(ClassNode classNode)
- Parameters:
-
classNode
- the class node for the interface
- Returns:
-
true
if this type implements the given interface
public boolean isAbstract()
public boolean isAnnotated()
public boolean isAnnotationDefinition()
public boolean isArray()
public boolean isDerivedFrom(ClassNode type)
- Parameters:
-
type
- the ClassNode of interest
- Returns:
- true if this node is derived from the given ClassNode
public boolean isDerivedFromGroovyObject()
- Returns:
-
true
if this type implementsGroovyObject
public boolean isEnum()
public boolean isGenericsPlaceHolder()
public boolean isInterface()
public boolean isPrimaryClassNode()
- Returns:
-
true
if this instance is a primaryClassNode
public boolean isRedirectNode()
public boolean isResolved()
public boolean isScript()
public boolean isScriptBody()
- Returns:
-
true
if this inner class or closure was declared inside a script body
public boolean isStaticClass()
Is this class declared in a static method (such as a closure / inner class declared in a static method)
public 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. 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.
- Returns:
-
true
if node is public but had no explicit public modifier
public boolean isUsingGenerics()
public ClassNode makeArray()
Returns a ClassNode
representing an array of the type represented by this.
protected boolean parametersEqual(Parameter[] a, Parameter[] b)
- Returns:
-
true
if the two arrays are of the same size and have the same contents
public void positionStmtsAfterEnumInitStmts(List<Statement> staticFieldStatements)
public ClassNode redirect()
Returns the ClassNode
this node is a proxy for or the node itself.
public void removeConstructor(ConstructorNode node)
public void removeField(String oldName)
public void removeMethod(MethodNode node)
public void renameField(String oldName, String newName)
public void setAnnotated(boolean annotated)
Marks if the current class uses annotations or not.
protected void setCompileUnit(CompileUnit cu)
public void setEnclosingMethod(MethodNode enclosingMethod)
public void setGenericsPlaceHolder(boolean placeholder)
public void setGenericsTypes(GenericsType[] genericsTypes)
public void setInterfaces(ClassNode[] interfaces)
public void setMixins(MixinNode[] mixins)
public void setModifiers(int modifiers)
public void setModule(ModuleNode module)
public String setName(String name)
public void setRedirect(ClassNode node)
Sets this instance as proxy for the given ClassNode
.
- Parameters:
-
node
- the class to redirect to; ifnull
the redirect is removed
public void setScript(boolean script)
public void setScriptBody(boolean scriptBody)
public void setStaticClass(boolean staticClass)
public void setSuperClass(ClassNode superClass)
Sets the superclass of this ClassNode
.
public void setSyntheticPublic(boolean syntheticPublic)
public void setUnresolvedSuperClass(ClassNode superClass)
public void setUsingGenerics(boolean usesGenerics)
public String toString()
public String toString(boolean showRedirect)
public MethodNode tryFindPossibleMethod(String name, Expression arguments)
public void visitContents(GroovyClassVisitor visitor)
© 2003-2020 The Apache Software Foundation
Licensed under the Apache license.
https://docs.groovy-lang.org/3.0.7/html/gapi/org/codehaus/groovy/ast/ClassNode.html