Class ClassNode

All Implemented Interfaces:
GroovydocHolder<AnnotatedNode>, NodeMetaDataHandler
Direct Known Subclasses:
DecompiledClassNode, InnerClassNode, MixinNode, WideningCategories.LowestUpperBoundClassNode

public 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 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:
  • Field Details

    • EMPTY_ARRAY

      public static final ClassNode[] EMPTY_ARRAY
    • THIS

      public static final ClassNode THIS
    • SUPER

      public static final ClassNode SUPER
    • isPrimaryNode

      protected boolean isPrimaryNode
    • lazyInitLock

      protected final Object lazyInitLock
    • clazz

      protected Class<?> clazz
  • Constructor Details

    • ClassNode

      public ClassNode(String name, int modifiers, ClassNode superClass, ClassNode[] interfaces, MixinNode[] mixins)
      Constructs a primary ClassNode representing a class declaration with all components. This constructor creates a new ClassNode that will be compiled by Groovy. The node represents an actual source class definition with specified interfaces and mixins.
      Parameters:
      name - the fully-qualified name of the class (e.g., "com.example.MyClass")
      modifiers - the bytecode modifiers (flags from Modifier or Opcodes)
      superClass - the base class (ClassNode representing the parent type); use ClassHelper.OBJECT_TYPE if no direct base class
      interfaces - array of ClassNode instances for interfaces implemented by this class; may be empty
      mixins - array of MixinNode instances to be mixed into this class; may be empty
    • ClassNode

      public ClassNode(String name, int modifiers, ClassNode superClass)
      Constructs a primary ClassNode representing a class declaration with superclass. This is a convenience constructor for creating classes with a single superclass and no interfaces or mixins. It delegates to the full constructor with empty interface and mixin arrays.
      Parameters:
      name - the fully-qualified name of the class (e.g., "com.example.MyClass")
      modifiers - the bytecode modifiers (flags from Modifier or Opcodes)
      superClass - the base class (ClassNode representing the parent type); use ClassHelper.OBJECT_TYPE if no direct base class
    • ClassNode

      public ClassNode(Class<?> c)
      Constructs a non-primary ClassNode from a real Java class. This constructor creates a ClassNode that represents an actual Java class loaded at runtime (not a Groovy source class). The class information is lazily initialized from reflection, and the node will not be compiled by Groovy.
      Parameters:
      c - the Java class (Class) to wrap; must not be null
  • Method Details

    • redirect

      public ClassNode redirect()
      Returns the ClassNode this node is a proxy for or the node itself. If this ClassNode has been set as a proxy to another ClassNode via setRedirect(ClassNode), recursively follows the redirect chain to return the ultimate target ClassNode. If no redirect is set, returns this. This mechanism allows lazy resolution of class references during compilation.
      Returns:
      the final ClassNode in the redirect chain; never null
    • isRedirectNode

      public boolean isRedirectNode()
      Returns whether this ClassNode is a redirect (proxy) node for another ClassNode. A redirect node acts as a placeholder that forwards most method calls to its target ClassNode. This mechanism is used during compilation to defer class resolution until the actual target class is available. The actual target can be found using redirect().
      Returns:
      true if this node redirects to another ClassNode; false if it is a standalone node
      See Also:
    • setRedirect

      public void setRedirect(ClassNode node)
      Sets this instance as a proxy for the given ClassNode, enabling deferred class resolution. After calling this method, most operations on this ClassNode are forwarded to the target ClassNode. Redirect is only allowed for non-primary ClassNodes (see isPrimaryClassNode()). If the target is null, the redirect is removed, making this node standalone again.
      Parameters:
      node - the ClassNode to redirect to; if null the redirect is cleared
      Throws:
      GroovyBugError - if this is a primary ClassNode, as redirects are only for proxy nodes
    • isPrimaryClassNode

      public boolean isPrimaryClassNode()
      Determines whether this ClassNode is a primary node or redirects to one. A primary ClassNode represents actual Groovy source code that will be compiled into bytecode. Returns true if this node is primary or if its redirect target (or array component type) is a primary ClassNode. Non-primary nodes are typically loaded via reflection.
      Returns:
      true if this is a primary ClassNode or redirects to one; false if it only represents a reflected class
    • getPlainNodeReference

      public ClassNode getPlainNodeReference()
    • getPlainNodeReference

      public ClassNode getPlainNodeReference(boolean skipPrimitives)
    • getModule

      public ModuleNode getModule()
    • setModule

      public void setModule(ModuleNode module)
    • getCompileUnit

      public CompileUnit getCompileUnit()
    • setCompileUnit

      @Deprecated(forRemoval=true, since="5.0.0") protected void setCompileUnit(CompileUnit cu)
      Deprecated, for removal: This API element is subject to removal in a future version.
    • getPackage

      public PackageNode getPackage()
    • getPackageName

      public String getPackageName()
    • hasPackageName

      public boolean hasPackageName()
    • getNameWithoutPackage

      public String getNameWithoutPackage()
    • getUnresolvedName

      public String getUnresolvedName()
    • getUnresolvedSuperClass

      public ClassNode getUnresolvedSuperClass()
      Returns the unresolved super class of this ClassNode, using redirect resolution by default. The unresolved super class represents the raw class hierarchy without applying redirect resolution. Returns null if this class has no explicit superclass.
      Returns:
      the unresolved ClassNode representing the super class, or null
      See Also:
    • getUnresolvedSuperClass

      public ClassNode getUnresolvedSuperClass(boolean deref)
      Returns the unresolved super class of this ClassNode with optional redirect resolution. When deref is true, resolves redirects recursively; otherwise returns the raw superclass stored in this ClassNode. Resolving may trigger lazy class initialization.
      Parameters:
      deref - true to apply redirect resolution, false to return raw superclass
      Returns:
      the unresolved ClassNode representing the super class, or null
      See Also:
    • setUnresolvedSuperClass

      public void setUnresolvedSuperClass(ClassNode superClass)
      Sets the unresolved super class for this ClassNode. This method sets the raw superclass without applying any redirect resolution.
      Parameters:
      superClass - the ClassNode representing the super class, or null
    • getUnresolvedInterfaces

      public ClassNode[] getUnresolvedInterfaces()
      Returns the unresolved interfaces implemented by this ClassNode, using redirect resolution by default. The unresolved interfaces represent the raw interface hierarchy without applying redirect resolution. Returns an empty array if this class implements no interfaces.
      Returns:
      an array of ClassNode representing the interfaces, never null
      See Also:
    • getUnresolvedInterfaces

      public ClassNode[] getUnresolvedInterfaces(boolean deref)
      Returns the unresolved interfaces implemented by this ClassNode with optional redirect resolution. When deref is true, resolves redirects recursively; otherwise returns the raw interfaces stored in this ClassNode. Resolving may trigger lazy class initialization.
      Parameters:
      deref - true to apply redirect resolution, false to return raw interfaces
      Returns:
      an array of ClassNode representing the interfaces, never null
      See Also:
    • getText

      public String getText()
      Description copied from class: ASTNode
      Returns a human-readable text representation of this AST node. Used for debugging and error messages. Default implementation returns a message indicating the representation is not yet implemented for this node type.
      Overrides:
      getText in class ASTNode
      Returns:
      text representation of this node, or placeholder for unimplemented types
    • getName

      public String getName()
      Returns the fully-qualified name of this class after following any redirects. If this ClassNode is a proxy for another ClassNode, the name of the target is returned. The name remains consistent across all compilation phases for the redirected type.
      Returns:
      the fully-qualified class name (e.g., "com.example.MyClass" or "int[]")
    • setName

      public String setName(String name)
      Sets the fully-qualified name of this class, delegating to the redirect if one exists. Allows the class name to be updated after construction, which is occasionally used during AST transformation phases. Changes are propagated to the redirect target if present.
      Parameters:
      name - the new fully-qualified class name to set
      Returns:
      the name that was set
    • getModifiers

      public int getModifiers()
      Returns the modifier flags for this ClassNode, combining visibility and behavioral modifiers. The flags are represented as a bitmask using ASM/bytecode conventions (e.g., ACC_PUBLIC, ACC_ABSTRACT, ACC_INTERFACE, ACC_ENUM). This method follows redirects if present, ensuring the modifiers of the actual target ClassNode are returned. Individual modifiers can be tested using bitwise AND operations with the ASM Opcodes constants.
      Returns:
      the modifier flags as an integer bitmask
      See Also:
    • setModifiers

      public void setModifiers(int modifiers)
      Sets the modifier flags for this ClassNode. The modifiers are a bitmask of ASM/bytecode convention flags (e.g., ACC_PUBLIC, ACC_ABSTRACT, ACC_INTERFACE, ACC_ENUM). Calling this method directly on a redirect node updates the redirect's modifiers instead of this node's. Use this method to control visibility and structural attributes like abstractness or finality.
      Parameters:
      modifiers - the new modifier flags as a bitmask
      See Also:
    • getSuperClass

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

      public void setSuperClass(ClassNode superClass)
      Sets the super class of this ClassNode. If this ClassNode has a redirect, the super class is set on the redirect instead. Updates the generics usage indicator if the super class uses generics and this is a primary ClassNode.
      Parameters:
      superClass - the ClassNode representing the super class, or null
      See Also:
    • getInterfaces

      public ClassNode[] getInterfaces()
      Returns:
      the interfaces implemented by this ClassNode
    • getAllInterfaces

      public Set<ClassNode> getAllInterfaces()
    • setInterfaces

      public void setInterfaces(ClassNode[] interfaces)
      Sets the interfaces implemented by this ClassNode. If this ClassNode has a redirect, the interfaces are set on the redirect instead. Updates the generics usage indicator if any interface uses generics and this is a primary ClassNode.
      Parameters:
      interfaces - an array of ClassNode representing the interfaces, or null
      See Also:
    • getMixins

      public MixinNode[] getMixins()
      Returns:
      the mixins associated with this ClassNode
    • setMixins

      public void setMixins(MixinNode[] mixins)
    • getPermittedSubclasses

      @Incubating public List<ClassNode> getPermittedSubclasses()
      Returns:
      permitted subclasses of sealed type, may initially be empty in early compiler phases
    • setPermittedSubclasses

      @Incubating public void setPermittedSubclasses(List<ClassNode> permittedSubclasses)
    • getRecordComponents

      @Incubating public List<RecordComponentNode> getRecordComponents()
      Gets the record components of record type.
      Returns:
      RecordComponentNode instances
      Since:
      4.0.0
    • setRecordComponents

      @Incubating public void setRecordComponents(List<RecordComponentNode> recordComponents)
      Sets the record components for record type.
      Since:
      4.0.0
    • addInterface

      public void addInterface(ClassNode node)
      Adds an interface to the list of interfaces implemented by this ClassNode. If the interface is already implemented, this method has no effect. The interface is appended to the end of the current interfaces array.
      Parameters:
      node - the ClassNode representing the interface to add
      See Also:
    • addMixin

      public void addMixin(MixinNode node)
    • addField

      public void addField(FieldNode node)
      Adds a FieldNode to this ClassNode. The field is added to the end of the field list and its declaring class is set to this node's redirect target. The field is registered in the internal field index for fast lookup by name.
      Parameters:
      node - the FieldNode to add, must not be null
      See Also:
    • addField

      public FieldNode addField(String name, int modifiers, ClassNode type, Expression initialValue)
      Creates and adds a FieldNode to this ClassNode with the specified properties. The field is initialized with the given name, modifiers, type, and optional initial value expression. This is a convenience method that creates a FieldNode and adds it via addField(FieldNode).
      Parameters:
      name - the name of the field
      modifiers - the access modifiers for the field (e.g., ACC_PUBLIC, ACC_PRIVATE)
      type - the ClassNode representing the field's type
      initialValue - the initial value Expression, or null if no initializer
      Returns:
      the newly created FieldNode
      See Also:
    • addFieldFirst

      public void addFieldFirst(FieldNode node)
      Adds a FieldNode to this ClassNode at the beginning of the field list. This method is similar to addField(FieldNode) but prepends the field instead of appending it, which can be useful when field declaration order matters.
      Parameters:
      node - the FieldNode to add first, must not be null
      See Also:
    • addFieldFirst

      public FieldNode addFieldFirst(String name, int modifiers, ClassNode type, Expression initialValue)
      Creates and adds a FieldNode to the beginning of this ClassNode's field list with the specified properties. This is a convenience method for adding fields where declaration order is significant.
      Parameters:
      name - the name of the field
      modifiers - the access modifiers for the field (e.g., ACC_PUBLIC, ACC_PRIVATE)
      type - the ClassNode representing the field's type
      initialValue - the initial value Expression, or null if no initializer
      Returns:
      the newly created FieldNode
      See Also:
    • addProperty

      public void addProperty(PropertyNode node)
      Adds a PropertyNode to this ClassNode. The property's associated field is also added to the field list, and the property itself is registered in the properties list. The property's declaring class is set to this node's redirect target.
      Parameters:
      node - the PropertyNode to add, must not be null
      See Also:
    • addProperty

      public PropertyNode addProperty(String name, int modifiers, ClassNode type, Expression initialValue, Statement getterBlock, Statement setterBlock)
      Creates and adds a PropertyNode to this ClassNode with the specified properties, or returns an existing property with the same name if one is already present. If a property with the given name exists, the method updates its getter block, setter block, and initial value expression if they are currently null. This prevents duplicate properties while allowing partial initialization across multiple calls.
      Parameters:
      name - the name of the property
      modifiers - the access modifiers for the property (e.g., ACC_PUBLIC, ACC_PRIVATE)
      type - the ClassNode representing the property's type
      initialValue - the initial value Expression, or null if no initializer
      getterBlock - the getter method's Statement body, or null
      setterBlock - the setter method's Statement body, or null
      Returns:
      the newly created PropertyNode or the existing property if one with this name is already present
      See Also:
    • addMethod

      public void addMethod(MethodNode node)
      Adds a MethodNode to this ClassNode. The method is added to the internal methods list and registered by name in the methods map for fast lookup. The method's declaring class is set to this node's redirect target. This method does not check for duplicates.
      Parameters:
      node - the MethodNode to add, must not be null
      See Also:
    • addMethod

      public MethodNode addMethod(String name, int modifiers, ClassNode returnType, Parameter[] parameters, ClassNode[] exceptions, Statement code)
      Creates and adds a MethodNode to this ClassNode with the specified properties, but only if a method with the same name and parameter signature does not already exist. If a matching method is already declared in this class, it is returned without adding a new one. This method is useful for adding default method implementations (e.g., getProperty(), invokeMethod()) where the class may already have a user-defined version.
      Parameters:
      name - the name of the method
      modifiers - the access modifiers (e.g., ACC_PUBLIC, ACC_STATIC)
      returnType - the ClassNode representing the return type
      parameters - array of Parameters, or an empty array for no parameters
      exceptions - array of exception ClassNodes that the method declares, or an empty array
      code - the method body as a Statement, or null
      Returns:
      the newly created MethodNode or an existing method if one matches the name and parameters
      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.
    • addConstructor

      public void addConstructor(ConstructorNode node)
    • addConstructor

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

      public void addObjectInitializerStatements(Statement statement)
      Adds a statement to the object initializer.
      Parameters:
      statement - the statement to be added
    • addStaticInitializerStatements

      public void addStaticInitializerStatements(List<Statement> statements, boolean fieldInit)
    • positionStmtsAfterEnumInitStmts

      public void positionStmtsAfterEnumInitStmts(List<Statement> staticFieldInitializerStatements)
    • getFields

      public List<FieldNode> getFields()
      Returns all FieldNodes declared in this ClassNode, including inherited fields. If this node is a proxy (has a redirect), the call is forwarded to the redirect target. The returned list includes fields from this class only, not from super classes.
      Returns:
      an unmodifiable or mutable list of FieldNodes associated with this ClassNode; an empty list if this class declares no fields
      See Also:
    • getProperties

      public List<PropertyNode> getProperties()
      Returns all PropertyNodes declared in this ClassNode. If this node is a proxy (has a redirect), the call is forwarded to the redirect target. Properties are distinct from fields and represent a higher-level abstraction with getter/setter semantics.
      Returns:
      a mutable list of PropertyNodes associated with this ClassNode; an empty list if this class declares no properties
      See Also:
    • getFieldIndex

      @Deprecated(forRemoval=true, since="5.0.0") public Map<String,FieldNode> getFieldIndex()
      Deprecated, for removal: This API element is subject to removal in a future version.
      for removal since 5.0.0; use getField(String) or getDeclaredField(String) instead
      Returns the internal field index map for fast field lookup by name. This method is deprecated as field access should go through getField(String) or getDeclaredField(String). The field index is an internal implementation detail and may change in future versions.
      Returns:
      a map from field name to FieldNode, or null if no field index exists
      See Also:
    • hasProperty

      public boolean hasProperty(String name)
      Tests whether a property with the given name exists in this ClassNode. This method searches through the properties list, comparing by name.
      Parameters:
      name - the name of the property to check
      Returns:
      true if a property with the given name exists, false otherwise
      See Also:
    • getProperty

      public PropertyNode getProperty(String name)
      Finds a PropertyNode matching the given name in this ClassNode. This method searches through the properties list, comparing by name. Only direct properties of this class are searched, not inherited ones.
      Parameters:
      name - the name of the property to find
      Returns:
      the PropertyNode with the given name, or null if not found
      See Also:
    • getMethods

      public List<MethodNode> getMethods()
      Returns all MethodNodes declared in this ClassNode, including inherited and synthetic methods. If this node is a proxy (has a redirect), the call is forwarded to the redirect target. This list includes constructors and all instance and static methods defined for this class.
      Returns:
      a list of all MethodNodes associated with this ClassNode; an empty list if this class declares no methods
      See Also:
    • getAbstractMethods

      public List<MethodNode> getAbstractMethods()
      Returns a list of all abstract MethodNodes declared in this ClassNode. Abstract methods are obtained from the declared methods map and filtered to return only those with the abstract modifier set. This includes abstract methods from superclasses and implemented interfaces.
      Returns:
      a list of abstract MethodNodes (possibly empty); returns an empty list if there are no abstract methods
      See Also:
    • getDeclaredMethodsMap

      public Map<String,MethodNode> getDeclaredMethodsMap()
      Returns all declared MethodNodes from this ClassNode and its superclasses and interfaces. The methods are collected in a single map with method type descriptors as keys, allowing resolution of method overrides across the inheritance hierarchy. This method provides a complete view of all accessible methods.
      Returns:
      a map from method type descriptor (name and signature) to MethodNode; includes methods from superclasses and interfaces
      See Also:
    • getAllDeclaredMethods

      public List<MethodNode> getAllDeclaredMethods()
      Returns all declared MethodNodes from this ClassNode and its superclasses and interfaces as a list. This is a convenience method that collects all values from the declared methods map into a single list.
      Returns:
      a list of all MethodNodes declared in this class, superclasses, and interfaces; an empty list if no methods are found
      See Also:
    • getDeclaredConstructors

      public List<ConstructorNode> getDeclaredConstructors()
    • getDeclaredConstructor

      public ConstructorNode getDeclaredConstructor(Parameter[] parameters)
      Returns:
      the constructor matching the given parameters or null
    • hasMethod

      public boolean hasMethod(String name, Parameter[] parameters)
      Tests whether a method with the given name and parameter signature exists in this class or any of its superclasses. This is a convenience method for checking method existence.
      Parameters:
      name - the name of the method to check
      parameters - an array of Parameters representing the method signature, or null/empty for methods with no parameters
      Returns:
      true if a method with the given name and parameters exists, false otherwise
      See Also:
    • hasDeclaredMethod

      public boolean hasDeclaredMethod(String name, Parameter[] parameters)
      Tests whether a method with the given name and parameter signature is declared directly in this class (not inherited from a superclass). This is a convenience method for checking method existence.
      Parameters:
      name - the name of the method to check
      parameters - an array of Parameters representing the method signature, or null/empty for methods with no parameters
      Returns:
      true if a method with the given name and parameters is declared in this class, false otherwise
      See Also:
    • getDeclaredField

      public FieldNode getDeclaredField(String name)
      Finds a FieldNode matching the given name declared directly in this class. This method does not search superclasses; to include inherited fields, use getField(String). If this node is a proxy (has a redirect), the call is forwarded to the redirect target.
      Parameters:
      name - the name of the field to find
      Returns:
      the FieldNode with the given name declared in this class, or null if not found
      See Also:
    • getField

      public FieldNode getField(String name)
      Finds a FieldNode matching the given name in this class or any of its superclasses. This method searches the inheritance hierarchy starting from this ClassNode and moving up through superclasses until a field with the matching name is found.
      Parameters:
      name - the name of the field to find
      Returns:
      the FieldNode with the given name, or null if not found in this class or any superclass
      See Also:
    • getObjectInitializerStatements

      public List<Statement> getObjectInitializerStatements()
    • getDeclaredMethods

      public List<MethodNode> getDeclaredMethods(String name)
      Returns a list of all MethodNodes with the given name declared directly in this class. This method does not search superclasses; to include inherited methods, use getMethods(String). If this node is a proxy (has a redirect), the call is forwarded to the redirect target. The list may be empty if no methods with the given name are declared in this class.
      Parameters:
      name - the name of the methods to find
      Returns:
      a list of MethodNodes with the given name (possibly empty)
      See Also:
    • getMethods

      public List<MethodNode> getMethods(String name)
      Returns a list of all MethodNodes with the given name from this class and its superclasses. This method searches the inheritance hierarchy starting from this ClassNode and moving up through superclasses, collecting all methods with matching names. The list may be empty if no methods with the given name are found in the class hierarchy.
      Parameters:
      name - the name of the methods to find
      Returns:
      a list of MethodNodes with the given name from this class and its superclasses (possibly empty)
      See Also:
    • getDeclaredMethod

      public MethodNode getDeclaredMethod(String name, Parameter[] parameters)
      Finds a MethodNode matching the given name and parameter signature declared directly in this class. This method does not search superclasses; to include inherited methods, use getMethod(String, Parameter[]). Parameter matching is done by comparing parameter types using the parametersEqual(Parameter[], Parameter[]) method.
      Parameters:
      name - the name of the method to find
      parameters - an array of Parameters representing the method signature, or null/empty for methods with no parameters
      Returns:
      the MethodNode with the given name and parameters, or null if not found
      See Also:
    • getMethod

      public MethodNode getMethod(String name, Parameter[] parameters)
      Finds a MethodNode matching the given name and parameter signature in this class or any of its superclasses. This method searches the inheritance hierarchy starting from this ClassNode. Parameter matching is done by comparing parameter types using the parametersEqual(Parameter[], Parameter[]) method.
      Parameters:
      name - the name of the method to find
      parameters - an array of Parameters representing the method signature, or null/empty for methods with no parameters
      Returns:
      the MethodNode with the given name and parameters, or null if not found in this class or any superclass
      See Also:
    • isDerivedFrom

      public boolean isDerivedFrom(ClassNode type)
      Parameters:
      type - the ClassNode of interest
      Returns:
      true if this node is derived from the given ClassNode
    • isDerivedFromGroovyObject

      public boolean isDerivedFromGroovyObject()
      Returns whether this ClassNode represents a type that implements the GroovyObject interface. GroovyObject is the core interface that all Groovy objects implement, providing methods like getProperty(), setProperty(), and invokeMethod() for dynamic behavior. This method checks the class hierarchy to determine if GroovyObject is in the list of implemented interfaces.
      Returns:
      true if this type directly or indirectly implements GroovyObject; false otherwise
      See Also:
    • implementsAnyInterfaces

      public boolean implementsAnyInterfaces(ClassNode... classNodes)
      Parameters:
      classNodes - the class nodes for the interfaces
      Returns:
      true if this type implements any of the given interfaces
    • implementsInterface

      public boolean implementsInterface(ClassNode classNode)
      Parameters:
      classNode - the class node for the interface
      Returns:
      true if this type implements the given interface
    • declaresAnyInterfaces

      public boolean declaresAnyInterfaces(ClassNode... classNodes)
      Parameters:
      classNodes - the class nodes for the interfaces
      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
    • 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/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:
    • parametersEqual

      @Deprecated(forRemoval=true, since="4.0.0") protected boolean parametersEqual(Parameter[] a, Parameter[] b)
      Deprecated, for removal: This API element is subject to removal in a future version.
    • getGetterMethod

      public MethodNode getGetterMethod(String getterName)
      Finds a getter MethodNode matching the given name, optionally searching superclasses. A getter method is defined as a method with no parameters that returns a non-void type. For methods starting with "is", the return type must be boolean. Synthetic and bridge methods are disregarded in favor of the most specific real getter.
      Parameters:
      getterName - the name of the getter method to find
      Returns:
      the getter MethodNode, or null if not found
      See Also:
    • getGetterMethod

      public MethodNode getGetterMethod(String getterName, boolean searchSupers)
      Finds a getter MethodNode matching the given name, optionally searching superclasses and interfaces. A getter method is defined as a method with no parameters that returns a non-void type. For methods starting with "is", the return type must be boolean. Synthetic and bridge methods are disregarded in favor of the most specific real getter.
      Parameters:
      getterName - the name of the getter method to find
      searchSupers - if true, searches superclasses and interfaces for the getter; if false, searches only methods declared in this class
      Returns:
      the getter MethodNode, or null if not found
      See Also:
    • getSetterMethod

      public MethodNode getSetterMethod(String setterName)
      Finds a setter MethodNode matching the given name, optionally searching superclasses. A setter method is defined as a method with exactly one parameter that returns void. This method searches from this class up the inheritance hierarchy until a matching setter is found.
      Parameters:
      setterName - the name of the setter method to find
      Returns:
      the setter MethodNode, or null if not found
      See Also:
    • getSetterMethod

      public MethodNode getSetterMethod(String setterName, boolean voidOnly)
      Finds a setter MethodNode matching the given name. A setter method is defined as a method with exactly one parameter. If voidOnly is true, the method must also return void. The method searches this class and superclasses for the first matching setter.
      Parameters:
      setterName - the name of the setter method to find
      voidOnly - if true, only returns setter methods that return void; if false, accepts setters that return any type
      Returns:
      the setter MethodNode, or null if not found
      See Also:
    • hasPossibleMethod

      public boolean hasPossibleMethod(String name, Expression arguments)
      Determines 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

      public MethodNode tryFindPossibleMethod(String name, Expression arguments)
    • hasPossibleStaticMethod

      public boolean hasPossibleStaticMethod(String name, Expression arguments)
      Checks 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:
      true if a matching method was found
    • renameField

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

      public void removeField(String oldName)
      Removes the FieldNode with the given name from this ClassNode. This method removes the field from the internal field list and index. If this node is a proxy (has a redirect), the operation is performed on the redirect target.
      Parameters:
      oldName - the name of the field to remove
      See Also:
    • removeMethod

      public void removeMethod(MethodNode node)
      Removes the given MethodNode from this ClassNode. The method is removed from the internal methods list and the methods map. If this node is a proxy (has a redirect), the operation is performed on the redirect target.
      Parameters:
      node - the MethodNode to remove
      See Also:
    • removeConstructor

      public void removeConstructor(ConstructorNode node)
    • equals

      public boolean equals(Object that)
      Compares this ClassNode with another object for equality. Two ClassNodes are considered equal if:
      • They are the same object (identity check)
      • Both are ClassNode instances and either have identical redirects, identical component types (for arrays), or have matching text representations
      If this ClassNode is a redirect node, the comparison is delegated to the redirect target. Array ClassNodes are compared based on their component types.
      Overrides:
      equals in class Object
      Parameters:
      that - the object to compare with this ClassNode
      Returns:
      true if the objects represent the same class; false otherwise
      See Also:
    • hashCode

      public int hashCode()
      Returns the hash code for this ClassNode. The hash code is based on the redirect's hash code if a redirect exists, or the text representation's hash code if this is a primary or array ClassNode. This ensures that equal ClassNodes (by equals(Object)) have equal hash codes, maintaining the hash code contract for use in hash-based collections.
      Overrides:
      hashCode in class Object
      Returns:
      a hash code value for this ClassNode
      See Also:
    • toString

      public String toString()
      Returns a string representation of this ClassNode for debugging and display purposes. For array types, appends "[]" to the component type's string representation. For generic types, includes generic type parameters (e.g., "List"). If this is a redirect node, includes arrow notation showing the redirect target.
      Overrides:
      toString in class Object
      Returns:
      a string representation of this ClassNode, including fully-qualified name and type information
    • toString

      public String toString(boolean showRedirect)
      Returns a string representation of this ClassNode with optional redirect information. For array types, appends "[]" to the component type's string representation. For generic types, includes generic type parameters (e.g., "List"). If showRedirect is true and this is a redirect node, includes arrow notation showing the redirect target; otherwise the redirect information is omitted.
      Parameters:
      showRedirect - if true, includes redirect information in the output; if false, omits it
      Returns:
      a string representation of this ClassNode
    • visitContents

      public void visitContents(GroovyClassVisitor visitor)
    • isAbstract

      public boolean isAbstract()
      Returns whether this ClassNode represents an abstract class. A class is abstract if the ACC_ABSTRACT modifier flag is set in its modifiers. Abstract classes cannot be instantiated directly; they serve as base types for subclasses. This method checks the actual modifier bits, independent of the source code syntax.
      Returns:
      true if this class has the abstract modifier set; false otherwise
      See Also:
    • isInterface

      public boolean isInterface()
      Returns whether this ClassNode represents an interface type. A class is an interface if the ACC_INTERFACE modifier flag is set in its modifiers. Interfaces define contracts for implementing classes without providing implementation details. Note that annotations are also interfaces (with the ACC_ANNOTATION flag also set).
      Returns:
      true if this class is an interface; false if it is a regular class
      See Also:
    • isAnnotationDefinition

      public boolean isAnnotationDefinition()
      Returns whether this ClassNode represents an annotation type (annotation definition). Annotation types are always interfaces with both the ACC_INTERFACE and ACC_ANNOTATION modifier flags set. They define metadata that can be attached to program elements. All annotation types are interfaces but not all interfaces are annotations.
      Returns:
      true if this class is an annotation type; false otherwise
      See Also:
    • isEnum

      public boolean isEnum()
      Returns whether this ClassNode represents an enumeration type. An enum class has the ACC_ENUM modifier flag set in its modifiers. Enumerations define a fixed set of named constants that can be iterated over at compile time.
      Returns:
      true if this class is an enumeration; false otherwise
      See Also:
    • isRecord

      @Incubating public boolean isRecord()
      Checks if the ClassNode instance represents a native record. Check instead for the RecordBase annotation if looking for records and record-like classes currently being compiled.
      Returns:
      true if the instance represents a native record
      Since:
      4.0.0
    • isSealed

      @Incubating public boolean isSealed()
      Returns:
      true for native and emulated (annotation based) sealed classes
      Since:
      4.0.0
    • isResolved

      public boolean isResolved()
      Determines whether this ClassNode has been resolved to an actual Java class. A ClassNode is considered resolved if it represents a real Class object loaded at runtime, or if it has been redirected to a resolved ClassNode, or if it represents an array of resolved component types. During early compilation phases, unresolved ClassNodes may reference types that haven't been loaded yet.
      Returns:
      true if this ClassNode has been resolved to a real class; false if it still represents an unresolved reference
    • getTypeClass

      public Class 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 using getPlainNodeReference().
      Returns:
      the class this classnode relates to. May return null.
    • isArray

      public boolean isArray()
      Determines whether this ClassNode represents an array type. A ClassNode is an array if its component type has been set. The component type represents the element type of the array (e.g., "int" for "int[]", or "String" for "String[]").
      Returns:
      true if this ClassNode represents an array type; false if it represents a scalar type
    • makeArray

      public ClassNode makeArray()
      Returns a ClassNode representing an array of the type represented by this.
    • getComponentType

      public ClassNode getComponentType()
      Returns the component type of this array ClassNode. If this ClassNode does not represent an array type, returns null. For array types, this returns the ClassNode representing the element type (e.g., for "String[]", returns the ClassNode for "String").
      Returns:
      the ClassNode representing the array's element type, or null if not an array
    • getOuterClass

      public ClassNode getOuterClass()
      Returns the outer class of this ClassNode, or null if this is not an inner class. If this ClassNode has a redirect, returns the outer class from the redirect. This method traverses the class hierarchy upward to find the immediately enclosing class.
      Returns:
      the outer ClassNode, or null if not an inner class
      See Also:
    • getOuterClasses

      public List<ClassNode> getOuterClasses()
      Returns a list of all outer classes enclosing this ClassNode in hierarchical order. The innermost outer class appears first in the list. Returns an empty list if this is not an inner class. If this ClassNode has a redirect, the list is derived from the redirect.
      Returns:
      a List of enclosing ClassNodes in order from innermost to outermost, never null
      See Also:
    • getOuterField

      public FieldNode getOuterField(String name)
      Returns:
      the field on the outer class or null if this is not an inner class
    • getInnerClasses

      public Iterator<InnerClassNode> getInnerClasses()
      Returns an iterator over the inner classes defined within this ClassNode. The iterator is backed by an unmodifiable view of the inner classes list. Returns an empty iterator if this class has no inner classes defined.
      Returns:
      an Iterator of InnerClassNodes defined in this class, never null
      See Also:
      • addInnerClass(InnerClassNode)
    • getEnclosingMethod

      public MethodNode getEnclosingMethod()
      Returns the enclosing MethodNode of this local inner class, or null if this is not a local inner class. Local inner classes are those declared within method bodies. If this ClassNode has a redirect, returns the enclosing method from the redirect.
      Returns:
      the MethodNode enclosing this class, or null
      See Also:
    • setEnclosingMethod

      public void setEnclosingMethod(MethodNode enclosingMethod)
      Sets the enclosing MethodNode for this local inner class. This is typically called when processing classes declared within method bodies.
      Parameters:
      enclosingMethod - the MethodNode enclosing this class, or null
      See Also:
    • asGenericsType

      public GenericsType asGenericsType()
    • getGenericsTypes

      public GenericsType[] getGenericsTypes()
      Returns the generic type parameters for this ClassNode, or null if none are defined. Generic types represent type parameters and their bounds as declared in class definitions. For example, in class List<T>, the GenericsType array would contain a single element representing the type parameter T.
      Returns:
      an array of GenericsType representing the type parameters, or null
      See Also:
    • setGenericsTypes

      public void setGenericsTypes(GenericsType[] genericsTypes)
      Sets the generic type parameters for this ClassNode. Setting generics types updates the generics usage indicator for this class. This method is typically called during compiler phases when type information becomes available.
      Parameters:
      genericsTypes - an array of GenericsType representing the type parameters, or null
      See Also:
    • isGenericsPlaceHolder

      public boolean isGenericsPlaceHolder()
    • setGenericsPlaceHolder

      public void setGenericsPlaceHolder(boolean placeholder)
    • isUsingGenerics

      public boolean isUsingGenerics()
    • setUsingGenerics

      public void setUsingGenerics(boolean usesGenerics)
    • isScript

      public boolean isScript()
      Returns whether this ClassNode represents a Groovy script body. A script is a special class that executes at the top level rather than requiring instantiation. This method checks both the direct flag and whether the class derives from Script. During compilation, script bodies are compiled into classes that extend the Script base class.
      Returns:
      true if this class represents a script; false otherwise
      See Also:
    • setScript

      public void setScript(boolean script)
      Marks this ClassNode as a script or removes the script flag. Setting this to true indicates that this class represents a Groovy script that should be executed at the top level. This is used internally during compilation to identify script classes that need special treatment (e.g., extending the Script base class).
      Parameters:
      script - true to mark as a script; false to unmark
      See Also:
    • isScriptBody

      public boolean isScriptBody()
      Returns whether this inner class or closure was declared inside a script body. This flag distinguishes between inner classes/closures defined within a script's top-level code versus those defined within regular class methods. Script body context affects how local variable access and scoping rules are applied. This method follows redirects if present.
      Returns:
      true if this inner class or closure is inside a script body; false otherwise
      See Also:
    • setScriptBody

      public void setScriptBody(boolean scriptBody)
      Marks this inner class or closure as being declared inside a script body. When set to true, indicates that the class definition occurs at the top level of a Groovy script (outside of any method or class definition). This affects variable scoping and how the compiler generates code for accessing enclosing scope variables. Typically used by the compiler during script class generation.
      Parameters:
      scriptBody - true to mark as defined in script body; false otherwise
      See Also:
    • isStaticClass

      public boolean isStaticClass()
      Returns whether this inner class or closure was declared inside a static method context. This flag identifies classes that are nested within static methods (as opposed to instance methods or top-level definitions). Inner classes in static contexts have different scoping rules and cannot access instance variables of the enclosing class. This method follows redirects if present.
      Returns:
      true if this class is declared in a static method; false otherwise
      See Also:
    • setStaticClass

      public void setStaticClass(boolean staticClass)
      Marks this inner class or closure as being declared in a static method context. When set to true, indicates that this class definition occurs within a static method, affecting how the compiler generates access to enclosing class members. Static inner classes have restricted access to the enclosing class (only to static members).
      Parameters:
      staticClass - true to mark as defined in a static context; false otherwise
      See Also:
    • 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. 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:
      true if node is public but had no explicit public modifier
    • setSyntheticPublic

      public void setSyntheticPublic(boolean syntheticPublic)
      Marks this ClassNode as having synthetic public visibility. When set to true, indicates that this class was implicitly made public by Groovy's default visibility rule rather than having an explicit public modifier in the source code. This distinction is important for AST transformations that need to preserve the original intent regarding visibility and access control.
      Parameters:
      syntheticPublic - true if public was added by the compiler; false if explicitly declared
      See Also:
    • isAnnotated

      public boolean isAnnotated()
      Returns whether this ClassNode has been marked as annotated. This flag is set to true when annotations are added to the class via AnnotatedNode.addAnnotation(AnnotationNode) or related methods. It serves as a quick indicator that the class carries metadata annotations, without requiring iteration through the full annotations list. The flag does not distinguish between different types of annotations (type annotations, method annotations, etc.).
      Returns:
      true if this class has been marked as having annotations; false otherwise
      See Also:
    • setAnnotated

      public void setAnnotated(boolean annotated)
      Marks this ClassNode as having annotations attached to it. This flag is used internally by the compiler to track whether annotations have been added to the class. It should be set to true when annotations are added and may be used as an optimization to avoid scanning the annotations list for empty cases.
      Parameters:
      annotated - true to mark as having annotations; false to clear the flag
      See Also:
    • getAnnotations

      public List<AnnotationNode> getAnnotations()
      Description copied from class: AnnotatedNode
      Returns all annotations attached to this AST node. Annotations are runtime-visible or source-level metadata attached to language elements.
      Overrides:
      getAnnotations in class AnnotatedNode
      Returns:
      list of AnnotationNode, or empty list if none
    • getAnnotations

      public List<AnnotationNode> getAnnotations(ClassNode type)
      Description copied from class: AnnotatedNode
      Returns annotations of a specific type attached to this AST node. Filters the annotation list by the provided type.
      Overrides:
      getAnnotations in class AnnotatedNode
      Parameters:
      type - the annotation type to filter by
      Returns:
      list of matching AnnotationNode, or empty list if none
    • getTypeAnnotations

      public List<AnnotationNode> getTypeAnnotations()
    • getTypeAnnotations

      public List<AnnotationNode> getTypeAnnotations(ClassNode type)
    • addTypeAnnotation

      public void addTypeAnnotation(AnnotationNode annotation)
    • addTypeAnnotations

      public void addTypeAnnotations(List<AnnotationNode> annotations)
    • addTransform

      public void addTransform(Class<? extends ASTTransformation> transform, ASTNode node)
    • getTransforms

      public Map<Class<? extends ASTTransformation>,Set<ASTNode>> getTransforms(CompilePhase phase)