Groovy 1.7.0

## org.codehaus.groovy.ast
Class ClassNode

    java.lang.Object
      org.codehaus.groovy.ast.ASTNode
          org.codehaus.groovy.ast.AnnotatedNode
              org.codehaus.groovy.ast.ClassNode

All Implemented Interfaces:
  org.objectweb.asm.Opcodes

    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:

1. 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.
2. 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
3. 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.
   Note: the redirect mechanism is only allowed for classes that are not primary ClassNodes. Typically this is done for classes created by name only. 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.
   see:
     ClassHelper
   author:
     James Strachan
   author:
     Jochen Theodorou
   version:
     $Revision: 18396 $

   Nested Class Summary
   `class` | `ClassNode.MapOfLists`

   Field Summary
   `static ClassNode[]` | `EMPTY_ARRAY`
   `static ClassNode` | `SUPER`
   `static ClassNode` | `THIS`
   `protected Class` | `clazz`
   `protected List` | `innerClasses`
   `protected boolean` | `isPrimaryNode`
   `protected Object` | `lazyInitLock`

   Constructor Summary
   `ClassNode()`

   Method Summary
   `def` | `ClassNode(Class c)` Creates a ClassNode from a real class.
   `def` | `ClassNode(String name, int modifiers, ClassNode superClass)` base class
   `def` | `ClassNode(String name, int modifiers, ClassNode superClass, ClassNode[] interfaces, MixinNode[] mixins)` base 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 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 transform, ASTNode node)`
   `boolean` | `declaresInterface(ClassNode classNode)` or if one of its interfaces extends directly or indirectly the interface NOTE: Doesn't consider an interface to implement itself.
   `boolean` | `equals(Object o)`
   `List` | `getAbstractMethods()` ClassNode or null if there are no such methods
   `List` | `getAllDeclaredMethods()`
   `Set` | `getAllInterfaces()`
   `List` | `getAnnotations()`
   `List` | `getAnnotations(ClassNode type)`
   `CompileUnit` | `getCompileUnit()`
   `ClassNode` | `getComponentType()`
   `List` | `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` | `getDeclaredMethods(String name)` This methods returns a list of all methods of the given name defined in the current class
   `Map` | `getDeclaredMethodsMap()`
   `MethodNode` | `getEnclosingMethod()`
   `FieldNode` | `getField(String name)` Finds a field matching the given name in this class or a parent class.
   `List` | `getFields()`
   `GenericsType[]` | `getGenericsTypes()`
   `MethodNode` | `getGetterMethod(String getterName)`
   `Iterator` | `getInnerClasses()`
   `ClassNode[]` | `getInterfaces()`
   `MethodNode` | `getMethod(String name, Parameter[] parameters)` Finds a method matching the given name and parameters in this class or any parent class.
   `List` | `getMethods()`
   `List` | `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()`
   `int` | `getModifiers()`
   `ModuleNode` | `getModule()`
   `String` | `getName()`
   `String` | `getNameWithoutPackage()`
   `List` | `getObjectInitializerStatements()`
   `ClassNode` | `getOuterClass()` Helper method to avoid casting to inner class
   `FieldNode` | `getOuterField(String name)` inner class
   `PackageNode` | `getPackage()`
   `String` | `getPackageName()`
   `ClassNode` | `getPlainNodeReference()`
   `List` | `getProperties()`
   `PropertyNode` | `getProperty(String name)`
   `MethodNode` | `getSetterMethod(String setterName)`
   `ClassNode` | `getSuperClass()`
   `Map` | `getTransforms(CompilePhase phase)`
   `Class` | `getTypeClass()`
   `ClassNode[]` | `getUnresolvedInterfaces()`
   `ClassNode[]` | `getUnresolvedInterfaces(boolean useRedirect)`
   `String` | `getUnresolvedName()`
   `ClassNode` | `getUnresolvedSuperClass()`
   `ClassNode` | `getUnresolvedSuperClass(boolean useRedirect)`
   `boolean` | `hasDeclaredMethod(String name, Parameter[] parameters)`
   `boolean` | `hasMethod(String name, Parameter[] parameters)`
   `boolean` | `hasPackageName()`
   `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)`
   `int` | `hashCode()`
   `boolean` | `implementsInterface(ClassNode classNode)`
   `boolean` | `isAnnotated()`
   `boolean` | `isAnnotationDefinition()`
   `boolean` | `isArray()`
   `boolean` | `isDerivedFrom(ClassNode type)`
   `boolean` | `isDerivedFromGroovyObject()` i.e. it implements GroovyObject
   `boolean` | `isEnum()`
   `boolean` | `isGenericsPlaceHolder()`
   `boolean` | `isInterface()`
   `boolean` | `isPrimaryClassNode()`
   `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)`
   `void` | `positionStmtsAfterEnumInitStmts(List staticFieldStatements)`
   `ClassNode` | `redirect()` Returns the ClassNode this ClassNode is redirecting to.
   `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` | `setUnresolvedSuperClass(ClassNode sn)`
   `void` | `setUsingGenerics(boolean b)`
   `String` | `toString()`
   `MethodNode` | `tryFindPossibleMethod(String name, Expression arguments)`
   `void` | `visitContents(GroovyClassVisitor visitor)`

   Methods inherited from class AnnotatedNode
   addAnnotation, addAnnotations, getAnnotations, getAnnotations, getDeclaringClass, isSynthetic, setDeclaringClass, setSynthetic

   Methods inherited from class ASTNode
   getColumnNumber, getLastColumnNumber, getLastLineNumber, getLineNumber, getText, setColumnNumber, setLastColumnNumber, setLastLineNumber, setLineNumber, setSourcePosition, visit

   Methods inherited from class Object
   wait, wait, wait, hashCode, getClass, equals, toString, notify, notifyAll

   Field Detail

   ### EMPTY_ARRAY

       static final ClassNode[] EMPTY_ARRAY

   ### SUPER

       static final ClassNode SUPER

   ### THIS

       static final ClassNode THIS

   ### clazz

       protected Class clazz

   ### innerClasses

       protected List innerClasses

   ### isPrimaryNode

       protected boolean isPrimaryNode

   ### lazyInitLock

       protected Object lazyInitLock

   Constructor Detail

   ### ClassNode

       ClassNode()

   Method Detail

   ### ClassNode

       public def ClassNode(Class c)

     Creates a ClassNode from a real class. The resulting ClassNode will not be a primary ClassNode.

   ### ClassNode

       public def ClassNode(String name, int modifiers, ClassNode superClass)

     param:
       name is the full name of the class
     param:
       modifiers the modifiers,
     param:
       superClass the base class name - use "java.lang.Object" if no direct base class
     see:
       org.objectweb.asm.Opcodes

   ### ClassNode

       public def ClassNode(String name, int modifiers, ClassNode superClass, ClassNode[] interfaces, MixinNode[] mixins)

     param:
       name is the full name of the class
     param:
       modifiers the modifiers,
     param:
       superClass the base class name - use "java.lang.Object" if no direct base class
     param:
       interfaces the interfaces for this class
     param:
       mixins the mixins for this class
     see:
       org.objectweb.asm.Opcodes

   ### addConstructor

       public void addConstructor(ConstructorNode node)

   ### addConstructor

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

   ### addField

       public void addField(FieldNode node)

   ### addField

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

   ### addFieldFirst

       public void addFieldFirst(FieldNode node)

   ### addFieldFirst

       public FieldNode addFieldFirst(String name, int modifiers, ClassNode type, Expression initialValue)

   ### addInterface

       public void addInterface(ClassNode type)

   ### addMethod

       public void addMethod(MethodNode node)

   ### 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.

   ### addMixin

       public void addMixin(MixinNode mixin)

   ### addObjectInitializerStatements

       public void addObjectInitializerStatements(Statement statements)

     Adds a statement to the object initializer.
     param:
       statements the statement to be added

   ### addProperty

       public void addProperty(PropertyNode node)

   ### addProperty

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

   ### addStaticInitializerStatements

       public void addStaticInitializerStatements(List staticStatements, boolean fieldInit)

   ### 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

   ### addTransform

       public void addTransform(Class transform, ASTNode node)

   ### declaresInterface

       public boolean declaresInterface(ClassNode classNode)

     param:
       classNode the class node for the interface
     return:
       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.

   ### equals

       public boolean equals(Object o)

   ### getAbstractMethods

       public List getAbstractMethods()

     return:
       the list of abstract methods associated with this ClassNode or null if there are no such methods

   ### getAllDeclaredMethods

       public List getAllDeclaredMethods()

   ### getAllInterfaces

       public Set getAllInterfaces()

   ### getAnnotations

       public List getAnnotations()

   ### getAnnotations

       public List getAnnotations(ClassNode type)

   ### getCompileUnit

       public CompileUnit getCompileUnit()

   ### getComponentType

       public ClassNode getComponentType()

   ### getDeclaredConstructors

       public List getDeclaredConstructors()

   ### getDeclaredField

       public FieldNode getDeclaredField(String name)

     Finds a field matching the given name in this class.
     param:
       name the name of the field of interest
     return:
       the method matching the given name and parameters or null

   ### getDeclaredMethod

       public MethodNode getDeclaredMethod(String name, Parameter[] parameters)

     Finds a method matching the given name and parameters in this class.
     return:
       the method matching the given name and parameters or null

   ### getDeclaredMethods

       public List getDeclaredMethods(String name)

     This methods returns a list of all methods of the given name defined in the current class
     return:
       the method list
     see:
       #getMethods(String)

   ### getDeclaredMethodsMap

       public Map getDeclaredMethodsMap()

   ### getEnclosingMethod

       public MethodNode getEnclosingMethod()

   ### getField

       public FieldNode getField(String name)

     Finds a field matching the given name in this class or a parent class.
     param:
       name the name of the field of interest
     return:
       the method matching the given name and parameters or null

   ### getFields

       public List getFields()

     return:
       the list of FieldNode's associated with this ClassNode

   ### getGenericsTypes

       public GenericsType[] getGenericsTypes()

   ### getGetterMethod

       public MethodNode getGetterMethod(String getterName)

   ### getInnerClasses

       public Iterator getInnerClasses()

     return:
       iterator of inner classes defined inside this one

   ### getInterfaces

       public ClassNode[] getInterfaces()

     return:
       the array of interfaces which this ClassNode implements

   ### getMethod

       public MethodNode getMethod(String name, Parameter[] parameters)

     Finds a method matching the given name and parameters in this class or any parent class.
     return:
       the method matching the given name and parameters or null

   ### getMethods

       public List getMethods()

     return:
       the list of methods associated with this ClassNode

   ### getMethods

       public List getMethods(String name)

     This methods creates a list of all methods with this name of the current class and of all super classes
     return:
       the methods list
     see:
       #getDeclaredMethods(String)

   ### getMixins

       public MixinNode[] getMixins()

     return:
       the array of mixins associated with this ClassNode

   ### getModifiers

       public int getModifiers()

   ### getModule

       public ModuleNode getModule()

   ### getName

       public String getName()

   ### getNameWithoutPackage

       public String getNameWithoutPackage()

   ### getObjectInitializerStatements

       public List getObjectInitializerStatements()

   ### getOuterClass

       public ClassNode getOuterClass()

     Helper method to avoid casting to inner class

   ### getOuterField

       public FieldNode getOuterField(String name)

     return:
       the field node on the outer class or null if this is not an inner class

   ### getPackage

       public PackageNode getPackage()

   ### getPackageName

       public String getPackageName()

     return:
       the package name of this class

   ### getPlainNodeReference

       public ClassNode getPlainNodeReference()

   ### getProperties

       public List getProperties()

   ### getProperty

       public PropertyNode getProperty(String name)

   ### getSetterMethod

       public MethodNode getSetterMethod(String setterName)

   ### getSuperClass

       public ClassNode getSuperClass()

     return:
       the ClassNode of the super class of this type

   ### getTransforms

       public Map getTransforms(CompilePhase phase)

   ### getTypeClass

       public Class getTypeClass()

   ### getUnresolvedInterfaces

       public ClassNode[] getUnresolvedInterfaces()

   ### getUnresolvedInterfaces

       public ClassNode[] getUnresolvedInterfaces(boolean useRedirect)

   ### getUnresolvedName

       public String getUnresolvedName()

   ### getUnresolvedSuperClass

       public ClassNode getUnresolvedSuperClass()

   ### getUnresolvedSuperClass

       public ClassNode getUnresolvedSuperClass(boolean useRedirect)

   ### hasDeclaredMethod

       public boolean hasDeclaredMethod(String name, Parameter[] parameters)

     see:
       #getDeclaredMethod(String, Parameter[])

   ### hasMethod

       public boolean hasMethod(String name, Parameter[] parameters)

     see:
       #getMethod(String, Parameter[])

   ### hasPackageName

       public boolean hasPackageName()

   ### hasPossibleMethod

       public boolean hasPossibleMethod(String name, Expression arguments)

     Returns true if the given method has a possibly matching instance method with the given name and arguments.
     param:
       name the name of the method of interest
     param:
       arguments the arguments to match against
     return:
       true if a matching method was found

   ### hasPossibleStaticMethod

       public boolean hasPossibleStaticMethod(String name, Expression arguments)

     Returns true if the given method has a possibly matching static method with the given name and arguments.
     param:
       name the name of the method of interest
     param:
       arguments the arguments to match against
     return:
       true if a matching method was found

   ### hasProperty

       public boolean hasProperty(String name)

   ### hashCode

       public int hashCode()

   ### implementsInterface

       public boolean implementsInterface(ClassNode classNode)

     param:
       classNode the class node for the interface
     return:
       true if this class or any base class implements the given interface

   ### isAnnotated

       public boolean isAnnotated()

   ### isAnnotationDefinition

       public boolean isAnnotationDefinition()

   ### isArray

       public boolean isArray()

   ### isDerivedFrom

       public boolean isDerivedFrom(ClassNode type)

     param:
       type the ClassNode of interest
     return:
       true if this node is derived from the given ClassNode

   ### isDerivedFromGroovyObject

       public boolean isDerivedFromGroovyObject()

     return:
       true if this class is derived from a groovy object i.e. it implements GroovyObject

   ### isEnum

       public boolean isEnum()

   ### isGenericsPlaceHolder

       public boolean isGenericsPlaceHolder()

   ### isInterface

       public boolean isInterface()

   ### isPrimaryClassNode

       public boolean isPrimaryClassNode()

     return:
       true if this instance is a primary ClassNode

   ### isResolved

       public boolean isResolved()

   ### isScript

       public boolean isScript()

   ### isScriptBody

       public boolean isScriptBody()

     return:
       Returns true if this inner class or closure was declared inside a script body

   ### isStaticClass

       public boolean isStaticClass()

     Is this class delcared in a static method (such as a closure / inner class declared in a static method)

   ### isUsingGenerics

       public boolean isUsingGenerics()

   ### makeArray

       public ClassNode makeArray()

     Returns a ClassNode representing an array of the class represented by this ClassNode

   ### parametersEqual

       protected boolean parametersEqual(Parameter[] a, Parameter[] b)

     return:
       true if the two arrays are of the same size and have the same contents

   ### positionStmtsAfterEnumInitStmts

       public void positionStmtsAfterEnumInitStmts(List staticFieldStatements)

   ### redirect

       public ClassNode redirect()

     Returns the ClassNode this ClassNode is redirecting to.

   ### renameField

       public void renameField(String oldName, String newName)

   ### setAnnotated

       public void setAnnotated(boolean flag)

     Marks if the current class uses annotations or not
     param:
       flag

   ### setCompileUnit

       protected void setCompileUnit(CompileUnit cu)

   ### setEnclosingMethod

       public void setEnclosingMethod(MethodNode enclosingMethod)

   ### setGenericsPlaceHolder

       public void setGenericsPlaceHolder(boolean b)

   ### setGenericsTypes

       public void setGenericsTypes(GenericsType[] genericsTypes)

   ### setInterfaces

       public void setInterfaces(ClassNode[] interfaces)

   ### setModifiers

       public void setModifiers(int modifiers)

   ### setModule

       public void setModule(ModuleNode module)

   ### setName

       public String setName(String name)

   ### setRedirect

       public void setRedirect(ClassNode cn)

     Sets this instance as proxy for the given ClassNode.
     param:
       cn the class to redirect to. If set to null the redirect will be removed

   ### setScript

       public void setScript(boolean script)

   ### setScriptBody

       public void setScriptBody(boolean scriptBody)

   ### setStaticClass

       public void setStaticClass(boolean staticClass)

   ### setSuperClass

       public void setSuperClass(ClassNode superClass)

     Sets the superclass of this ClassNode

   ### setUnresolvedSuperClass

       public void setUnresolvedSuperClass(ClassNode sn)

   ### setUsingGenerics

       public void setUsingGenerics(boolean b)

   ### toString

       public String toString()

   ### tryFindPossibleMethod

       public MethodNode tryFindPossibleMethod(String name, Expression arguments)

   ### visitContents

       public void visitContents(GroovyClassVisitor visitor)

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