Class ASTNode

java.lang.Object
org.codehaus.groovy.ast.ASTNode
All Implemented Interfaces:
NodeMetaDataHandler
Direct Known Subclasses:
AnnotatedNode, AnnotationNode, GenericsType, ModifierNode, ModuleNode, Statement

public class ASTNode extends Object implements NodeMetaDataHandler
Base class for any AST node. This class supports basic information used in all nodes of the AST:
  • line and column number information. Usually a node represents a certain area in a text file determined by a starting position and an ending position. For nodes that do not represent this, this information will be -1. A node can also be configured in its line/col information using another node through setSourcePosition(otherNode).
  • every node can store meta data. A phase operation or transform can use this to transport arbitrary information to another phase operation or transform. The only requirement is that the other phase operation or transform runs after the part storing the information. If the information transport is done it is strongly recommended to remove that meta data.
  • a text representation of this node trough getText(). This was in the past used for assertion messages. Since the usage of power asserts this method will not be called for this purpose anymore and might be removed in future versions of Groovy
  • Constructor Details

    • ASTNode

      public ASTNode()
  • Method Details

    • visit

      public void visit(GroovyCodeVisitor visitor)
      Accepts a code visitor for AST traversal and transformation. Subclasses must implement this method to support visitor pattern-based processing. The visitor pattern enables decoupling of AST structure from processing logic.
      Parameters:
      visitor - the GroovyCodeVisitor to process this node
      Throws:
      RuntimeException - if visitor pattern support is not implemented for this node type
    • getText

      public String getText()
      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.
      Returns:
      text representation of this node, or placeholder for unimplemented types
    • getLineNumber

      public int getLineNumber()
      Returns the line number where this AST node begins in the source file. Line numbers start from 1. Returns -1 if position information is not available (for synthetic or generated nodes).
      Returns:
      the starting line number, or -1 if not available
    • setLineNumber

      public void setLineNumber(int lineNumber)
      Sets the starting line number for this AST node in the source file. Line numbers are 1-indexed. Use -1 to indicate unavailable position.
      Parameters:
      lineNumber - the starting line number to set
    • getColumnNumber

      public int getColumnNumber()
      Returns the column number where this AST node begins in the source file. Column numbers are 0-indexed. Returns -1 if position information is unavailable (for synthetic or generated nodes).
      Returns:
      the starting column number, or -1 if not available
    • setColumnNumber

      public void setColumnNumber(int columnNumber)
      Sets the starting column number for this AST node in the source file. Column numbers are 0-indexed. Use -1 to indicate unavailable position.
      Parameters:
      columnNumber - the starting column number to set
    • getLastLineNumber

      public int getLastLineNumber()
      Returns the line number where this AST node ends in the source file. Line numbers start from 1. Returns -1 if position information is unavailable. The end position is typically inclusive (last line of the node's span).
      Returns:
      the ending line number, or -1 if not available
    • setLastLineNumber

      public void setLastLineNumber(int lastLineNumber)
      Sets the ending line number for this AST node in the source file. Line numbers are 1-indexed. Use -1 to indicate unavailable position. The end position should typically be inclusive (last line of the node's span).
      Parameters:
      lastLineNumber - the ending line number to set
    • getLastColumnNumber

      public int getLastColumnNumber()
      Returns the column number where this AST node ends in the source file. Column numbers are 0-indexed. Returns -1 if position information is unavailable. The end position is typically exclusive (one past the last character).
      Returns:
      the ending column number, or -1 if not available
    • setLastColumnNumber

      public void setLastColumnNumber(int lastColumnNumber)
      Sets the ending column number for this AST node in the source file. Column numbers are 0-indexed. Use -1 to indicate unavailable position. The end position should typically be exclusive (one past the last character).
      Parameters:
      lastColumnNumber - the ending column number to set
    • setSourcePosition

      public void setSourcePosition(ASTNode node)
      Sets the source position information using another ASTNode as reference. Copies all position data (line/column start and end) from the source node, enabling consistent source location tracking for synthetic or transformed nodes.
      Parameters:
      node - the reference node providing position information
    • copyNodeMetaData

      public void copyNodeMetaData(ASTNode other)
      Copies all node metadata from another ASTNode. Metadata is arbitrary information attached by compiler phases or AST transforms for inter-phase communication. This method performs a deep metadata transfer.
      Parameters:
      other - the source node to copy metadata from
    • getMetaDataMap

      public Map<?,?> getMetaDataMap()
      Returns the metadata map for this node. Metadata stores arbitrary phase-specific information attached during compilation. Returns null if no metadata has been set.
      Specified by:
      getMetaDataMap in interface NodeMetaDataHandler
      Returns:
      the metadata map, or null if empty
    • setMetaDataMap

      public void setMetaDataMap(Map<?,?> metaDataMap)
      Sets the metadata map for this node. Replaces any existing metadata. Metadata stores arbitrary phase-specific information for inter-phase communication during compilation.
      Specified by:
      setMetaDataMap in interface NodeMetaDataHandler
      Parameters:
      metaDataMap - the metadata map to set