Package: org.codehaus.groovy.ast.expr

## [Java] Class CastExpression

- org.codehaus.groovy.ast.expr.CastExpression

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

  See Also:
    Expression for the target expression being cast
    ClassNode for the target type

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

  - ### Constructor Summary

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

  - ### Methods Summary

    Methods
    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)`

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

- - ### Constructor Detail

    - #### public CastExpression(ClassNode type, Expression expression)

      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

    - #### public CastExpression(ClassNode type, Expression expression, boolean ignoreAutoboxing)

      Creates a cast expression with optional autoboxing control.

      Parameters:
        `type` - the target type for the cast (non-null)
        `expression` - the expression being cast
        `ignoreAutoboxing` - whether to ignore autoboxing/unboxing conversions

  - ### Method Detail

    - #### public static CastExpression asExpression(ClassNode type, Expression expression)

      Creates a cast expression with coercion semantics using the `as` operator.

      Parameters:
        `type` - the target type for coercion (non-null)
        `expression` - the expression being coerced
      Returns:
        a cast expression configured for coercion

    - #### public Expression getExpression()

      Returns the expression being cast.

      Returns:
        the source expression (may be null)

    - #### public String getText()

    - #### public boolean isCoerce()

      Indicates whether this is a coercion expression (using `as`) rather than a strict cast.

      Returns:
        true if this uses coercion semantics, false for explicit casting

    - #### public boolean isIgnoringAutoboxing()

      Indicates whether autoboxing is ignored for this cast.

      Returns:
        true if autoboxing should be ignored, false otherwise

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

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

    - #### public void setExpression(Expression expression)

      Sets the expression being cast.

      Parameters:
        `expression` - the new expression

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

    - #### public void setType(ClassNode type)

      Overridden to prevent changing the target type after construction.

      throws:
        UnsupportedOperationException always, as the type is fixed at construction

    - #### public String toString()

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

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