Class CastExpression

All Implemented Interfaces:
GroovydocHolder<AnnotatedNode>, NodeMetaDataHandler

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.
  • Constructor Details

    • CastExpression

      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
    • CastExpression

      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 Details

    • asExpression

      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
    • getExpression

      public Expression getExpression()
      Returns the expression being cast.
      Returns:
      the source expression (may be null)
    • setExpression

      public void setExpression(Expression expression)
      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 (using as) 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

      public String toString()
      Overrides:
      toString in class Object
    • visit

      public void visit(GroovyCodeVisitor visitor)
      Description copied from class: ASTNode
      Accepts 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:
      visit in class ASTNode
      Parameters:
      visitor - the GroovyCodeVisitor to process this node
    • transformExpression

      public Expression transformExpression(ExpressionTransformer transformer)
      Description copied from class: Expression
      Transforms 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:
      transformExpression in class Expression
      Parameters:
      transformer - the ExpressionTransformer to apply
      Returns:
      a transformed copy of this expression (or this expression itself if no changes are needed)
    • getText

      public String getText()
      Description copied from class: ASTNode
      Returns 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.
      Overrides:
      getText in class ASTNode
      Returns:
      text representation of this node, or placeholder for unimplemented types
    • setType

      public void setType(ClassNode type)
      Overridden to prevent changing the target type after construction.
      Overrides:
      setType in class Expression
      Parameters:
      type - the ClassNode representing this expression's type
      Throws:
      UnsupportedOperationException - always, as the type is fixed at construction