Class ClassNode

All Implemented Interfaces:
GroovydocHolder<AnnotatedNode>, NodeMetaDataHandler
Direct Known Subclasses:
DecompiledClassNode, InnerClassNode, IntersectionTypeClassNode, 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. getOuterClassType() / setOuterClassType(ClassNode) are likewise not proxied: they record the parameterized enclosing type of a JLS 4.5 rare type use (Outer<T>.Inner), distinct from getOuterClass() which is the enclosing class of a nested class declaration.

See Also:
  • Field Details

    • EMPTY_ARRAY

      public static final ClassNode[] EMPTY_ARRAY
      Shared zero-length array for call sites that need a type list but have no classes, such as no superinterfaces or no declared exceptions.
    • THIS

      public static final ClassNode THIS
      Sentinel type of a this() constructor call. The call is recognized by identity with this node, which is a distinct Object class node and not the enclosing class.
    • SUPER

      public static final ClassNode SUPER
      Sentinel type of a super() constructor call. The call is recognized by identity with this node, which is a distinct Object class node and not the superclass.
    • isPrimaryNode

      protected boolean isPrimaryNode
      true when this node was built as a Groovy source class the compiler emits. Reflection classes, synthetic arrays, and plain references clear the flag. setRedirect(ClassNode) rejects a primary node, and setModule(org.codehaus.groovy.ast.ModuleNode) stores the module only while the flag is set. The flag is not read through a redirect; isPrimaryClassNode() follows one.
    • lazyInitLock

      protected final Object lazyInitLock
      Lock for the one-time fill-in of members copied from a reflection class. Subclasses that initialize lazily synchronize on this same object. Each class node has its own lock.
    • clazz

      protected Class<?> clazz
      Reflection class this node wraps, or null for a source class, a placeholder, or an unresolved array. isResolved() and getTypeClass() treat a non-null value as resolved for this instance before they consult a redirect.
  • 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()
      Returns a non-primary proxy that redirects to redirect(), so generics or type annotations can be added without mutating the canonical class. A primitive is returned as this. Use getPlainNodeReference(boolean) to force a proxy for a primitive.
      Returns:
      this node when it is a primitive, otherwise a new redirecting proxy
    • getPlainNodeReference

      public ClassNode getPlainNodeReference(boolean skipPrimitives)
      Builds a non-primary proxy that redirects to redirect(). The proxy keeps this node's own unresolved name, modifiers, and unresolved superclass, and has no interfaces or mixins of its own; getters that follow the redirect see the canonical class. An array proxy takes its component type from the redirect, and a recorded outer class type is copied onto the proxy. When skipPrimitives is true and this node is a primitive, this is returned so the shared primitive node keeps its identity.
      Parameters:
      skipPrimitives - true to return a primitive node unchanged
      Returns:
      this for a skipped primitive, otherwise a new proxy
    • getModule

      public ModuleNode getModule()
      Returns the module recorded on the resolved class, or null when setModule(org.codehaus.groovy.ast.ModuleNode) did not store one. The lookup follows redirect().
      Returns:
      the owning module, or null
    • setModule

      public void setModule(ModuleNode module)
      Records the module that owns this source class. The module is stored as metadata on this node only when isPrimaryNode is set; a placeholder or reflection class ignores the call. getModule() finds it by following the redirect back to this node.
      Parameters:
      module - the module to associate with this primary class
    • getCompileUnit

      public CompileUnit getCompileUnit()
      Returns the compile unit of the module that owns this class. A redirect is followed. The unit is taken from the module stored by setModule(org.codehaus.groovy.ast.ModuleNode), not from setCompileUnit(org.codehaus.groovy.ast.CompileUnit).
      Returns:
      the owning compile unit, or null when the class has no module
    • 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.
      for removal since 5.0.0; the compile unit is no longer stored on the class node
      Walks the redirect chain and discards cu. The node at the end of the chain ignores the argument, so the compile unit is never stored here. getCompileUnit() reads it from the module.
      Parameters:
      cu - unused compile unit
    • getPackage

      public PackageNode getPackage()
      Returns the package of getModule(), or null when this class has no module.
      Returns:
      the package node, or null
    • getPackageName

      public String getPackageName()
      Returns the package portion of getName(), which follows the redirect. The portion is the text before the last dot, so an inner-class $ suffix stays in the simple name. A name with no dot after its first character is treated as the default package.
      Returns:
      the package name, or null for the default package
    • hasPackageName

      public boolean hasPackageName()
      Reports whether getName() has a dot after its first character. A default-package class, whose redirected name has no such dot, returns false.
      Returns:
      true when the redirected name includes a package
    • getNameWithoutPackage

      public String getNameWithoutPackage()
      Returns the text of getName() after the last dot. When the redirected name has no dot after its first character, that whole name is returned. An inner class keeps its $ suffix, so pkg.Outer$Inner yields Outer$Inner.
      Returns:
      the class name without its package
    • getUnresolvedName

      public String getUnresolvedName()
      Returns the name stored on this node, without following a redirect. A placeholder can still report the name it was created with after getName() reports the resolved class. A type variable is printed from this name.
      Returns:
      this node's own name, which may be unresolved
    • 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()
      Collects the interfaces of this type into a LinkedHashSet, keeping the order of first encounter and dropping a repeat. If this node is itself an interface, it is included first. The walk then follows getInterfaces() of this node and of each interface reached that way, depth-first. Interfaces that appear only on a superclass are not visited.
      Returns:
      the interfaces, never null
    • 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)
      Replaces the mixins of the resolved class. A redirect is written through. The array is stored as given and is not copied; null clears it.
      Parameters:
      mixins - the mixin array to store, or null
    • getPermittedSubclasses

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

      public void setPermittedSubclasses(List<ClassNode> permittedSubclasses)
      Replaces the permitted-subclass list stored on the resolved node. A redirect is written through, and the list is stored as given. getPermittedSubclasses() returns the list held after a reflection class is initialized, which may replace a list stored before that initialization.
      Parameters:
      permittedSubclasses - the classes a sealed type permits
    • 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)
      Appends a mixin to the resolved class when an equal mixin is not already present. An existing match leaves the mixin list unchanged.
      Parameters:
      node - the mixin to add
    • 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)
      Appends a constructor to the resolved class and sets that constructor's declaring class to the resolved node. The constructor list is created on first use. A constructor with the same parameters is not detected or reused.
      Parameters:
      node - the constructor to add
    • addConstructor

      public ConstructorNode addConstructor(int modifiers, Parameter[] parameters, ClassNode[] exceptions, Statement code)
      Creates a constructor from the given parts and appends it to the resolved class. A constructor with the same parameters is not reused; the new node is always added.
      Parameters:
      modifiers - the constructor modifiers
      parameters - the parameters, or an empty array
      exceptions - the declared exception types, or an empty array
      code - the constructor body, or null
      Returns:
      the constructor that was added
    • 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)
      Adds statements to the synthetic static initializer <clinit>, creating that method when the class does not already have one. When fieldInit is false the statements are appended, which is where a static initializer block goes. When fieldInit is true they are placed in front of the statements already in the initializer, and statements is mutated to hold that combined sequence.
      Parameters:
      statements - the statements to add; mutated when fieldInit is true
      fieldInit - true for static field initializers, which precede other static statements
    • positionStmtsAfterEnumInitStmts

      public void positionStmtsAfterEnumInitStmts(List<Statement> staticFieldInitializerStatements)
      Inserts static field initializers into <clinit> immediately after every binary expression whose left side is the synthetic $VALUES field. That expression marks the end of the generated enum constants. The initializer is created when the class does not have one. When its body is not a block, or it contains no such expression, the statements are not inserted.
      Parameters:
      staticFieldInitializerStatements - initializers of explicitly declared static fields
    • 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()
      Returns the constructors declared on the resolved class. A redirect is followed and a reflection class is initialized first. Superclass constructors are not included. The list is created empty when the class has none, and it is the live list updated by adding a constructor and by removeConstructor(org.codehaus.groovy.ast.ConstructorNode).
      Returns:
      the mutable declared constructors, never null
    • 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()
      Returns the instance-initializer statements stored on this node. The list is created on first access and is the live storage addObjectInitializerStatements(Statement) appends to. A redirect is not followed, and a reflection class is not initialized to fill the list.
      Returns:
      the mutable initializer statements, never null
    • 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.
      Compares parameter lists by length and declared type. Types are compared with class-node equality, so a primitive and its wrapper do not match. Delegates to ParameterUtils.parametersEqual(org.codehaus.groovy.ast.Parameter[], org.codehaus.groovy.ast.Parameter[]).
      Parameters:
      a - the first parameter list
      b - the second parameter list
      Returns:
      true when both lists have the same length and equal parameter types
    • 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)
      Searches for one method on this type that can accept the arguments.

      A TupleExpression supplies the argument list; any other expression counts as a single argument. A spread is not expanded, so the count can be smaller than the number of values passed at runtime. The search walks this class and then each superclass. A candidate matches when the argument count equals the parameter count, or the last parameter is an array and the argument count reaches that parameter, so the array takes any arguments beyond it, including none. Each argument type must be derived from the parameter type in that position, or from the array component type for a variable-arity tail. Static and non-public methods take part in this walk. Unlike hasPossibleMethod(java.lang.String, org.codehaus.groovy.ast.expr.Expression), argument types are checked.

      The first match is kept. A second match found while still scanning this class yields null. Once the scan has moved to a superclass, a second match also yields null when the first match does not have exactly one parameter per argument. When it does, differing parameter types yield null as well; equal types, using the superclass method's trailing array component after its last fixed parameter, leave the first match in place.

      When the class hierarchy has no match and getInterfaces() is non-empty, the first public instance method on those interfaces and their superinterfaces with a compatible arity and argument types is returned. Interfaces that appear only on a superclass are not searched, and a second interface method does not cancel the first.

      Parameters:
      name - the method name
      arguments - the call arguments, usually a TupleExpression
      Returns:
      the selected method, or null when none matches or the class hierarchy is ambiguous
    • 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)
      Rebinds the resolved class's field index from oldName to newName. The indexed field node is not renamed and the field list is not reordered, so only lookup through the index observes the new name. A name that is not in the index is recorded as null and replaces whatever was stored under newName. When the class has no field index yet, the call does nothing.
      Parameters:
      oldName - the index key to remove
      newName - the index key that receives the removed entry
    • 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)
      Removes a constructor from the declared constructors of the resolved class. Comparison is by identity. A constructor that is not in the list leaves the list unchanged.
      Parameters:
      node - the constructor node to remove
    • 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)
      Visits the members of the resolved class in order: properties, fields, declared constructors, then methods. The class node itself and its inner classes are not visited. Each method is visited once, including a method added while another method is being visited. Methods already seen are tracked by identity, and the method list is scanned again until no unseen method remains.
      Parameters:
      visitor - the visitor that receives each member
    • 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:
    • isStatic

      public boolean isStatic()
      Returns whether this ClassNode represents a static class. A class is static if the ACC_STATIC modifier flag is set in its modifiers. Static nested classes have no enclosing instance; they can be used without an instance of the enclosing class. This method checks the actual modifier bits, independent of the source code syntax.
      Returns:
      true if this class has the static modifier set; false otherwise
      Since:
      6.0.0
      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

      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:
    • getOuterClassType

      public ClassNode getOuterClassType()
      For a JLS 4.5 rare type Outer<T>.Inner, the parameterized enclosing type stored on this node (type-use, not declaration; not node metadata and not redirected). Distinct from getOuterClass(), which is the enclosing class of a nested class declaration. Copied by getPlainNodeReference().
      Returns:
      the parameterized enclosing type, or null if this is not a rare type
      Since:
      6.0.0
    • setOuterClassType

      public void setOuterClassType(ClassNode outer)
      Records the parameterized enclosing type of a JLS 4.5 rare type on this node. Does not write through redirect().
      Parameters:
      outer - the parameterized Outer<T> node; null clears it
      Since:
      6.0.0
    • 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()
      Views this node as a generic type argument. A node that is not a type-variable placeholder becomes a generics type with no bounds. A placeholder whose stored generics type already has upper bounds returns that same object. Otherwise a new generics type is built with this node as the type variable and the redirect, or this node when there is no redirect, as its upper bound. A generic declaration already stored on the placeholder is copied onto that new type.
      Returns:
      a generics type representing this node
    • 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()
      Reports whether this node stands for a type variable rather than a class. The flag lives on this node and is not read from the redirect.
      Returns:
      true when setGenericsPlaceHolder(boolean) has marked this node as a placeholder
    • setGenericsPlaceHolder

      public void setGenericsPlaceHolder(boolean placeholder)
      Sets or clears the type-variable flag on this node. Setting it also marks the node as using generics. Clearing it does not turn that mark off.
      Parameters:
      placeholder - true when this node stands for a type variable
    • isUsingGenerics

      public boolean isUsingGenerics()
      Reports the generics-usage flag stored on this node, without following a redirect. The flag is raised when type parameters or a placeholder are set, and when a primary node's superclass or interfaces already use generics. Those updates do not clear it.
      Returns:
      true when this node has been marked as using generics
    • setUsingGenerics

      public void setUsingGenerics(boolean usesGenerics)
      Sets the generics-usage flag on this node, including clearing it. Unlike setGenericsTypes(org.codehaus.groovy.ast.GenericsType[]) and setGenericsPlaceHolder(boolean), the argument replaces the previous value.
      Parameters:
      usesGenerics - true to mark the node as using generics
    • 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()
      Returns a copy of the type-use annotations stored on this node. A redirect is not followed: type-use annotations belong to the use, not to the canonical class. The copy does not observe later additions.
      Returns:
      a new list of type-use annotations, empty when none were added
    • getTypeAnnotations

      public List<AnnotationNode> getTypeAnnotations(ClassNode type)
      Returns the type-use annotations on this node whose annotation class is equal to type. A redirect is not followed. The result is a new list.
      Parameters:
      type - the annotation class to select
      Returns:
      a new list of matching type-use annotations, possibly empty
    • addTypeAnnotation

      public void addTypeAnnotation(AnnotationNode annotation)
      Appends a type-use annotation to this node and marks the node annotated. The annotation is stored here and not on the redirect target. A canonical class cannot carry type-use annotations, so the call fails when this node is not a redirect and it is resolved or primary. Throws NullPointerException if annotation is null.
      Parameters:
      annotation - the type-use annotation to add
    • addTypeAnnotations

      public void addTypeAnnotations(List<AnnotationNode> annotations)
      Adds each type-use annotation through addTypeAnnotation(org.codehaus.groovy.ast.AnnotationNode), in order. An empty list does nothing. Each element is subject to the same rejection of a resolved or primary non-redirect.
      Parameters:
      annotations - the type-use annotations to add
    • addTransform

      public void addTransform(Class<? extends ASTTransformation> transform, ASTNode node)
      Records an AST node as a reason to run a global transform, when transform carries GroovyASTTransformation. The node is stored under that transform class, in insertion order, in the map for the annotation's CompilePhase. A node already recorded for that phase is ignored. A transform class without the annotation is ignored. The map lives on this node and is not redirected.
      Parameters:
      transform - the transform class to schedule
      node - the AST node that triggered it, usually the annotation use
    • getTransforms

      public Map<Class<? extends ASTTransformation>,Set<ASTNode>> getTransforms(CompilePhase phase)
      Returns the live map from transform class to the AST nodes that scheduled it for one compilation phase. The first call creates an empty map for every phase, so a phase with no transforms yields an empty map rather than null. The map is the storage addTransform(java.lang.Class<? extends org.codehaus.groovy.transform.ASTTransformation>, org.codehaus.groovy.ast.ASTNode) updates, and it is not redirected.
      Parameters:
      phase - the compilation phase to query
      Returns:
      the mutable map of transform class to triggering nodes for phase