org.codehaus.groovy.ast
Class ClassNode

java.lang.Object
  extended by org.codehaus.groovy.ast.ASTNode
      extended by org.codehaus.groovy.ast.AnnotatedNode
          extended by org.codehaus.groovy.ast.ClassNode
All Implemented Interfaces:
org.objectweb.asm.Opcodes
Direct Known Subclasses:
InnerClassNode, MixinNode

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.
Note: the proxy mechanism is only allowed for classes being marked as primary ClassNode which means they represent no actual class. The redirect itself can be any type of ClassNode
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.

Version:
$Revision: 15543 $
Author:
James Strachan, Jochen Theodorou
See Also:
ClassHelper

Field Summary
protected  java.lang.Class clazz
           
static ClassNode[] EMPTY_ARRAY
           
protected  boolean isPrimaryNode
           
protected  java.lang.Object lazyInitLock
           
static ClassNode SUPER
           
static ClassNode THIS
           
 
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_NATIVE, ACC_PRIVATE, ACC_PROTECTED, ACC_PUBLIC, ACC_STATIC, ACC_STRICT, ACC_SUPER, ACC_SYNCHRONIZED, ACC_SYNTHETIC, ACC_TRANSIENT, ACC_VARARGS, ACC_VOLATILE, ACONST_NULL, ALOAD, ANEWARRAY, ARETURN, ARRAYLENGTH, ASTORE, ATHROW, BALOAD, BASTORE, BIPUSH, CALOAD, CASTORE, CHECKCAST, D2F, D2I, D2L, DADD, DALOAD, DASTORE, DCMPG, DCMPL, DCONST_0, DCONST_1, DDIV, DLOAD, DMUL, DNEG, DREM, DRETURN, DSTORE, DSUB, DUP, DUP_X1, DUP_X2, DUP2, DUP2_X1, DUP2_X2, F2D, F2I, F2L, FADD, FALOAD, FASTORE, FCMPG, FCMPL, FCONST_0, FCONST_1, FCONST_2, FDIV, FLOAD, FMUL, FNEG, FREM, FRETURN, FSTORE, FSUB, GETFIELD, GETSTATIC, GOTO, 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, 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, LOOKUPSWITCH, LOR, LREM, LRETURN, LSHL, LSHR, LSTORE, LSUB, LUSHR, LXOR, MONITORENTER, MONITOREXIT, MULTIANEWARRAY, NEW, NEWARRAY, NOP, POP, POP2, PUTFIELD, PUTSTATIC, RET, RETURN, SALOAD, SASTORE, SIPUSH, SWAP, T_BOOLEAN, T_BYTE, T_CHAR, T_DOUBLE, T_FLOAT, T_INT, T_LONG, T_SHORT, TABLESWITCH, V1_1, V1_2, V1_3, V1_4, V1_5, V1_6
 
Constructor Summary
ClassNode(java.lang.Class c)
          Creates a ClassNode from a real class.
ClassNode(java.lang.String name, int modifiers, ClassNode superClass)
           
ClassNode(java.lang.String name, int modifiers, ClassNode superClass, ClassNode[] interfaces, MixinNode[] mixins)
           
 
Method Summary
 void addConstructor(ConstructorNode node)
           
 ConstructorNode addConstructor(int modifiers, Parameter[] parameters, ClassNode[] exceptions, Statement code)
           
 void addField(FieldNode node)
           
 FieldNode addField(java.lang.String name, int modifiers, ClassNode type, Expression initialValue)
           
 void addInterface(ClassNode type)
           
 void addMethod(MethodNode node)
           
 MethodNode addMethod(java.lang.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)
           
 void addProperty(PropertyNode node)
           
 PropertyNode addProperty(java.lang.String name, int modifiers, ClassNode type, Expression initialValueExpression, Statement getterBlock, Statement setterBlock)
           
 void addStaticInitializerStatements(java.util.List staticStatements, boolean fieldInit)
           
 MethodNode addSyntheticMethod(java.lang.String name, int modifiers, ClassNode returnType, Parameter[] parameters, ClassNode[] exceptions, Statement code)
          Adds a synthetic method as part of the compilation process
 boolean declaresInterface(ClassNode classNode)
           
 boolean equals(java.lang.Object o)
           
 java.util.List getAbstractMethods()
          Returns a list containing MethodNode objects for each abstract method in the class represented by this ClassNode
 java.util.List getAllDeclaredMethods()
           
 CompileUnit getCompileUnit()
           
 ClassNode getComponentType()
           
 java.util.List getDeclaredConstructors()
           
 FieldNode getDeclaredField(java.lang.String name)
          Finds a field matching the given name in this class.
 MethodNode getDeclaredMethod(java.lang.String name, Parameter[] parameters)
          Finds a method matching the given name and parameters in this class.
 java.util.List getDeclaredMethods(java.lang.String name)
          This methods returns a list of all methods of the given name defined in the current class
 java.util.Map getDeclaredMethodsMap()
           
 MethodNode getEnclosingMethod()
           
 FieldNode getField(java.lang.String name)
          Finds a field matching the given name in this class or a parent class.
 java.util.List getFields()
          Returns a list containing FieldNode objects for each field in the class represented by this ClassNode
 GenericsType[] getGenericsTypes()
           
 MethodNode getGetterMethod(java.lang.String getterName)
           
 ClassNode[] getInterfaces()
          Returns an array of ClassNodes representing the interfaces the class implements
 MethodNode getMethod(java.lang.String name, Parameter[] parameters)
          Finds a method matching the given name and parameters in this class or any parent class.
 java.util.List getMethods()
          Returns a list containing MethodNode objects for each method in the class represented by this ClassNode
 java.util.List getMethods(java.lang.String name)
          This methods creates a list of all methods with this name of the current class and of all super classes
 MixinNode[] getMixins()
           
 int getModifiers()
           
 ModuleNode getModule()
           
 java.lang.String getName()
           
 java.lang.String getNameWithoutPackage()
           
 java.util.List getObjectInitializerStatements()
           
 ClassNode getOuterClass()
          Helper method to avoid casting to inner class
 FieldNode getOuterField(java.lang.String name)
           
 java.lang.String getPackageName()
           
 ClassNode getPlainNodeReference()
           
 java.util.List getProperties()
           
 PropertyNode getProperty(java.lang.String name)
           
 MethodNode getSetterMethod(java.lang.String setterName)
           
 ClassNode getSuperClass()
           
 java.lang.Class getTypeClass()
           
 ClassNode getUnresolvedSuperClass()
           
 ClassNode getUnresolvedSuperClass(boolean useRedirect)
           
 boolean hasDeclaredMethod(java.lang.String name, Parameter[] parameters)
           
 boolean hasMethod(java.lang.String name, Parameter[] parameters)
           
 boolean hasPackageName()
           
 boolean hasPossibleMethod(java.lang.String name, Expression arguments)
          Returns true if the given method has a possibly matching method with the given name and arguments
 boolean hasPossibleStaticMethod(java.lang.String name, Expression arguments)
          Returns true if the given method has a possibly matching static method with the given name and arguments
 boolean hasProperty(java.lang.String name)
           
 boolean implementsInterface(ClassNode classNode)
           
 boolean isAnnotated()
           
 boolean isArray()
           
 boolean isDerivedFrom(ClassNode type)
           
 boolean isDerivedFromGroovyObject()
           
 boolean isGenericsPlaceHolder()
           
 boolean isInterface()
           
 boolean isPrimaryClassNode()
          Returns if this instance is a primary ClassNode
 boolean isResolved()
           
 boolean isScript()
           
 boolean isScriptBody()
           
 boolean isStaticClass()
          Is this class delcared in a static method (such as a closure / inner class declared in a static method)
 boolean isUsingGenerics()
           
 ClassNode makeArray()
          Returns a ClassNode representing an array of the class represented by this ClassNode
protected  boolean parametersEqual(Parameter[] a, Parameter[] b)
           
 ClassNode redirect()
          Returns the ClassNode this ClassNode is redirecting to.
 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 setModule(ModuleNode module)
           
 java.lang.String setName(java.lang.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 setUnresolvedSuperClass(ClassNode sn)
           
 void setUsingGenerics(boolean b)
           
 java.lang.String toString()
           
 void visitContents(GroovyClassVisitor visitor)
           
 
Methods inherited from class org.codehaus.groovy.ast.AnnotatedNode
addAnnotation, addAnnotations, getAnnotationClass, getAnnotations, getAnnotations, getDeclaringClass, isSynthetic, setDeclaringClass, setSynthetic
 
Methods inherited from class org.codehaus.groovy.ast.ASTNode
getColumnNumber, getLastColumnNumber, getLastLineNumber, getLineNumber, getText, setColumnNumber, setLastColumnNumber, setLastLineNumber, setLineNumber, setSourcePosition, visit
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

EMPTY_ARRAY

public static ClassNode[] EMPTY_ARRAY

THIS

public static ClassNode THIS

SUPER

public static ClassNode SUPER

isPrimaryNode

protected boolean isPrimaryNode

lazyInitLock

protected java.lang.Object lazyInitLock

clazz

protected java.lang.Class clazz
Constructor Detail

ClassNode

public ClassNode(java.lang.Class c)
Creates a ClassNode from a real class. The resulting ClassNode will be no primary ClassNode.


ClassNode

public ClassNode(java.lang.String name,
                 int modifiers,
                 ClassNode superClass)
Parameters:
name - is the full name of the class
modifiers - the modifiers,
superClass - the base class name - use "java.lang.Object" if no direct base class
See Also:
Opcodes

ClassNode

public ClassNode(java.lang.String name,
                 int modifiers,
                 ClassNode superClass,
                 ClassNode[] interfaces,
                 MixinNode[] mixins)
Parameters:
name - is the full name of the class
modifiers - the modifiers,
superClass - the base class name - use "java.lang.Object" if no direct base class
See Also:
Opcodes
Method Detail

redirect

public ClassNode redirect()
Returns the ClassNode this ClassNode is redirecting to.


setRedirect

public void setRedirect(ClassNode cn)
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

public ClassNode makeArray()
Returns a ClassNode representing an array of the class represented by this ClassNode


isPrimaryClassNode

public boolean isPrimaryClassNode()
Returns if this instance is a primary ClassNode


getEnclosingMethod

public MethodNode getEnclosingMethod()

setEnclosingMethod

public void setEnclosingMethod(MethodNode enclosingMethod)

setSuperClass

public void setSuperClass(ClassNode superClass)
Sets the superclass of this ClassNode


getFields

public java.util.List getFields()
Returns a list containing FieldNode objects for each field in the class represented by this ClassNode


getInterfaces

public ClassNode[] getInterfaces()
Returns an array of ClassNodes representing the interfaces the class implements


setInterfaces

public void setInterfaces(ClassNode[] interfaces)

getMixins

public MixinNode[] getMixins()

getMethods

public java.util.List getMethods()
Returns a list containing MethodNode objects for each method in the class represented by this ClassNode


getAbstractMethods

public java.util.List getAbstractMethods()
Returns a list containing MethodNode objects for each abstract method in the class represented by this ClassNode


getAllDeclaredMethods

public java.util.List getAllDeclaredMethods()

getDeclaredMethodsMap

public java.util.Map getDeclaredMethodsMap()

getName

public java.lang.String getName()

setName

public java.lang.String setName(java.lang.String name)

getModifiers

public int getModifiers()

getProperties

public java.util.List getProperties()

getDeclaredConstructors

public java.util.List getDeclaredConstructors()

getModule

public ModuleNode getModule()

setModule

public void setModule(ModuleNode module)

addField

public void addField(FieldNode node)

addProperty

public void addProperty(PropertyNode node)

addProperty

public PropertyNode addProperty(java.lang.String name,
                                int modifiers,
                                ClassNode type,
                                Expression initialValueExpression,
                                Statement getterBlock,
                                Statement setterBlock)

hasProperty

public boolean hasProperty(java.lang.String name)

getProperty

public PropertyNode getProperty(java.lang.String name)

addConstructor

public void addConstructor(ConstructorNode node)

addConstructor

public ConstructorNode addConstructor(int modifiers,
                                      Parameter[] parameters,
                                      ClassNode[] exceptions,
                                      Statement code)

addMethod

public void addMethod(MethodNode node)

addMethod

public MethodNode addMethod(java.lang.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

public boolean hasDeclaredMethod(java.lang.String name,
                                 Parameter[] parameters)
See Also:
getDeclaredMethod(String, Parameter[])

hasMethod

public boolean hasMethod(java.lang.String name,
                         Parameter[] parameters)
See Also:
getMethod(String, Parameter[])

addSyntheticMethod

public MethodNode addSyntheticMethod(java.lang.String name,
                                     int modifiers,
                                     ClassNode returnType,
                                     Parameter[] parameters,
                                     ClassNode[] exceptions,
                                     Statement code)
Adds a synthetic method as part of the compilation process


addField

public FieldNode addField(java.lang.String name,
                          int modifiers,
                          ClassNode type,
                          Expression initialValue)

addInterface

public void addInterface(ClassNode type)

equals

public boolean equals(java.lang.Object o)
Overrides:
equals in class java.lang.Object

addMixin

public void addMixin(MixinNode mixin)

getDeclaredField

public FieldNode getDeclaredField(java.lang.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

getField

public FieldNode getField(java.lang.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

getOuterField

public FieldNode getOuterField(java.lang.String name)
Returns:
the field node on the outer class or null if this is not an inner class

getOuterClass

public ClassNode getOuterClass()
Helper method to avoid casting to inner class


addObjectInitializerStatements

public void addObjectInitializerStatements(Statement statements)

getObjectInitializerStatements

public java.util.List getObjectInitializerStatements()

addStaticInitializerStatements

public void addStaticInitializerStatements(java.util.List staticStatements,
                                           boolean fieldInit)

getDeclaredMethods

public java.util.List getDeclaredMethods(java.lang.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)

getMethods

public java.util.List getMethods(java.lang.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)

getDeclaredMethod

public MethodNode getDeclaredMethod(java.lang.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

getMethod

public MethodNode getMethod(java.lang.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

isDerivedFrom

public boolean isDerivedFrom(ClassNode type)
Returns:
true if this node is derived from the given class node

isDerivedFromGroovyObject

public boolean isDerivedFromGroovyObject()
Returns:
true if this class is derived from a groovy object i.e. it implements GroovyObject

implementsInterface

public boolean implementsInterface(ClassNode classNode)
Parameters:
classNode - the class node for the interface
Returns:
true if this class or any base class implements the given interface

declaresInterface

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 or indirectly the interface

getSuperClass

public ClassNode getSuperClass()
Returns:
the ClassNode of the super class of this type

getUnresolvedSuperClass

public ClassNode getUnresolvedSuperClass()

getUnresolvedSuperClass

public ClassNode getUnresolvedSuperClass(boolean useRedirect)

setUnresolvedSuperClass

public void setUnresolvedSuperClass(ClassNode sn)

getCompileUnit

public CompileUnit getCompileUnit()

setCompileUnit

protected void setCompileUnit(CompileUnit cu)

parametersEqual

protected boolean parametersEqual(Parameter[] a,
                                  Parameter[] b)
Returns:
true if the two arrays are of the same size and have the same contents

getPackageName

public java.lang.String getPackageName()
Returns:
the package name of this class

getNameWithoutPackage

public java.lang.String getNameWithoutPackage()

visitContents

public void visitContents(GroovyClassVisitor visitor)

getGetterMethod

public MethodNode getGetterMethod(java.lang.String getterName)

getSetterMethod

public MethodNode getSetterMethod(java.lang.String setterName)

isStaticClass

public boolean isStaticClass()
Is this class delcared 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

public java.lang.String toString()
Overrides:
toString in class java.lang.Object

hasPossibleMethod

public boolean hasPossibleMethod(java.lang.String name,
                                 Expression arguments)
Returns true if the given method has a possibly matching method with the given name and arguments


hasPossibleStaticMethod

public boolean hasPossibleStaticMethod(java.lang.String name,
                                       Expression arguments)
Returns true if the given method has a possibly matching static method with the given name and arguments


isInterface

public boolean isInterface()

isResolved

public boolean isResolved()

isArray

public boolean isArray()

getComponentType

public ClassNode getComponentType()

getTypeClass

public java.lang.Class getTypeClass()

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

public GenericsType[] getGenericsTypes()

setGenericsTypes

public void setGenericsTypes(GenericsType[] genericsTypes)

setGenericsPlaceHolder

public void setGenericsPlaceHolder(boolean b)

isGenericsPlaceHolder

public boolean isGenericsPlaceHolder()

isUsingGenerics

public boolean isUsingGenerics()

setUsingGenerics

public void setUsingGenerics(boolean b)

getPlainNodeReference

public ClassNode getPlainNodeReference()

Copyright © 2003-2008 The Codehaus. All rights reserved.