Class ClassNode
- All Implemented Interfaces:
- GroovydocHolder<AnnotatedNode>,- NodeMetaDataHandler
- Direct Known Subclasses:
- DecompiledClassNode,- InnerClassNode,- MixinNode,- WideningCategories.LowestUpperBoundClassNode
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 placeholders, 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:
- 
Field SummaryFieldsModifier and TypeFieldDescriptionprotected Classstatic final ClassNode[]protected List<InnerClassNode>protected booleanprotected final Objectstatic final ClassNodestatic final ClassNodeFields inherited from interface groovy.lang.groovydoc.GroovydocHolderDOC_COMMENT
- 
Constructor SummaryConstructors
- 
Method SummaryModifier and TypeMethodDescriptionaddConstructor(int modifiers, Parameter[] parameters, ClassNode[] exceptions, Statement code) voidaddField(String name, int modifiers, ClassNode type, Expression initialValue) voidaddFieldFirst(String name, int modifiers, ClassNode type, Expression initialValue) voidaddFieldFirst(FieldNode node) voidaddInterface(ClassNode type) 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.voidaddMethod(MethodNode node) voidvoidaddObjectInitializerStatements(Statement statements) Adds a statement to the object initializer.addProperty(String name, int modifiers, ClassNode type, Expression initialValueExpression, Statement getterBlock, Statement setterBlock) voidaddProperty(PropertyNode node) voidaddStaticInitializerStatements(List<Statement> staticStatements, boolean fieldInit) addSyntheticMethod(String name, int modifiers, ClassNode returnType, Parameter[] parameters, ClassNode[] exceptions, Statement code) Adds a synthetic method as part of the compilation process.voidaddTransform(Class<? extends ASTTransformation> transform, ASTNode node) voidaddTypeAnnotation(AnnotationNode annotation) voidaddTypeAnnotations(List<AnnotationNode> annotations) booleandeclaresAnyInterfaces(ClassNode... classNodes) booleandeclaresInterface(ClassNode classNode) booleangetAnnotations(ClassNode type) getDeclaredConstructor(Parameter[] parameters) getDeclaredField(String name) Finds a field matching the given name in this class.getDeclaredMethod(String name, Parameter[] parameters) Finds a method matching the given name and parameters in this class.getDeclaredMethods(String name) Returns a list of all methods with the given name from this class.Finds a field matching the given name in this class or a parent class.getGetterMethod(String getterName) getGetterMethod(String getterName, boolean searchSuperClasses) Finds a method matching the given name and parameters in this class or any super class.getMethods(String name) Returns a list of all methods with the given name from this class and its super class(es).intgetName()getOuterField(String name) getPlainNodeReference(boolean skipPrimitives) getProperty(String name) Deprecated.Gets the record components of record type.getSetterMethod(String setterName) getSetterMethod(String setterName, boolean voidOnly) getText()Map<Class<? extends ASTTransformation>,Set<ASTNode>> getTransforms(CompilePhase phase) getTypeAnnotations(ClassNode type) Returns the concrete class this classnode relates to.getUnresolvedInterfaces(boolean useRedirect) getUnresolvedSuperClass(boolean useRedirect) booleanhasDeclaredMethod(String name, Parameter[] parameters) inthashCode()booleanbooleanbooleanhasPossibleMethod(String name, Expression arguments) Determines if the type has a possibly-matching instance method with the given name and arguments.booleanhasPossibleStaticMethod(String name, Expression arguments) Checks if the given method has a possibly matching static method with the given name and arguments.booleanhasProperty(String name) booleanimplementsAnyInterfaces(ClassNode... classNodes) booleanimplementsInterface(ClassNode classNode) booleanbooleanbooleanbooleanisArray()booleanisDerivedFrom(ClassNode type) booleanbooleanisEnum()booleanbooleanbooleanbooleanisRecord()Checks if theClassNodeinstance represents a nativerecord.booleanbooleanbooleanisScript()booleanbooleanisSealed()booleanIs this class declared in a static method (such as a closure / inner class declared in a static method)booleanIndicates that this class has been "promoted" to public by Groovy when in fact there was no public modifier explicitly in the source code.booleanReturns aClassNoderepresenting an array of the type represented by this.protected booleanparametersEqual(Parameter[] a, Parameter[] b) Deprecated.voidpositionStmtsAfterEnumInitStmts(List<Statement> staticFieldStatements) redirect()Returns theClassNodethis node is a proxy for or the node itself.voidvoidremoveField(String oldName) voidremoveMethod(MethodNode node) voidrenameField(String oldName, String newName) voidsetAnnotated(boolean annotated) Marks if the current class uses annotations or not.protected voidvoidsetEnclosingMethod(MethodNode enclosingMethod) voidsetGenericsPlaceHolder(boolean placeholder) voidsetGenericsTypes(GenericsType[] genericsTypes) voidsetInterfaces(ClassNode[] interfaces) voidvoidsetModifiers(int modifiers) voidsetModule(ModuleNode module) voidsetPermittedSubclasses(List<ClassNode> permittedSubclasses) voidsetRecordComponentNodes(List<RecordComponentNode> recordComponentNodes) Deprecated.voidsetRecordComponents(List<RecordComponentNode> recordComponents) Sets the record components for record type.voidsetRedirect(ClassNode node) Sets this instance as proxy for the givenClassNode.voidsetScript(boolean script) voidsetScriptBody(boolean scriptBody) voidsetStaticClass(boolean staticClass) voidsetSuperClass(ClassNode superClass) Sets the superclass of thisClassNode.voidsetSyntheticPublic(boolean syntheticPublic) voidsetUnresolvedSuperClass(ClassNode superClass) voidsetUsingGenerics(boolean usesGenerics) toString()toString(boolean showRedirect) tryFindPossibleMethod(String name, Expression arguments) voidvisitContents(GroovyClassVisitor visitor) Methods inherited from class org.codehaus.groovy.ast.AnnotatedNodeaddAnnotation, addAnnotation, addAnnotations, getDeclaringClass, getGroovydoc, getInstance, hasNoRealSourcePosition, isSynthetic, setDeclaringClass, setHasNoRealSourcePosition, setSyntheticMethods inherited from class org.codehaus.groovy.ast.ASTNodecopyNodeMetaData, getColumnNumber, getLastColumnNumber, getLastLineNumber, getLineNumber, getMetaDataMap, setColumnNumber, setLastColumnNumber, setLastLineNumber, setLineNumber, setMetaDataMap, setSourcePosition, visitMethods inherited from class java.lang.Objectclone, finalize, getClass, notify, notifyAll, wait, wait, waitMethods inherited from interface org.codehaus.groovy.ast.NodeMetaDataHandlercopyNodeMetaData, getNodeMetaData, getNodeMetaData, getNodeMetaData, putNodeMetaData, removeNodeMetaData, setNodeMetaData
- 
Field Details- 
EMPTY_ARRAY
- 
THIS
- 
SUPER
- 
isPrimaryNodeprotected boolean isPrimaryNode
- 
innerClasses
- 
lazyInitLock
- 
clazz
 
- 
- 
Constructor Details- 
ClassNodeCreates a non-primaryClassNodefrom a real class.
- 
ClassNode- Parameters:
- name- the fully-qualified name of the class
- modifiers- the modifiers; see- Opcodes
- superClass- the base class; use "java.lang.Object" if no direct base class
 
- 
ClassNodepublic ClassNode(String name, int modifiers, ClassNode superClass, ClassNode[] interfaces, MixinNode[] mixins) - Parameters:
- name- the fully-qualified name of the class
- modifiers- the modifiers; see- 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 Details- 
redirectReturns theClassNodethis node is a proxy for or the node itself.
- 
isRedirectNodepublic boolean isRedirectNode()
- 
setRedirectSets this instance as proxy for the givenClassNode.- Parameters:
- node- the class to redirect to; if- nullthe redirect is removed
 
- 
makeArrayReturns aClassNoderepresenting an array of the type represented by this.
- 
isPrimaryClassNodepublic boolean isPrimaryClassNode()- Returns:
- trueif this instance is a primary- ClassNode
 
- 
getEnclosingMethod
- 
setEnclosingMethod
- 
isSyntheticPublicpublic 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:
- trueif node is public but had no explicit public modifier
 
- 
setSyntheticPublicpublic void setSyntheticPublic(boolean syntheticPublic) 
- 
setSuperClassSets the superclass of thisClassNode.
- 
getFields- Returns:
- the fields associated with this ClassNode
 
- 
getInterfaces- Returns:
- the interfaces implemented by this ClassNode
 
- 
setInterfaces
- 
getPermittedSubclasses- Returns:
- permitted subclasses of sealed type, may initially be empty in early compiler phases
 
- 
setPermittedSubclasses
- 
getMixins- Returns:
- the mixins associated with this ClassNode
 
- 
setMixins
- 
getMethods- Returns:
- the methods associated with this ClassNode
 
- 
getAbstractMethods- Returns:
- the abstract methods associated with this ClassNode
 
- 
getAllDeclaredMethods
- 
getAllInterfaces
- 
getDeclaredMethodsMap
- 
getName
- 
getUnresolvedName
- 
setName
- 
getModifierspublic int getModifiers()
- 
setModifierspublic void setModifiers(int modifiers) 
- 
getProperties
- 
getDeclaredConstructors
- 
getDeclaredConstructor- Returns:
- the constructor matching the given parameters or null
 
- 
removeConstructor
- 
getModule
- 
getPackage
- 
setModule
- 
addField
- 
addFieldFirst
- 
getFieldIndex
- 
addProperty
- 
addPropertypublic PropertyNode addProperty(String name, int modifiers, ClassNode type, Expression initialValueExpression, Statement getterBlock, Statement setterBlock) 
- 
hasProperty
- 
getProperty
- 
addConstructor
- 
addConstructorpublic ConstructorNode addConstructor(int modifiers, Parameter[] parameters, ClassNode[] exceptions, Statement code) 
- 
addMethod
- 
removeMethod
- 
addMethodpublic 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.
- 
hasDeclaredMethod- See Also:
 
- 
hasMethod- See Also:
 
- 
addSyntheticMethodpublic MethodNode addSyntheticMethod(String name, int modifiers, ClassNode returnType, Parameter[] parameters, ClassNode[] exceptions, Statement code) Adds a synthetic method as part of the compilation process.
- 
addField
- 
addFieldFirst
- 
addInterface
- 
equals
- 
hashCodepublic int hashCode()
- 
addMixin
- 
getDeclaredFieldFinds 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
 
- 
getFieldFinds 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
 
- 
getOuterField- Returns:
- the field on the outer class or nullif this is not an inner class
 
- 
getOuterClass
- 
getOuterClasses
- 
addObjectInitializerStatementsAdds a statement to the object initializer.- Parameters:
- statements- the statement to be added
 
- 
getObjectInitializerStatements
- 
addStaticInitializerStatements
- 
positionStmtsAfterEnumInitStmts
- 
getDeclaredMethodsReturns a list of all methods with the given name from this class.- Returns:
- method list (possibly empty)
- See Also:
 
- 
getMethodsReturns a list of all methods with the given name from this class and its super class(es).- Returns:
- method list (possibly empty)
- See Also:
 
- 
getDeclaredMethodFinds a method matching the given name and parameters in this class.- Returns:
- method node or null
 
- 
getMethodFinds a method matching the given name and parameters in this class or any super class.- Returns:
- method node or null
 
- 
isDerivedFrom- Parameters:
- type- the ClassNode of interest
- Returns:
- true if this node is derived from the given ClassNode
 
- 
isDerivedFromGroovyObjectpublic boolean isDerivedFromGroovyObject()- Returns:
- trueif this type implements- GroovyObject
 
- 
implementsAnyInterfaces- Parameters:
- classNodes- the class nodes for the interfaces
- Returns:
- trueif this type implements any of the given interfaces
 
- 
implementsInterface- Parameters:
- classNode- the class node for the interface
- Returns:
- trueif this type implements the given interface
 
- 
declaresAnyInterfaces- Parameters:
- classNodes- the class nodes for the interfaces
- Returns:
- trueif 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
 
- 
declaresInterface- Parameters:
- classNode- the class node for the interface
- Returns:
- trueif 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.
- See Also:
 
- 
getSuperClass- Returns:
- the ClassNodeof the super class of this type
 
- 
getUnresolvedSuperClass
- 
getUnresolvedSuperClass
- 
setUnresolvedSuperClass
- 
getUnresolvedInterfaces
- 
getUnresolvedInterfaces
- 
getCompileUnit
- 
setCompileUnit
- 
parametersEqualDeprecated.
- 
getPackageName
- 
getNameWithoutPackage
- 
visitContents
- 
getGetterMethod
- 
getGetterMethod
- 
getSetterMethod
- 
getSetterMethod
- 
isStaticClasspublic boolean isStaticClass()Is this class declared in a static method (such as a closure / inner class declared in a static method)
- 
setStaticClasspublic void setStaticClass(boolean staticClass) 
- 
isScriptBodypublic boolean isScriptBody()- Returns:
- trueif this inner class or closure was declared inside a script body
 
- 
setScriptBodypublic void setScriptBody(boolean scriptBody) 
- 
isScriptpublic boolean isScript()
- 
setScriptpublic void setScript(boolean script) 
- 
toString
- 
toString
- 
hasPossibleMethodDetermines 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
 
- 
tryFindPossibleMethod
- 
hasPossibleStaticMethodChecks 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:
- trueif a matching method was found
 
- 
isInterfacepublic boolean isInterface()
- 
isRecordChecks if theClassNodeinstance represents a nativerecord. Check instead for theRecordBaseannotation if looking for records and record-like classes currently being compiled.- Returns:
- trueif the instance represents a native- record
- Since:
- 4.0.0
 
- 
getRecordComponentsGets the record components of record type.- Returns:
- RecordComponentNodeinstances
- Since:
- 4.0.0
 
- 
getRecordComponentNodesDeprecated.
- 
setRecordComponentsSets the record components for record type.- Since:
- 4.0.0
 
- 
setRecordComponentNodesDeprecated.
- 
isAbstractpublic boolean isAbstract()
- 
isSealed- Returns:
- truefor native and emulated (annotation based) sealed classes
- Since:
- 4.0.0
 
- 
isResolvedpublic boolean isResolved()
- 
isArraypublic boolean isArray()
- 
getComponentType
- 
getTypeClassReturns 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 usinggetPlainNodeReference().- Returns:
- the class this classnode relates to. May return null.
 
- 
hasPackageNamepublic boolean hasPackageName()
- 
setAnnotatedpublic void setAnnotated(boolean annotated) Marks if the current class uses annotations or not.
- 
isAnnotatedpublic boolean isAnnotated()
- 
asGenericsType
- 
getGenericsTypes
- 
setGenericsTypes
- 
setGenericsPlaceHolderpublic void setGenericsPlaceHolder(boolean placeholder) 
- 
isGenericsPlaceHolderpublic boolean isGenericsPlaceHolder()
- 
isUsingGenericspublic boolean isUsingGenerics()
- 
setUsingGenericspublic void setUsingGenerics(boolean usesGenerics) 
- 
getPlainNodeReference
- 
getPlainNodeReference
- 
isAnnotationDefinitionpublic boolean isAnnotationDefinition()
- 
getAnnotations- Overrides:
- getAnnotationsin class- AnnotatedNode
 
- 
getAnnotations- Overrides:
- getAnnotationsin class- AnnotatedNode
 
- 
addTransform
- 
getTransforms
- 
renameField
- 
removeField
- 
isEnumpublic boolean isEnum()
- 
getInnerClasses- Returns:
- iterator of inner classes defined inside this one
 
- 
getText
- 
getTypeAnnotations
- 
getTypeAnnotations
- 
addTypeAnnotation
- 
addTypeAnnotations
 
-