Package org.codehaus.groovy.ast.expr

# Class ConstantExpression

java.lang.Object
org.codehaus.groovy.ast.ASTNode
org.codehaus.groovy.ast.AnnotatedNode
org.codehaus.groovy.ast.expr.Expression
org.codehaus.groovy.ast.expr.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:
  - `Expression`
  - `VariableExpression`
  - `BinaryExpression`

- ## Field Summary

  Fields
  Modifier and Type
  Field
  Description
  `static final ConstantExpression`
  `EMPTY_EXPRESSION`
  `static final ConstantExpression`
  `EMPTY_STRING`
  `static final ConstantExpression`
  `FALSE`
  `static final ConstantExpression`
  `NULL`
  `static final ConstantExpression`
  `PRIM_FALSE`
  `static final ConstantExpression`
  `PRIM_TRUE`
  `static final ConstantExpression`
  `TRUE`
  `static final ConstantExpression`
  `VOID`

  ### Fields inherited from class org.codehaus.groovy.ast.expr.Expression

  `EMPTY_ARRAY`

  ### Fields inherited from interface groovy.lang.groovydoc.GroovydocHolder

  `DOC_COMMENT`

- ## Constructor Summary

  Constructors
  Constructor
  Description
  `ConstantExpression(Object value)`
  Creates a constant expression with the specified value.
  `ConstantExpression(Object value, boolean keepPrimitive)`
  Creates a constant expression with optional primitive type preservation.

- ## Method Summary

  Modifier and Type
  Method
  Description
  `String`
  `getConstantName()`
  Returns the constant name associated with this expression.
  `String`
  `getText()`
  Returns a human-readable text representation of this AST node.
  `Object`
  `getValue()`
  Returns the constant value represented by this expression.
  `boolean`
  `isEmptyStringExpression()`
  Indicates whether this constant expression represents an empty string value.
  `boolean`
  `isFalseExpression()`
  Indicates whether this constant expression represents the boolean false value.
  `boolean`
  `isNullExpression()`
  Indicates whether this constant expression represents a null value.
  `boolean`
  `isTrueExpression()`
  Indicates whether this constant expression represents the boolean true value.
  `void`
  `setConstantName(String constantName)`
  Sets the constant name for this expression.
  `String`
  `toString()`
  `Expression`
  `transformExpression(ExpressionTransformer transformer)`
  Transforms this expression and any nested expressions according to the provided transformer.
  `void`
  `visit(GroovyCodeVisitor visitor)`
  Accepts a code visitor for AST traversal and transformation.

  ### Methods inherited from class org.codehaus.groovy.ast.expr.Expression

  `getType, setType, transformExpressions, transformExpressions`

  ### Methods inherited from class org.codehaus.groovy.ast.AnnotatedNode

  `addAnnotation, addAnnotation, addAnnotations, getAnnotations, getAnnotations, getDeclaringClass, getGroovydoc, getInstance, hasNoRealSourcePosition, isSynthetic, setDeclaringClass, setHasNoRealSourcePosition, setSynthetic`

  ### Methods inherited from class org.codehaus.groovy.ast.ASTNode

  `copyNodeMetaData, getColumnNumber, getLastColumnNumber, getLastLineNumber, getLineNumber, getMetaDataMap, setColumnNumber, setLastColumnNumber, setLastLineNumber, setLineNumber, setMetaDataMap, setSourcePosition`

  ### Methods inherited from class java.lang.Object

  `clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait`

  ### Methods inherited from interface org.codehaus.groovy.ast.NodeMetaDataHandler

  `copyNodeMetaData, getNodeMetaData, getNodeMetaData, getNodeMetaData, newMetaDataMap, putNodeMetaData, removeNodeMetaData, setNodeMetaData`

- ## Field Details

  - ### NULL

    public static final ConstantExpression NULL

  - ### TRUE

    public static final ConstantExpression TRUE

  - ### FALSE

    public static final ConstantExpression FALSE

  - ### EMPTY_STRING

    public static final ConstantExpression EMPTY_STRING

  - ### PRIM_TRUE

    public static final ConstantExpression PRIM_TRUE

  - ### PRIM_FALSE

    public static final ConstantExpression PRIM_FALSE

  - ### VOID

    public static final ConstantExpression VOID

  - ### EMPTY_EXPRESSION

    public static final ConstantExpression EMPTY_EXPRESSION

- ## 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