Class FieldNode

All Implemented Interfaces:
GroovydocHolder<AnnotatedNode>, NodeMetaDataHandler, Variable

public class FieldNode extends AnnotatedNode implements Variable
Represents a field (member variable)
  • Constructor Details

    • FieldNode

      public FieldNode(String name, int modifiers, ClassNode type, ClassNode owner, Expression initialValueExpression)
      Creates a field node representing a class member variable. The field's type can be dynamically determined based on the provided ClassNode.
      Parameters:
      name - the field name
      modifiers - ASM modifier flags (public, static, final, etc.)
      type - the field's ClassNode type
      owner - the ClassNode that declares this field
      initialValueExpression - optional initial value expression (null for no initializer)
    • FieldNode

      protected FieldNode()
  • Method Details

    • newStatic

      public static FieldNode newStatic(Class theClass, String name) throws SecurityException, NoSuchFieldException
      Creates a new FieldNode wrapping a reflection Field from a Java class. Extracts type information from the reflected field and creates an AST representation with public static access modifiers matching the source field.
      Parameters:
      theClass - the class containing the reflected field
      name - the field name
      Returns:
      a new FieldNode representing the reflected field
      Throws:
      SecurityException - if field access is denied
      NoSuchFieldException - if the field does not exist
    • getInitialExpression

      public Expression getInitialExpression()
      Returns the initial value expression for this field. The expression is evaluated when the field is initialized. Returns null if no initializer is present.
      Specified by:
      getInitialExpression in interface Variable
      Returns:
      the Expression providing the initial value, or null
    • getName

      public String getName()
      Returns the field name.
      Specified by:
      getName in interface Variable
      Returns:
      the field's identifier
    • getType

      public ClassNode getType()
      Returns the declared type of this field as a ClassNode. The type may be dynamically determined or may reference generics.
      Specified by:
      getType in interface Variable
      Returns:
      the field's type node
    • setType

      public void setType(ClassNode type)
      Sets the field's type. Updates both the current type and tracks the origin type. If the type is dynamic, marks this field as dynamically typed.
      Parameters:
      type - the field's ClassNode type
    • getOwner

      public ClassNode getOwner()
      Returns the class that declares this field.
      Returns:
      the declaring ClassNode, or null if not yet set
    • isHolder

      public boolean isHolder()
      Checks whether this field is a holder field. Holder fields are used internally for certain compilation strategies.
      Returns:
      true if this field is marked as a holder
    • setHolder

      public void setHolder(boolean holder)
      Marks this field as a holder field for internal compilation purposes.
      Parameters:
      holder - true to mark as a holder field
    • isDynamicTyped

      public boolean isDynamicTyped()
      Checks whether this field's type is dynamically typed (unresolved type). Dynamic typing occurs when the exact type cannot be determined at compile time.
      Specified by:
      isDynamicTyped in interface Variable
      Returns:
      true if this field uses dynamic typing
    • getModifiers

      public int getModifiers()
      Returns the ASM modifier flags for this field. Flags include visibility (public/protected/private), static, final, transient, volatile, etc.
      Specified by:
      getModifiers in interface Variable
      Returns:
      ASM opcode flags representing this field's modifiers
      See Also:
      • Opcodes
    • setModifiers

      public void setModifiers(int modifiers)
      Sets the ASM modifier flags for this field. Allows updating visibility or other modifier flags during compilation.
      Parameters:
      modifiers - ASM opcode flags to set
    • hasInitialExpression

      public boolean hasInitialExpression()
      Checks whether this field has an initial value expression.
      Specified by:
      hasInitialExpression in interface Variable
      Returns:
      true if an initializer expression is present
    • isInStaticContext

      public boolean isInStaticContext()
      Checks whether this field is in a static context. A field is in a static context if it has the static modifier.
      Specified by:
      isInStaticContext in interface Variable
      Returns:
      true if this field is static
    • isEnum

      public boolean isEnum()
      Checks whether this field is an enum constant. Enum constants are fields with the ACC_ENUM access flag.
      Returns:
      true if this field is an enum constant
    • setOwner

      public void setOwner(ClassNode owner)
      Sets the class that declares this field. Used during AST construction to establish the ownership relationship.
      Parameters:
      owner - the declaring ClassNode
    • getInitialValueExpression

      public Expression getInitialValueExpression()
      Returns the initial value expression for this field. Provides direct access to the initializer expression if one is defined.
      Returns:
      the Expression providing initial value, or null if absent
    • setInitialValueExpression

      public void setInitialValueExpression(Expression initialValueExpression)
      Sets the initial value expression for this field. The expression will be evaluated during field initialization.
      Parameters:
      initialValueExpression - the Expression for initialization, or null
    • equals

      public boolean equals(Object obj)
      Overrides:
      equals in class Object
    • getOriginType

      public ClassNode getOriginType()
      Returns the original declared type of this field before any transformations. Useful for preserving type information through compilation phases that may modify types.
      Specified by:
      getOriginType in interface Variable
      Returns:
      the ClassNode representing the original type
    • setOriginType

      public void setOriginType(ClassNode cn)
      Sets the original type information for this field. Typically set during type transformation to preserve original type metadata.
      Parameters:
      cn - the original ClassNode type
    • rename

      public void rename(String name)
      Renames this field within its declaring class. Updates both the AST node and the declaring class's field registry.
      Parameters:
      name - the new field name