public class CastExpression
extends Expression
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.
| Fields inherited from class | Fields |
|---|---|
class Expression |
EMPTY_ARRAY |
| Constructor and description |
|---|
CastExpression(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. |
| Type Params | Return Type | Name and description |
|---|---|---|
|
public static CastExpression |
asExpression(ClassNode type, Expression expression)Creates a cast expression with coercion semantics using the as operator. |
|
public Expression |
getExpression()Returns the expression being cast. |
|
public String |
getText() |
|
public boolean |
isCoerce()Indicates whether this is a coercion expression (using as) rather than a strict cast. |
|
public boolean |
isIgnoringAutoboxing()Indicates whether autoboxing is ignored for this cast. |
|
public boolean |
isStrict()Indicates whether this uses strict Java cast (CHECKCAST bytecode) instead of Groovy coercion. |
|
public void |
setCoerce(boolean coerce)Sets whether this expression uses coercion (loose Groovy semantics) or strict casting. |
|
public void |
setExpression(Expression expression)Sets the expression being cast. |
|
public void |
setStrict(boolean strict)Sets strict casting mode. |
|
public void |
setType(ClassNode type)Overridden to prevent changing the target type after construction. |
|
public String |
toString() |
|
public Expression |
transformExpression(ExpressionTransformer transformer) |
|
public void |
visit(GroovyCodeVisitor visitor) |
| 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 |
Creates a cast expression for the specified type and expression.
type - the target type for the cast (non-null)expression - the expression being castCreates a cast expression with optional autoboxing control.
type - the target type for the cast (non-null)expression - the expression being castignoreAutoboxing - whether to ignore autoboxing/unboxing conversions Creates a cast expression with coercion semantics using the as operator.
type - the target type for coercion (non-null)expression - the expression being coercedReturns the expression being cast.
Indicates whether this is a coercion expression (using as) rather than a strict cast.
Indicates whether autoboxing is ignored for this cast.
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.
Sets whether this expression uses coercion (loose Groovy semantics) or strict casting.
coerce - true for coercion, false for explicit castSets the expression being cast.
expression - the new expressionSets strict casting mode. If true, the compiler generates a strict Java cast (CHECKCAST) and disables Groovy coercion semantics.
strict - true for strict Java-like casting, false for Groovy coercionOverridden to prevent changing the target type after construction.
Copyright © 2003-2026 The Apache Software Foundation. All rights reserved.