Top Description Inners Fields Constructors Methods
org.codehaus.groovy.ast

public Class ClassNode

extends AnnotatedNode
implements Opcodes
Class Inheritance
All Implemented Interfaces
org.objectweb.asm.Opcodes
Known Direct Subclasses
org.codehaus.groovy.ast.CompileUnit.ConstructedOuterNestedClassNode, org.codehaus.groovy.ast.InnerClassNode, org.codehaus.groovy.ast.MixinNode, org.codehaus.groovy.ast.decompiled.DecompiledClassNode, org.codehaus.groovy.ast.tools.WideningCategories.LowestUpperBoundClassNode, org.codehaus.groovy.control.ResolveVisitor.ConstructedNestedClass, org.codehaus.groovy.control.ResolveVisitor.ConstructedClassWithPackage, org.codehaus.groovy.control.ResolveVisitor.LowerCaseClass, org.codehaus.groovy.transform.stc.UnionTypeClassNode
Imports
org.apache.groovy.ast.tools.ClassNodeUtils, org.codehaus.groovy.GroovyBugError, org.codehaus.groovy.ast.expr.BinaryExpression, .Expression, .FieldExpression, .TupleExpression, org.codehaus.groovy.ast.stmt.BlockStatement, .ExpressionStatement, .Statement, org.codehaus.groovy.ast.tools.ParameterUtils, org.codehaus.groovy.control.CompilePhase, org.codehaus.groovy.transform.ASTTransformation, .GroovyASTTransformation, org.codehaus.groovy.vmplugin.VMPluginFactory, org.objectweb.asm.Opcodes, java.lang.reflect.Array, java.util.ArrayList, .Collections, .EnumMap, .Iterator, .LinkedHashMap, .LinkedHashSet, .LinkedList, .List, .ListIterator, .Map, .Optional, .Set

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

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 Also
org.codehaus.groovy.ast.ClassHelper

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class

Field Summary

Modifier and TypeField and Description
private boolean
protected Class<T>
private CompileUnit
private ClassNode
private List<ConstructorNode>
public static final ClassNode[]
private MethodNode
enclosingMethod

Tracks the enclosing method for local inner classes.

private Map<String, FieldNode>
private List<FieldNode>
private GenericsType[]
protected List<InnerClassNode>
private ClassNode[]
protected boolean
private volatile boolean
protected final Object
private ClassNode.MapOfLists
private List<MethodNode>
private MixinNode[]
private int
private ModuleNode
private String
private List<Statement>
private boolean
private List<PropertyNode>
private ClassNode
private boolean
private boolean
private boolean
public static final ClassNode
private ClassNode
private boolean
public static final ClassNode
private Map<CompilePhase, Map<Class<? extends ASTTransformation>, Set<ASTNode>>>
transformInstances

The AST Transformations to be applied during compilation.

private boolean

Constructor Summary

AccessConstructor and Description
private
ClassNode(ClassNode componentType)

Constructor used by makeArray() if no real class is available.

private
ClassNode(Class<?> c, ClassNode componentType)

Constructor used by makeArray() if a real class is available.

public
ClassNode(Class<?> c)

Creates a non-primary ClassNode from a real class.

public
ClassNode(String
the fully-qualified name of the class
name
,
int
the modifiers; see org.objectweb.asm.Opcodes
modifiers
,
ClassNode
the base class; use "java.lang.Object" if no direct base class
superClass
)

public
ClassNode(String
the fully-qualified name of the class
name
,
int
the modifiers; see org.objectweb.asm.Opcodes
modifiers
,
ClassNode
the base class; use "java.lang.Object" if no direct base class
superClass
,
ClassNode[]
the interfaces for this class
interfaces
,
MixinNode[]
the mixins for this class
mixins
)

Method Summary

Modifier and TypeMethod and Description
public void
public ConstructorNode
addConstructor(int modifiers, Parameter[] parameters, ClassNode[] exceptions, Statement code)

public void
private void
addField(FieldNode node, boolean isFirst)

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

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

public void
public void
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
public void
addObjectInitializerStatements(Statement
the statement to be added
statements
)

Adds a statement to the object initializer.

public void
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
public boolean

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
declaresAnyInterfaces
(ClassNode...
the class nodes for the interfaces
classNodes
)

public boolean

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.
declaresInterface
(ClassNode
the class node for the interface
classNode
)

public boolean
equals(Object
the reference object with which to compare.
that
)

Overrides java.lang.Object.equals.

Indicates whether some other object is "equal to" this one.
private String
genericTypeAsString(GenericsType genericsType)

Avoids a recursive definition of toString.

public List<MethodNode>

Returns:

the abstract methods associated with this ClassNode
getAbstractMethods
()

public List<MethodNode>
public Set<ClassNode>
private void
public List<AnnotationNode>
public List<AnnotationNode>
public CompileUnit
public ClassNode
public ConstructorNode

Returns:

the constructor matching the given parameters or null
getDeclaredConstructor
(Parameter[] parameters)

public List<ConstructorNode>
public FieldNode

Returns:

the method matching the given name and parameters or null
getDeclaredField
(String
the name of the field of interest
name
)

Finds a field matching the given name in this class.

public MethodNode

Returns:

the method matching the given name and parameters or null
getDeclaredMethod
(String name, Parameter[] parameters)

Finds a method matching the given name and parameters in this class.

public List<MethodNode>

Returns:

the method list
getDeclaredMethods
(String name)

This method returns a list of all methods of the given name defined in the current class

public Map<String, MethodNode>
public MethodNode
public FieldNode

Returns:

the method matching the given name and parameters or null
getField
(String
the name of the field of interest
name
)

Finds a field matching the given name in this class or a parent class.

public Map<String, FieldNode>
public List<FieldNode>

Returns:

the fields associated with this ClassNode
getFields
()

public GenericsType[]
public MethodNode
getGetterMethod(String getterName)

public MethodNode
getGetterMethod(String getterName, boolean searchSuperClasses)

public Iterator<InnerClassNode>

Returns:

iterator of inner classes defined inside this one
getInnerClasses
()

public ClassNode[]

Returns:

the interfaces implemented by this ClassNode
getInterfaces
()

public MethodNode

Returns:

the method matching the given name and parameters or null
getMethod
(String name, Parameter[] parameters)

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

public List<MethodNode>

Returns:

the methods associated with this ClassNode
getMethods
()

public List<MethodNode>

Returns:

the methods list
getMethods
(String name)

This method creates a list of all methods with this name of the current class and of all super classes

public MixinNode[]

Returns:

the mixins associated with this ClassNode
getMixins
()

public int
public ModuleNode
public String
public String
public List<Statement>
private MethodNode
public ClassNode
public List<ClassNode>
public FieldNode

Returns:

the field on the outer class or null if this is not an inner class
getOuterField
(String name)

public PackageNode
public String
public ClassNode
public List<PropertyNode>
public PropertyNode
public MethodNode
getSetterMethod(String setterName)

public MethodNode
getSetterMethod(String setterName, boolean voidOnly)

public ClassNode

Returns:

the ClassNode of the super class of this type
getSuperClass
()

public String
private Map<CompilePhase, Map<Class<? extends ASTTransformation>, Set<ASTNode>>>
public Map<Class<? extends ASTTransformation>, Set<ASTNode>>
public Class<T>

Returns:

the class this classnode relates to. May return null.
getTypeClass
()

Returns the concrete class this classnode relates to.

public ClassNode[]
public ClassNode[]
getUnresolvedInterfaces(boolean useRedirect)

public String
public ClassNode
public ClassNode
getUnresolvedSuperClass(boolean useRedirect)

private static boolean
hasCompatibleNumberOfArgs(final MethodNode method, final int nArgs)

private static boolean
hasCompatibleType(final TupleExpression args, final MethodNode method, final int i)

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

private static boolean
hasExactMatchingCompatibleType(final MethodNode match, final MethodNode maybe, final int i)

public int
hashCode()

Overrides java.lang.Object.hashCode.

Returns a hash code value for the object.
public boolean
hasMethod(String name, Parameter[] parameters)

public boolean
public boolean

Returns:

true if a matching method was found
hasPossibleMethod
(final String
the name of the method of interest
name
,
final Expression
the arguments to match against
arguments
)

Determines if the type has a possibly-matching instance method with the given name and arguments.

public boolean

Returns:

true if a matching method was found
hasPossibleStaticMethod
(final String
the name of the method of interest
name
,
final Expression
the arguments to match against
arguments
)

Checks if the given method has a possibly matching static method with the given name and arguments.

public boolean
public boolean

Returns:

true if this type implements any of the given interfaces
implementsAnyInterfaces
(ClassNode...
the class nodes for the interfaces
classNodes
)

public boolean

Returns:

true if this type implements the given interface
implementsInterface
(ClassNode
the class node for the interface
classNode
)

public boolean
public boolean
public boolean
public boolean
public boolean

Returns:

true if this node is derived from the given ClassNode
isDerivedFrom
(ClassNode
the ClassNode of interest
type
)

public boolean

Returns:

true if this type implements GroovyObject
isDerivedFromGroovyObject
()

public boolean
public boolean
public boolean
private static boolean
isPotentialVarArg(final MethodNode method, final int lastParamIndex)

public boolean

Returns:

true if this instance is a primary ClassNode
isPrimaryClassNode
()

public boolean
public boolean
public boolean
public boolean

Returns:

true if this inner class or closure was declared inside a script body
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

Returns:

true if node is public but had no explicit public modifier
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
private void
lazyClassInit()

The complete class structure will be initialized only when really needed to avoid having too many objects during compilation.

public ClassNode
makeArray()

Returns a ClassNode representing an array of the type represented by this.

protected boolean

Returns:

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

public void
public ClassNode
redirect()

Returns the ClassNode this node is a proxy for or the node itself.

public void
public void
public void
public void
renameField(String oldName, String newName)

public void
setAnnotated(boolean annotated)

Marks if the current class uses annotations or not.

protected void
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
public String
public void
setRedirect(ClassNode
the class to redirect to; if null the redirect is removed
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
public void
setUsingGenerics(boolean usesGenerics)

public String
toString()

Overrides java.lang.Object.toString.

Returns a string representation of the object.
public String
toString(boolean showRedirect)

private String
public MethodNode
tryFindPossibleMethod(final String name, final Expression arguments)

public void
private void
Inherited from org.codehaus.groovy.ast.AnnotatedNode:
addAnnotationaddAnnotationsgetDeclaringClassgetGroovydocgetInstancehasNoRealSourcePositionisSyntheticsetDeclaringClasssetHasNoRealSourcePositionsetSynthetic