Package org.codehaus.groovy.ast.expr
Class CastExpression
java.lang.Object
org.codehaus.groovy.ast.ASTNode
org.codehaus.groovy.ast.AnnotatedNode
org.codehaus.groovy.ast.expr.Expression
org.codehaus.groovy.ast.expr.CastExpression
- All Implemented Interfaces:
GroovydocHolder<AnnotatedNode>,NodeMetaDataHandler
Represents a type cast expression in Groovy.
Supports both explicit casts (e.g.,
(String) obj) and coercion expressions (e.g., obj as String).
The expression can operate in strict mode (using Java CHECKCAST) or loose mode (using Groovy coercion semantics).
Autoboxing may be ignored for certain optimizations.-
Field Summary
Fields inherited from class org.codehaus.groovy.ast.expr.Expression
EMPTY_ARRAYFields inherited from interface groovy.lang.groovydoc.GroovydocHolder
DOC_COMMENT -
Constructor Summary
ConstructorsConstructorDescriptionCastExpression(ClassNode type, Expression expression) Creates a cast expression for the specified type and expression.CastExpression(ClassNode type, Expression expression, boolean ignoreAutoboxing) Creates a cast expression with optional autoboxing control. -
Method Summary
Modifier and TypeMethodDescriptionstatic CastExpressionasExpression(ClassNode type, Expression expression) Creates a cast expression with coercion semantics using theasoperator.Returns the expression being cast.getText()Returns a human-readable text representation of this AST node.booleanisCoerce()Indicates whether this is a coercion expression (usingas) rather than a strict cast.booleanIndicates whether autoboxing is ignored for this cast.booleanisStrict()Indicates whether this uses strict Java cast (CHECKCAST bytecode) instead of Groovy coercion.voidsetCoerce(boolean coerce) Sets whether this expression uses coercion (loose Groovy semantics) or strict casting.voidsetExpression(Expression expression) Sets the expression being cast.voidsetStrict(boolean strict) Sets strict casting mode.voidOverridden to prevent changing the target type after construction.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, 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
-
Constructor Details
-
CastExpression
Creates a cast expression for the specified type and expression.- Parameters:
type- the target type for the cast (non-null)expression- the expression being cast
-
CastExpression
Creates a cast expression with optional autoboxing control.- Parameters:
type- the target type for the cast (non-null)expression- the expression being castignoreAutoboxing- whether to ignore autoboxing/unboxing conversions
-
-
Method Details
-
asExpression
Creates a cast expression with coercion semantics using theasoperator.- Parameters:
type- the target type for coercion (non-null)expression- the expression being coerced- Returns:
- a cast expression configured for coercion
-
getExpression
Returns the expression being cast.- Returns:
- the source expression (may be null)
-
setExpression
Sets the expression being cast.- Parameters:
expression- the new expression
-
isIgnoringAutoboxing
public boolean isIgnoringAutoboxing()Indicates whether autoboxing is ignored for this cast.- Returns:
- true if autoboxing should be ignored, false otherwise
-
isCoerce
public boolean isCoerce()Indicates whether this is a coercion expression (usingas) rather than a strict cast.- Returns:
- true if this uses coercion semantics, false for explicit casting
-
setCoerce
public void setCoerce(boolean coerce) Sets whether this expression uses coercion (loose Groovy semantics) or strict casting.- Parameters:
coerce- true for coercion, false for explicit cast
-
isStrict
public boolean isStrict()Indicates whether this uses strict Java cast (CHECKCAST bytecode) instead of Groovy coercion. In strict mode, the compiler disables Groovy coercion semantics and relies on Java's type checking.- Returns:
- true if strict casting is enabled, false otherwise
-
setStrict
public void setStrict(boolean strict) Sets strict casting mode. If true, the compiler generates a strict Java cast (CHECKCAST) and disables Groovy coercion semantics.- Parameters:
strict- true for strict Java-like casting, false for Groovy coercion
-
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)
-
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. -
setType
Overridden to prevent changing the target type after construction.- Overrides:
setTypein classExpression- Parameters:
type- theClassNoderepresenting this expression's type- Throws:
UnsupportedOperationException- always, as the type is fixed at construction
-