Class ClassNode
- All Implemented Interfaces:
org.objectweb.asm.Opcodes
- Direct Known Subclasses:
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 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:
-
Field Summary
Modifier and TypeFieldDescriptionprotected Class
static final ClassNode[]
protected List<InnerClassNode>
protected boolean
protected Object
static final ClassNode
static final ClassNode
Fields inherited from interface org.objectweb.asm.Opcodes
AALOAD, AASTORE, ACC_ABSTRACT, ACC_ANNOTATION, ACC_BRIDGE, ACC_DEPRECATED, ACC_ENUM, ACC_FINAL, ACC_INTERFACE, ACC_MANDATED, ACC_MODULE, ACC_NATIVE, ACC_OPEN, ACC_PRIVATE, ACC_PROTECTED, ACC_PUBLIC, ACC_RECORD, ACC_STATIC, ACC_STATIC_PHASE, ACC_STRICT, ACC_SUPER, ACC_SYNCHRONIZED, ACC_SYNTHETIC, ACC_TRANSIENT, ACC_TRANSITIVE, ACC_VARARGS, ACC_VOLATILE, ACONST_NULL, ALOAD, ANEWARRAY, ARETURN, ARRAYLENGTH, ASM10_EXPERIMENTAL, ASM4, ASM5, ASM6, ASM7, ASM8, ASM9, ASTORE, ATHROW, BALOAD, BASTORE, BIPUSH, CALOAD, CASTORE, CHECKCAST, D2F, D2I, D2L, DADD, DALOAD, DASTORE, DCMPG, DCMPL, DCONST_0, DCONST_1, DDIV, DLOAD, DMUL, DNEG, DOUBLE, DREM, DRETURN, DSTORE, DSUB, DUP, DUP_X1, DUP_X2, DUP2, DUP2_X1, DUP2_X2, F_APPEND, F_CHOP, F_FULL, F_NEW, F_SAME, F_SAME1, F2D, F2I, F2L, FADD, FALOAD, FASTORE, FCMPG, FCMPL, FCONST_0, FCONST_1, FCONST_2, FDIV, FLOAD, FLOAT, FMUL, FNEG, FREM, FRETURN, FSTORE, FSUB, GETFIELD, GETSTATIC, GOTO, H_GETFIELD, H_GETSTATIC, H_INVOKEINTERFACE, H_INVOKESPECIAL, H_INVOKESTATIC, H_INVOKEVIRTUAL, H_NEWINVOKESPECIAL, H_PUTFIELD, H_PUTSTATIC, I2B, I2C, I2D, I2F, I2L, I2S, IADD, IALOAD, IAND, IASTORE, ICONST_0, ICONST_1, ICONST_2, ICONST_3, ICONST_4, ICONST_5, ICONST_M1, IDIV, IF_ACMPEQ, IF_ACMPNE, IF_ICMPEQ, IF_ICMPGE, IF_ICMPGT, IF_ICMPLE, IF_ICMPLT, IF_ICMPNE, IFEQ, IFGE, IFGT, IFLE, IFLT, IFNE, IFNONNULL, IFNULL, IINC, ILOAD, IMUL, INEG, INSTANCEOF, INTEGER, INVOKEDYNAMIC, INVOKEINTERFACE, INVOKESPECIAL, INVOKESTATIC, INVOKEVIRTUAL, IOR, IREM, IRETURN, ISHL, ISHR, ISTORE, ISUB, IUSHR, IXOR, JSR, L2D, L2F, L2I, LADD, LALOAD, LAND, LASTORE, LCMP, LCONST_0, LCONST_1, LDC, LDIV, LLOAD, LMUL, LNEG, LONG, LOOKUPSWITCH, LOR, LREM, LRETURN, LSHL, LSHR, LSTORE, LSUB, LUSHR, LXOR, MONITORENTER, MONITOREXIT, MULTIANEWARRAY, NEW, NEWARRAY, NOP, NULL, POP, POP2, PUTFIELD, PUTSTATIC, RET, RETURN, SALOAD, SASTORE, SIPUSH, SOURCE_DEPRECATED, SOURCE_MASK, SWAP, T_BOOLEAN, T_BYTE, T_CHAR, T_DOUBLE, T_FLOAT, T_INT, T_LONG, T_SHORT, TABLESWITCH, TOP, UNINITIALIZED_THIS, V_PREVIEW, V1_1, V1_2, V1_3, V1_4, V1_5, V1_6, V1_7, V1_8, V10, V11, V12, V13, V14, V15, V16, V17, V18, V19, V20, V9
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionaddConstructor
(int modifiers, Parameter[] parameters, ClassNode[] exceptions, Statement code) void
addField
(String name, int modifiers, ClassNode type, Expression initialValue) void
addFieldFirst
(String name, int modifiers, ClassNode type, Expression initialValue) void
addFieldFirst
(FieldNode node) void
addInterface
(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.void
addMethod
(MethodNode node) void
void
addObjectInitializerStatements
(Statement statements) Adds a statement to the object initializer.addProperty
(String name, int modifiers, ClassNode type, Expression initialValueExpression, Statement getterBlock, Statement setterBlock) void
addProperty
(PropertyNode node) void
addStaticInitializerStatements
(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 processvoid
addTransform
(Class<? extends ASTTransformation> transform, ASTNode node) boolean
declaresInterface
(ClassNode classNode) boolean
getAnnotations
(ClassNode type) getDeclaredConstructor
(Parameter[] parameters) Finds a constructor matching the given parameters in this class.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) This methods returns a list of all methods of the given name defined in the current classFinds 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 parent class.getMethods
(String name) This methods creates a list of all methods with this name of the current class and of all super classesint
getName()
Helper method to avoid casting to inner classgetOuterField
(String name) getProperty
(String name) getSetterMethod
(String setterName) getSetterMethod
(String setterName, boolean voidOnly) getText()
Map<Class<? extends ASTTransformation>,
Set<ASTNode>> getTransforms
(CompilePhase phase) Returns the concrete class this classnode relates to.getUnresolvedInterfaces
(boolean useRedirect) getUnresolvedSuperClass
(boolean useRedirect) boolean
hasDeclaredMethod
(String name, Parameter[] parameters) int
hashCode()
boolean
boolean
boolean
hasPossibleMethod
(String name, Expression arguments) Returns true if the given method has a possibly matching instance method with the given name and arguments.boolean
hasPossibleStaticMethod
(String name, Expression arguments) Returns true if the given method has a possibly matching static method with the given name and arguments.boolean
hasProperty
(String name) boolean
implementsInterface
(ClassNode classNode) boolean
boolean
boolean
isArray()
boolean
isDerivedFrom
(ClassNode type) boolean
boolean
isEnum()
boolean
boolean
boolean
boolean
boolean
boolean
isScript()
boolean
boolean
Is this class declared in a static method (such as a closure / inner class declared in a static method)boolean
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
Returns a ClassNode representing an array of the class represented by this ClassNodeprotected boolean
parametersEqual
(Parameter[] a, Parameter[] b) void
positionStmtsAfterEnumInitStmts
(List<Statement> staticFieldStatements) redirect()
Returns the ClassNode this ClassNode is redirecting to.void
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 notprotected void
void
setEnclosingMethod
(MethodNode enclosingMethod) void
setGenericsPlaceHolder
(boolean b) void
setGenericsTypes
(GenericsType[] genericsTypes) void
setInterfaces
(ClassNode[] interfaces) void
setModifiers
(int modifiers) void
setModule
(ModuleNode module) 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 ClassNodevoid
setSyntheticPublic
(boolean syntheticPublic) void
void
setUsingGenerics
(boolean b) toString()
toString
(boolean showRedirect) tryFindPossibleMethod
(String name, Expression arguments) void
visitContents
(GroovyClassVisitor visitor) Methods inherited from class org.codehaus.groovy.ast.AnnotatedNode
addAnnotation, addAnnotations, getDeclaringClass, hasNoRealSourcePosition, isSynthetic, setDeclaringClass, setHasNoRealSourcePosition, setSynthetic
Methods inherited from class org.codehaus.groovy.ast.ASTNode
copyNodeMetaData, getColumnNumber, getLastColumnNumber, getLastLineNumber, getLineNumber, getNodeMetaData, getNodeMetaData, putNodeMetaData, removeNodeMetaData, setColumnNumber, setLastColumnNumber, setLastLineNumber, setLineNumber, setNodeMetaData, setSourcePosition, visit
-
Field Details
-
EMPTY_ARRAY
-
THIS
-
SUPER
-
isPrimaryNode
protected boolean isPrimaryNode -
innerClasses
-
lazyInitLock
-
clazz
-
-
Constructor Details
-
ClassNode
Creates a ClassNode from a real class. The resulting ClassNode will not be a primary ClassNode. -
ClassNode
- Parameters:
name
- is the full name of the classmodifiers
- the modifiers,superClass
- the base class name - use "java.lang.Object" if no direct base class- See Also:
-
Opcodes
-
ClassNode
public ClassNode(String name, int modifiers, ClassNode superClass, ClassNode[] interfaces, MixinNode[] mixins) - Parameters:
name
- is the full name of the classmodifiers
- the modifiers,superClass
- the base class name - use "java.lang.Object" if no direct base classinterfaces
- the interfaces for this classmixins
- the mixins for this class- See Also:
-
Opcodes
-
-
Method Details
-
redirect
Returns the ClassNode this ClassNode is redirecting to. -
setRedirect
Sets this instance as proxy for the given ClassNode.- Parameters:
cn
- the class to redirect to. If set to null the redirect will be removed
-
makeArray
Returns a ClassNode representing an array of the class represented by this ClassNode -
isPrimaryClassNode
public boolean isPrimaryClassNode()- Returns:
- true if this instance is a primary ClassNode
-
getEnclosingMethod
-
setEnclosingMethod
-
isSyntheticPublic
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. I.e. 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 this class is public but had no explicit public modifier
-
setSyntheticPublic
public void setSyntheticPublic(boolean syntheticPublic) -
setSuperClass
Sets the superclass of this ClassNode -
getFields
- Returns:
- the list of FieldNode's associated with this ClassNode
-
getInterfaces
- Returns:
- the array of interfaces which this ClassNode implements
-
setInterfaces
-
getMixins
- Returns:
- the array of mixins associated with this ClassNode
-
getMethods
- Returns:
- the list of methods associated with this ClassNode
-
getAbstractMethods
- Returns:
- the list of abstract methods associated with this ClassNode or null if there are no such methods
-
getAllDeclaredMethods
-
getAllInterfaces
-
getDeclaredMethodsMap
-
getName
-
getUnresolvedName
-
setName
-
getModifiers
public int getModifiers() -
setModifiers
public void setModifiers(int modifiers) -
getProperties
-
getDeclaredConstructors
-
getDeclaredConstructor
Finds a constructor matching the given parameters in this class.- Returns:
- the constructor matching the given parameters or null
-
removeConstructor
-
getModule
-
getPackage
-
setModule
-
addField
-
addFieldFirst
-
addProperty
-
addProperty
public PropertyNode addProperty(String name, int modifiers, ClassNode type, Expression initialValueExpression, Statement getterBlock, Statement setterBlock) -
hasProperty
-
getProperty
-
addConstructor
-
addConstructor
public ConstructorNode addConstructor(int modifiers, Parameter[] parameters, ClassNode[] exceptions, Statement code) -
addMethod
-
removeMethod
-
addMethod
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. -
hasDeclaredMethod
- See Also:
-
hasMethod
- See Also:
-
addSyntheticMethod
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 -
addField
-
addFieldFirst
-
addInterface
-
equals
-
hashCode
public int hashCode() -
addMixin
-
getDeclaredField
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
-
getField
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
-
getOuterField
- Returns:
- the field node on the outer class or null if this is not an inner class
-
getOuterClass
Helper method to avoid casting to inner class -
addObjectInitializerStatements
Adds a statement to the object initializer.- Parameters:
statements
- the statement to be added
-
getObjectInitializerStatements
-
addStaticInitializerStatements
-
positionStmtsAfterEnumInitStmts
-
getDeclaredMethods
This methods returns a list of all methods of the given name defined in the current class- Returns:
- the method list
- See Also:
-
getMethods
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:
-
getDeclaredMethod
Finds a method matching the given name and parameters in this class.- Returns:
- the method matching the given name and parameters or null
-
getMethod
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
-
isDerivedFrom
- Parameters:
type
- the ClassNode of interest- Returns:
- true if this node is derived from the given ClassNode
-
isDerivedFromGroovyObject
public boolean isDerivedFromGroovyObject()- Returns:
- true if this class is derived from a groovy object i.e. it implements GroovyObject
-
implementsInterface
- Parameters:
classNode
- the class node for the interface- Returns:
- true if this class or any base class implements the given interface
-
declaresInterface
- 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 or 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.
-
getSuperClass
- Returns:
- the ClassNode of the super class of this type
-
getUnresolvedSuperClass
-
getUnresolvedSuperClass
-
setUnresolvedSuperClass
-
getUnresolvedInterfaces
-
getUnresolvedInterfaces
-
getCompileUnit
-
setCompileUnit
-
parametersEqual
- Returns:
- true if the two arrays are of the same size and have the same contents
-
getPackageName
- Returns:
- the package name of this class
-
getNameWithoutPackage
-
visitContents
-
getGetterMethod
-
getGetterMethod
-
getSetterMethod
-
getSetterMethod
-
isStaticClass
public boolean isStaticClass()Is this class declared in a static method (such as a closure / inner class declared in a static method) -
setStaticClass
public void setStaticClass(boolean staticClass) -
isScriptBody
public boolean isScriptBody()- Returns:
- Returns true if this inner class or closure was declared inside a script body
-
setScriptBody
public void setScriptBody(boolean scriptBody) -
isScript
public boolean isScript() -
setScript
public void setScript(boolean script) -
toString
-
toString
-
hasPossibleMethod
Returns true if the given method has a possibly matching instance method with the given name and arguments.- Parameters:
name
- the name of the method of interestarguments
- the arguments to match against- Returns:
- true if a matching method was found
-
tryFindPossibleMethod
-
hasPossibleStaticMethod
Returns true if the given method has a possibly matching static method with the given name and arguments.- Parameters:
name
- the name of the method of interestarguments
- the arguments to match against- Returns:
- true if a matching method was found
-
isInterface
public boolean isInterface() -
isResolved
public boolean isResolved() -
isArray
public boolean isArray() -
getComponentType
-
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 usinggetPlainNodeReference()
.- Returns:
- the class this classnode relates to. May return null.
-
hasPackageName
public boolean hasPackageName() -
setAnnotated
public void setAnnotated(boolean flag) Marks if the current class uses annotations or not- Parameters:
flag
-
-
isAnnotated
public boolean isAnnotated() -
getGenericsTypes
-
setGenericsTypes
-
setGenericsPlaceHolder
public void setGenericsPlaceHolder(boolean b) -
isGenericsPlaceHolder
public boolean isGenericsPlaceHolder() -
isUsingGenerics
public boolean isUsingGenerics() -
setUsingGenerics
public void setUsingGenerics(boolean b) -
getPlainNodeReference
-
isAnnotationDefinition
public boolean isAnnotationDefinition() -
getAnnotations
- Overrides:
getAnnotations
in classAnnotatedNode
-
getAnnotations
- Overrides:
getAnnotations
in classAnnotatedNode
-
addTransform
-
getTransforms
-
renameField
-
removeField
-
isEnum
public boolean isEnum() -
getInnerClasses
- Returns:
- iterator of inner classes defined inside this one
-
isRedirectNode
public boolean isRedirectNode() -
getText
-