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
-
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 useinstanceof
and casting to perform operations specific to a single local variable (VariableExpression
) or for the multiple assignment case (TupleExpression
).Alternatively, if
isMultipleAssignmentDeclaration()
isfalse
you can use the method "VariableExpression getVariableExpression()
" method. Similarly, ifisMultipleAssignmentDeclaration()
istrue
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 aClassCastException
.
-
-
Constructor Summary
Constructors Constructor Description DeclarationExpression(Expression left, Token operation, Expression right)
Creates a DeclarationExpression for Expressions like "def (x, y) = [1, 2]"DeclarationExpression(VariableExpression left, Token operation, Expression right)
Creates a DeclarationExpression for VariableExpressions like "def x" or "String y = 'foo'".
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description java.lang.String
getText()
TupleExpression
getTupleExpression()
This method returns the left hand side of the declaration cast to the TupleExpression type.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)
Expression
transformExpression(ExpressionTransformer transformer)
Return a copy of the expression calling the transformer on any nested expressionsvoid
visit(GroovyCodeVisitor visitor)
-
Methods inherited from class org.codehaus.groovy.ast.expr.BinaryExpression
getLeftExpression, getOperation, getRightExpression, newAssignmentExpression, newInitializationExpression, toString
-
Methods inherited from class org.codehaus.groovy.ast.expr.Expression
getType, setType, transformExpressions, transformExpressions
-
Methods inherited from class org.codehaus.groovy.ast.AnnotatedNode
addAnnotation, addAnnotations, getAnnotations, getAnnotations, getDeclaringClass, hasNoRealSourcePosition, isSynthetic, setDeclaringClass, setHasNoRealSourcePosition, setSynthetic
-
Methods inherited from class org.codehaus.groovy.ast.ASTNode
copyNodeMetaData, getColumnNumber, getLastColumnNumber, getLastLineNumber, getLineNumber, getMetaDataMap, getNodeMetaData, getNodeMetaData, putNodeMetaData, removeNodeMetaData, setColumnNumber, setLastColumnNumber, setLastLineNumber, setLineNumber, setNodeMetaData, setSourcePosition
-
-
-
-
Constructor Detail
-
DeclarationExpression
public DeclarationExpression(VariableExpression left, Token operation, Expression right)
Creates a DeclarationExpression for VariableExpressions like "def x" or "String y = 'foo'".- Parameters:
left
- the left hand side of a variable declarationoperation
- the operation, typically an assignment operatorright
- the right hand side of a declaration
-
DeclarationExpression
public DeclarationExpression(Expression left, Token operation, Expression right)
Creates a DeclarationExpression for Expressions like "def (x, y) = [1, 2]"- Parameters:
left
- the left hand side of a declaration. Must be either a VariableExpression or a TupleExpression with at least one element.operation
- the operation, typically an assignment operatorright
- the right hand side of a declaration
-
-
Method Detail
-
visit
public void visit(GroovyCodeVisitor visitor)
- Overrides:
visit
in classBinaryExpression
-
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:
java.lang.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:
java.lang.ClassCastException
- if the left hand side is not a TupleExpression (and is probably a VariableExpression).
-
getText
public java.lang.String getText()
- Overrides:
getText
in classBinaryExpression
-
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 classBinaryExpression
- Parameters:
leftExpression
- either a VariableExpression or a TupleExpression with one or more elements.
-
setRightExpression
public void setRightExpression(Expression rightExpression)
- Overrides:
setRightExpression
in classBinaryExpression
-
transformExpression
public Expression transformExpression(ExpressionTransformer transformer)
Description copied from class:Expression
Return a copy of the expression calling the transformer on any nested expressions- Overrides:
transformExpression
in classBinaryExpression
-
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.
-
-