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
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 Summary
FieldsModifier and TypeFieldDescriptionstatic final ConstantExpressionstatic final ConstantExpressionstatic final ConstantExpressionstatic final ConstantExpressionstatic final ConstantExpressionstatic final ConstantExpressionstatic final ConstantExpressionstatic final ConstantExpressionFields inherited from class org.codehaus.groovy.ast.expr.Expression
EMPTY_ARRAYFields inherited from interface groovy.lang.groovydoc.GroovydocHolder
DOC_COMMENT -
Constructor Summary
ConstructorsConstructorDescriptionConstantExpression(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 TypeMethodDescriptionReturns the constant name associated with this expression.getText()Returns a human-readable text representation of this AST node.getValue()Returns the constant value represented by this expression.booleanIndicates whether this constant expression represents an empty string value.booleanIndicates whether this constant expression represents the boolean false value.booleanIndicates whether this constant expression represents a null value.booleanIndicates whether this constant expression represents the boolean true value.voidsetConstantName(String constantName) Sets the constant name for this expression.toString()transformExpression(ExpressionTransformer transformer) Transforms this expression and any nested expressions according to the provided transformer.voidvisit(GroovyCodeVisitor visitor) Accepts a code visitor for AST traversal and transformation.Methods inherited from class org.codehaus.groovy.ast.expr.Expression
getType, setType, transformExpressions, transformExpressionsMethods inherited from class org.codehaus.groovy.ast.AnnotatedNode
addAnnotation, addAnnotation, addAnnotations, getAnnotations, getAnnotations, getDeclaringClass, getGroovydoc, getInstance, hasNoRealSourcePosition, isSynthetic, setDeclaringClass, setHasNoRealSourcePosition, setSyntheticMethods inherited from class org.codehaus.groovy.ast.ASTNode
copyNodeMetaData, getColumnNumber, getLastColumnNumber, getLastLineNumber, getLineNumber, getMetaDataMap, setColumnNumber, setLastColumnNumber, setLastLineNumber, setLineNumber, setMetaDataMap, setSourcePositionMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface org.codehaus.groovy.ast.NodeMetaDataHandler
copyNodeMetaData, getNodeMetaData, getNodeMetaData, getNodeMetaData, newMetaDataMap, putNodeMetaData, removeNodeMetaData, setNodeMetaData
-
Field Details
-
NULL
-
TRUE
-
FALSE
-
EMPTY_STRING
-
PRIM_TRUE
-
PRIM_FALSE
-
VOID
-
EMPTY_EXPRESSION
-
-
Constructor Details
-
ConstantExpression
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
Creates a constant expression with optional primitive type preservation. IfkeepPrimitiveis 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 nullkeepPrimitive- if true, preserve primitive types for wrapper classes (e.g., Integer becomes int); if false, use the boxed type
-
-
Method Details
-
toString
-
visit
Description copied from class:ASTNodeAccepts 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:
visitin classASTNode- Parameters:
visitor- theGroovyCodeVisitorto process this node
-
transformExpression
Description copied from class:ExpressionTransforms 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:
transformExpressionin classExpression- Parameters:
transformer- theExpressionTransformerto apply- Returns:
- a transformed copy of this expression (or this expression itself if no changes are needed)
-
getValue
Returns the constant value represented by this expression.- Returns:
- the constant value; may be null to represent a null literal
-
getText
Description copied from class:ASTNodeReturns 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. -
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
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
-