Class ConstantExpression

All Implemented Interfaces:
GroovydocHolder<AnnotatedNode>, NodeMetaDataHandler
Direct Known Subclasses:
AnnotationConstantExpression

public class ConstantExpression extends Expression
Represents a constant value expression such as literals (null, true, false, numbers, strings), class literals, and other compile-time constant values. Each user-defined constant expression maintains its own instance to preserve line and column information for accurate error reporting. Predefined instances (e.g., NULL, TRUE, FALSE) are provided as internal constants and should not be compared directly; use the isXXXExpression() methods instead.
See Also:
  • Field Details

  • Constructor Details

    • ConstantExpression

      public ConstantExpression(Object value)
      Creates a constant expression with the specified value. The type of this constant expression will be inferred from the value's class.
      Parameters:
      value - the constant value; may be null to represent a null literal
    • ConstantExpression

      public ConstantExpression(Object value, boolean keepPrimitive)
      Creates a constant expression with optional primitive type preservation. If keepPrimitive is true, primitive wrapper types (Integer, Long, Boolean, etc.) are kept as their primitive equivalents rather than boxed types.
      Parameters:
      value - the constant value; may be null
      keepPrimitive - if true, preserve primitive types for wrapper classes (e.g., Integer becomes int); if false, use the boxed type
  • Method Details

    • toString

      public String toString()
      Overrides:
      toString in class Object
    • visit

      public void visit(GroovyCodeVisitor visitor)
      Description copied from class: ASTNode
      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.
      Overrides:
      visit in class ASTNode
      Parameters:
      visitor - the GroovyCodeVisitor to process this node
    • transformExpression

      public Expression transformExpression(ExpressionTransformer transformer)
      Description copied from class: Expression
      Transforms this expression and any nested expressions according to the provided transformer. This method is called during AST transformation phases and must recursively transform any nested expressions to support full AST tree transformation.
      Specified by:
      transformExpression in class Expression
      Parameters:
      transformer - the ExpressionTransformer to apply
      Returns:
      a transformed copy of this expression (or this expression itself if no changes are needed)
    • getValue

      public Object getValue()
      Returns the constant value represented by this expression.
      Returns:
      the constant value; may be null to represent a null literal
    • 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
    • getConstantName

      public String getConstantName()
      Returns the constant name associated with this expression. This is typically used for named constants defined in source code.
      Returns:
      the constant name; may be null if not set
    • setConstantName

      public void setConstantName(String constantName)
      Sets the constant name for this expression.
      Parameters:
      constantName - the name to associate with this constant; may be null
    • isNullExpression

      public boolean isNullExpression()
      Indicates whether this constant expression represents a null value.
      Returns:
      true if this expression represents null; false otherwise
    • isTrueExpression

      public boolean isTrueExpression()
      Indicates whether this constant expression represents the boolean true value.
      Returns:
      true if this expression represents Boolean.TRUE; false otherwise
    • isFalseExpression

      public boolean isFalseExpression()
      Indicates whether this constant expression represents the boolean false value.
      Returns:
      true if this expression represents Boolean.FALSE; false otherwise
    • isEmptyStringExpression

      public boolean isEmptyStringExpression()
      Indicates whether this constant expression represents an empty string value.
      Returns:
      true if this expression represents an empty string; false otherwise