Package org.codehaus.groovy.ast.expr

# Class DeclarationExpression

java.lang.Object
org.codehaus.groovy.ast.ASTNode
org.codehaus.groovy.ast.AnnotatedNode
org.codehaus.groovy.ast.expr.Expression
org.codehaus.groovy.ast.expr.BinaryExpression
org.codehaus.groovy.ast.expr.DeclarationExpression

All Implemented Interfaces:
  `GroovydocHolder<AnnotatedNode>`, `NodeMetaDataHandler`

public class DeclarationExpression extends BinaryExpression
Represents one or more local variables. Typically it is a single local variable declared by name with an expression like "def foo" or with type "String foo". However, the multiple assignment feature allows you to create two or more variables using an expression like: `def (x, y) = [1, 2]`.

You can access the left hand side of a declaration using the "`Expression getLeftExpression()`" method. In which case you might then use `instanceof` and casting to perform operations specific to a single local variable (`VariableExpression`) or for the multiple assignment case (`TupleExpression`).

Alternatively, if `isMultipleAssignmentDeclaration()` is `false` you can use the method "`VariableExpression getVariableExpression()`" method. Similarly, if `isMultipleAssignmentDeclaration()` is `true` you can use the method "`TupleExpression getTupleExpression()`" method. Calling either of these expression getters when the "isMultipleAssignment" condition is not appropriate is unsafe and will result in a `ClassCastException`.

JEP 394 type patterns reuse this node as the right-hand side of `instanceof`: `e instanceof String s` is modelled as an `instanceof` `BinaryExpression` whose RHS is a `DeclarationExpression` with an `EmptyExpression` initializer. The pattern variable's scope is flow-sensitive (see GROOVY-12242).

- ## Field Summary

  ### Fields inherited from class org.codehaus.groovy.ast.expr.Expression

  `EMPTY_ARRAY`

  ### Fields inherited from interface groovy.lang.groovydoc.GroovydocHolder

  `DOC_COMMENT`

- ## Constructor Summary

  Constructors
  Constructor
  Description
  `DeclarationExpression(Expression left, Token operation, Expression right)`
  Creates a declaration like "def v" or "int w = 0" or "def (x, y) = [1, 2]".
  `DeclarationExpression(VariableExpression left, Token operation, Expression right)`
  Creates a declaration like "def v" or "int w = 0".

- ## Method Summary

  Modifier and Type
  Method
  Description
  `String`
  `getText()`
  Returns a human-readable text representation of this AST node.
  `TupleExpression`
  `getTupleExpression()`
  This method returns the left hand side of the declaration cast to the TupleExpression type.
  `ClassNode`
  `getType()`
  Returns the type of the declared variable (or of the tuple for a multiple assignment).
  `VariableExpression`
  `getVariableExpression()`
  This method returns the left hand side of the declaration cast to the VariableExpression type.
  `boolean`
  `isMultipleAssignmentDeclaration()`
  This method tells you if this declaration is a multiple assignment declaration, which has the form "def (x, y) = ..." in Groovy.
  `void`
  `setLeftExpression(Expression leftExpression)`
  This method sets the leftExpression for this BinaryExpression.
  `void`
  `setRightExpression(Expression rightExpression)`
  Sets the right operand of this binary expression.
  `Expression`
  `transformExpression(ExpressionTransformer transformer)`
  Transforms this expression and any nested expressions according to the provided transformer.
  `void`
  `visit(GroovyCodeVisitor visitor)`
  Accepts a code visitor for AST traversal and transformation.

  ### Methods inherited from class org.codehaus.groovy.ast.expr.BinaryExpression

  `getLeftExpression, getOperation, getRightExpression, isSafe, newAssignmentExpression, newInitializationExpression, setSafe, toString`

  ### Methods inherited from class org.codehaus.groovy.ast.expr.Expression

  `setType, transformExpressions, transformExpressions`

  ### Methods inherited from class org.codehaus.groovy.ast.AnnotatedNode

  `addAnnotation, addAnnotation, addAnnotations, getAnnotations, getAnnotations, getDeclaringClass, getGroovydoc, getInstance, hasNoRealSourcePosition, isSynthetic, setDeclaringClass, setHasNoRealSourcePosition, setSynthetic`

  ### Methods inherited from class org.codehaus.groovy.ast.ASTNode

  `copyNodeMetaData, getColumnNumber, getLastColumnNumber, getLastLineNumber, getLineNumber, getMetaDataMap, setColumnNumber, setLastColumnNumber, setLastLineNumber, setLineNumber, setMetaDataMap, setSourcePosition`

  ### Methods inherited from class java.lang.Object

  `clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait`

  ### Methods inherited from interface org.codehaus.groovy.ast.NodeMetaDataHandler

  `copyNodeMetaData, getNodeMetaData, getNodeMetaData, getNodeMetaData, newMetaDataMap, putNodeMetaData, removeNodeMetaData, setNodeMetaData`

- ## Constructor Details

  - ### DeclarationExpression

    public DeclarationExpression(VariableExpression left, Token operation, Expression right)
    Creates a declaration like "def v" or "int w = 0".
    Parameters:
      `left` - the left hand side of a variable declaration
      `operation` - the operation, assumed to be assignment operator
      `right` - the right hand side of a declaration; `EmptyExpression` for no initial value

  - ### DeclarationExpression

    public DeclarationExpression(Expression left, Token operation, Expression right)
    Creates a declaration like "def v" or "int w = 0" or "def (x, y) = [1, 2]".
    Parameters:
      `left` - the left hand side of a declaration -- either a `VariableExpression` or a `TupleExpression` with at least one element
      `operation` - the operation, assumed to be assignment operator
      `right` - the right hand side of a declaration

- ## Method Details

  - ### 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 `BinaryExpression`
    Parameters:
      `visitor` - the `GroovyCodeVisitor` to process this node

  - ### getVariableExpression

    public VariableExpression getVariableExpression()
    This method returns the left hand side of the declaration cast to the VariableExpression type. This is an unsafe method to call. In a multiple assignment statement, the left hand side will be a TupleExpression and a ClassCastException will occur. If you invoke this method then be sure to invoke isMultipleAssignmentDeclaration() first to check that it is safe to do so. If that method returns true then this method is safe to call.
    Returns:
      left hand side of normal variable declarations
    Throws:
      `ClassCastException` - if the left hand side is not a VariableExpression (and is probably a multiple assignment statement).

  - ### getTupleExpression

    public TupleExpression getTupleExpression()
    This method returns the left hand side of the declaration cast to the TupleExpression type. This is an unsafe method to call. In a single assignment statement, the left hand side will be a VariableExpression and a ClassCastException will occur. If you invoke this method then be sure to invoke isMultipleAssignmentDeclaration() first to check that it is safe to do so. If that method returns true then this method is safe to call.
    Returns:
      left hand side of multiple assignment declarations
    Throws:
      `ClassCastException` - if the left hand side is not a TupleExpression (and is probably a VariableExpression).

  - ### getType

    public ClassNode getType()
    Returns the type of the declared variable (or of the tuple for a multiple assignment). For a JEP 394 pattern used as the RHS of `instanceof`, this is the pattern type `T` in `e instanceof T t`.
    Overrides:
      `getType` in class `Expression`
    Returns:
      the `ClassNode` representing this expression's type

  - ### 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 `BinaryExpression`
    Returns:
      text representation of this node, or placeholder for unimplemented types

  - ### setLeftExpression

    public void setLeftExpression(Expression leftExpression)
    This method sets the leftExpression for this BinaryExpression. The parameter must be either a VariableExpression or a TupleExpression with one or more elements.
    Overrides:
      `setLeftExpression` in class `BinaryExpression`
    Parameters:
      `leftExpression` - either a VariableExpression or a TupleExpression with one or more elements.

  - ### setRightExpression

    public void setRightExpression(Expression rightExpression)
    Description copied from class: `BinaryExpression`
    Sets the right operand of this binary expression.
    Overrides:
      `setRightExpression` in class `BinaryExpression`
    Parameters:
      `rightExpression` - the new right operand; must not be null

  - ### 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.
    Overrides:
      `transformExpression` in class `BinaryExpression`
    Parameters:
      `transformer` - the `ExpressionTransformer` to apply
    Returns:
      a transformed copy of this expression (or this expression itself if no changes are needed)

  - ### isMultipleAssignmentDeclaration

    public boolean isMultipleAssignmentDeclaration()
    This method tells you if this declaration is a multiple assignment declaration, which has the form "def (x, y) = ..." in Groovy. If this method returns true, then the left hand side is an ArgumentListExpression. Do not call "getVariableExpression()" on this object if this method returns true, instead use "getLeftExpression()".
    Returns:
      true if this declaration is a multiple assignment declaration, which means the left hand side is an ArgumentListExpression.