Package: org.codehaus.groovy.ast.expr

## [Java] Class ConstantExpression

- org.codehaus.groovy.ast.expr.ConstantExpression

-     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
    Modifiers | Name | Description
    --- | --- | ---
    `static ConstantExpression` | `EMPTY_EXPRESSION` |
    `static ConstantExpression` | `EMPTY_STRING` |
    `static ConstantExpression` | `FALSE` |
    `static ConstantExpression` | `NULL` |
    `static ConstantExpression` | `PRIM_FALSE` |
    `static ConstantExpression` | `PRIM_TRUE` |
    `static ConstantExpression` | `TRUE` |
    `static ConstantExpression` | `VOID` |

  - Inherited fields
    Fields inherited from class | Fields
    --- | ---
    `class Expression` | `EMPTY_ARRAY`

  - ### Constructor Summary

    Constructors
    Constructor and 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.

  - ### Methods Summary

    Methods
    Type Params | Return Type | Name and description
    --- | --- | ---
    `` | `public String` | `getConstantName()` Returns the constant name associated with this expression.
    `` | `public String` | `getText()`
    `` | `public Object` | `getValue()` Returns the constant value represented by this expression.
    `` | `public boolean` | `isEmptyStringExpression()` Indicates whether this constant expression represents an empty string value.
    `` | `public boolean` | `isFalseExpression()` Indicates whether this constant expression represents the boolean false value.
    `` | `public boolean` | `isNullExpression()` Indicates whether this constant expression represents a null value.
    `` | `public boolean` | `isTrueExpression()` Indicates whether this constant expression represents the boolean true value.
    `` | `public void` | `setConstantName(String constantName)` Sets the constant name for this expression.
    `` | `public String` | `toString()`
    `` | `public Expression` | `transformExpression(ExpressionTransformer transformer)`
    `` | `public void` | `visit(GroovyCodeVisitor visitor)`

  - ### Inherited Methods Summary

    Inherited Methods
    Methods inherited from class | Name
    --- | ---
    `class Expression` | `getType, setType, transformExpression, transformExpressions, transformExpressions`
    `class AnnotatedNode` | `addAnnotation, addAnnotation, addAnnotations, getAnnotations, getAnnotations, getDeclaringClass, getGroovydoc, getInstance, hasNoRealSourcePosition, isSynthetic, setDeclaringClass, setHasNoRealSourcePosition, setSynthetic`
    `class ASTNode` | `copyNodeMetaData, getColumnNumber, getLastColumnNumber, getLastLineNumber, getLineNumber, getMetaDataMap, getText, setColumnNumber, setLastColumnNumber, setLastLineNumber, setLineNumber, setMetaDataMap, setSourcePosition, visit`

- - ### Field Detail

    - #### public static final ConstantExpression EMPTY_EXPRESSION

    - #### public static final ConstantExpression EMPTY_STRING

    - #### public static final ConstantExpression FALSE

    - #### public static final ConstantExpression NULL

    - #### public static final ConstantExpression PRIM_FALSE

    - #### public static final ConstantExpression PRIM_TRUE

    - #### public static final ConstantExpression TRUE

    - #### public static final ConstantExpression VOID

  - ### Constructor Detail

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

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

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

    - #### public String getText()

    - #### public Object getValue()

      Returns the constant value represented by this expression.

      Returns:
        the constant value; may be null to represent a null literal

    - #### public boolean isEmptyStringExpression()

      Indicates whether this constant expression represents an empty string value.

      Returns:
        true if this expression represents an empty string; false otherwise

    - #### public boolean isFalseExpression()

      Indicates whether this constant expression represents the boolean false value.

      Returns:
        true if this expression represents Boolean.FALSE; false otherwise

    - #### public boolean isNullExpression()

      Indicates whether this constant expression represents a null value.

      Returns:
        true if this expression represents null; false otherwise

    - #### public boolean isTrueExpression()

      Indicates whether this constant expression represents the boolean true value.

      Returns:
        true if this expression represents Boolean.TRUE; false otherwise

    - #### public void setConstantName(String constantName)

      Sets the constant name for this expression.

      Parameters:
        `constantName` - the name to associate with this constant; may be null

    - #### public String toString()

    - #### public Expression transformExpression(ExpressionTransformer transformer)

    - #### public void visit(GroovyCodeVisitor visitor)