Top Description Constructors Methods
org.codehaus.groovy.ast.expr

public Class DeclarationExpression

extends BinaryExpression
Class Inheritance
Imports
org.codehaus.groovy.GroovyBugError, org.codehaus.groovy.ast.GroovyCodeVisitor, org.codehaus.groovy.syntax.Token

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.

Constructor Summary

AccessConstructor and Description
public
DeclarationExpression(final VariableExpression
the left hand side of a variable declaration
left
,
final Token
the operation, typically an assignment operator
operation
,
final Expression
the right hand side of a declaration
right
)

Creates a DeclarationExpression for VariableExpressions like "def x" or "String y = 'foo'".

public
DeclarationExpression(final Expression
the left hand side of a declaration. Must be either a VariableExpression or a TupleExpression with at least one element.
left
,
final Token
the operation, typically an assignment operator
operation
,
final Expression
the right hand side of a declaration
right
)

Creates a DeclarationExpression for Expressions like "def (x, y) = [1, 2]"

Method Summary

Modifier and TypeMethod and Description
private static void
public String
public TupleExpression

Returns:

left hand side of multiple assignment declarations
getTupleExpression
()

This method returns the left hand side of the declaration cast to the TupleExpression type.

public VariableExpression

Returns:

left hand side of normal variable declarations
getVariableExpression
()

This method returns the left hand side of the declaration cast to the VariableExpression type.

public boolean

Returns:

true if this declaration is a multiple assignment declaration, which means the left hand side is an ArgumentListExpression.
isMultipleAssignmentDeclaration
()

This method tells you if this declaration is a multiple assignment declaration, which has the form "def (x, y) = ..." in Groovy.

public void
setLeftExpression(Expression
either a VariableExpression or a TupleExpression with one or more elements.
leftExpression
)

Overrides org.codehaus.groovy.ast.expr.BinaryExpression.setLeftExpression.

This method sets the leftExpression for this BinaryExpression.
public void
public Expression
transformExpression(ExpressionTransformer transformer)

Overrides org.codehaus.groovy.ast.expr.BinaryExpression.transformExpression.

Return a copy of the expression calling the transformer on any nested expressions
public void
Inherited from org.codehaus.groovy.ast.expr.BinaryExpression:
getLeftExpressiongetOperationgetRightExpressionisSafenewAssignmentExpressionnewInitializationExpressionsetSafetoString

Constructor Detail

DeclarationExpressionback to summary
public DeclarationExpression(final VariableExpression left, final Token operation, final Expression right)

Creates a DeclarationExpression for VariableExpressions like "def x" or "String y = 'foo'".

Parameters
left:VariableExpression

the left hand side of a variable declaration

operation:Token

the operation, typically an assignment operator

right:Expression

the right hand side of a declaration

DeclarationExpressionback to summary
public DeclarationExpression(final Expression left, final Token operation, final Expression right)

Creates a DeclarationExpression for Expressions like "def (x, y) = [1, 2]"

Parameters
left:Expression

the left hand side of a declaration. Must be either a VariableExpression or a TupleExpression with at least one element.

operation:Token

the operation, typically an assignment operator

right:Expression

the right hand side of a declaration

Method Detail

checkback to summary
private static void check(Expression left)
getTextback to summary
public String getText()

Overrides org.codehaus.groovy.ast.expr.BinaryExpression.getText.

Annotations
@Override
getTupleExpressionback to summary
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:TupleExpression

left hand side of multiple assignment declarations

Exceptions
ClassCastException:
if the left hand side is not a TupleExpression (and is probably a VariableExpression).
getVariableExpressionback to summary
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:VariableExpression

left hand side of normal variable declarations

Exceptions
ClassCastException:
if the left hand side is not a VariableExpression (and is probably a multiple assignment statement).
isMultipleAssignmentDeclarationback to summary
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:boolean

true if this declaration is a multiple assignment declaration, which means the left hand side is an ArgumentListExpression.

setLeftExpressionback to summary
public void setLeftExpression(Expression leftExpression)

Overrides org.codehaus.groovy.ast.expr.BinaryExpression.setLeftExpression.

This method sets the leftExpression for this BinaryExpression. The parameter must be either a VariableExpression or a TupleExpression with one or more elements.

Parameters
leftExpression:Expression

either a VariableExpression or a TupleExpression with one or more elements.

Annotations
@Override
setRightExpressionback to summary
public void setRightExpression(Expression rightExpression)

Overrides org.codehaus.groovy.ast.expr.BinaryExpression.setRightExpression.

Annotations
@Override
transformExpressionback to summary
public Expression transformExpression(ExpressionTransformer transformer)

Overrides org.codehaus.groovy.ast.expr.BinaryExpression.transformExpression.

Doc from org.codehaus.groovy.ast.expr.Expression.transformExpression.

Return a copy of the expression calling the transformer on any nested expressions

Annotations
@Override
visitback to summary
public void visit(GroovyCodeVisitor visitor)

Overrides org.codehaus.groovy.ast.expr.BinaryExpression.visit.

Annotations
@Override