KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > core > dom > Assignment


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11
12 package org.eclipse.jdt.core.dom;
13
14 import java.util.ArrayList JavaDoc;
15 import java.util.HashMap JavaDoc;
16 import java.util.List JavaDoc;
17 import java.util.Map JavaDoc;
18
19 /**
20  * Assignment expression AST node type.
21  *
22  * <pre>
23  * Assignment:
24  * Expression AssignmentOperator Expression
25  * </pre>
26  *
27  * @since 2.0
28  */

29 public class Assignment extends Expression {
30         
31     /**
32      * Assignment operators (typesafe enumeration).
33      * <pre>
34      * AssignmentOperator:<code>
35      * <b>=</b> ASSIGN
36      * <b>+=</b> PLUS_ASSIGN
37      * <b>-=</b> MINUS_ASSIGN
38      * <b>*=</b> TIMES_ASSIGN
39      * <b>/=</b> DIVIDE_ASSIGN
40      * <b>&amp;=</b> BIT_AND_ASSIGN
41      * <b>|=</b> BIT_OR_ASSIGN
42      * <b>^=</b> BIT_XOR_ASSIGN
43      * <b>%=</b> REMAINDER_ASSIGN
44      * <b>&lt;&lt;=</b> LEFT_SHIFT_ASSIGN
45      * <b>&gt;&gt;=</b> RIGHT_SHIFT_SIGNED_ASSIGN
46      * <b>&gt;&gt;&gt;=</b> RIGHT_SHIFT_UNSIGNED_ASSIGN</code>
47      * </pre>
48      */

49     public static class Operator {
50     
51         /**
52          * The name of the operator
53          */

54         private String JavaDoc op;
55         
56         /**
57          * Creates a new assignment operator with the given name.
58          * <p>
59          * Note: this constructor is private. The only instances
60          * ever created are the ones for the standard operators.
61          * </p>
62          *
63          * @param op the character sequence for the operator
64          */

65         private Operator(String JavaDoc op) {
66             this.op = op;
67         }
68         
69         /**
70          * Returns the character sequence for the operator.
71          *
72          * @return the character sequence for the operator
73          */

74         public String JavaDoc toString() {
75             return op;
76         }
77         
78         /** = operator. */
79         public static final Operator ASSIGN = new Operator("=");//$NON-NLS-1$
80
/** += operator. */
81         public static final Operator PLUS_ASSIGN = new Operator("+=");//$NON-NLS-1$
82
/** -= operator. */
83         public static final Operator MINUS_ASSIGN = new Operator("-=");//$NON-NLS-1$
84
/** *= operator. */
85         public static final Operator TIMES_ASSIGN = new Operator("*=");//$NON-NLS-1$
86
/** /= operator. */
87         public static final Operator DIVIDE_ASSIGN = new Operator("/=");//$NON-NLS-1$
88
/** &amp;= operator. */
89         public static final Operator BIT_AND_ASSIGN = new Operator("&=");//$NON-NLS-1$
90
/** |= operator. */
91         public static final Operator BIT_OR_ASSIGN = new Operator("|=");//$NON-NLS-1$
92
/** ^= operator. */
93         public static final Operator BIT_XOR_ASSIGN = new Operator("^=");//$NON-NLS-1$
94
/** %= operator. */
95         public static final Operator REMAINDER_ASSIGN = new Operator("%=");//$NON-NLS-1$
96
/** &lt;&lt;== operator. */
97         public static final Operator LEFT_SHIFT_ASSIGN =
98             new Operator("<<=");//$NON-NLS-1$
99
/** &gt;&gt;= operator. */
100         public static final Operator RIGHT_SHIFT_SIGNED_ASSIGN =
101             new Operator(">>=");//$NON-NLS-1$
102
/** &gt;&gt;&gt;= operator. */
103         public static final Operator RIGHT_SHIFT_UNSIGNED_ASSIGN =
104             new Operator(">>>=");//$NON-NLS-1$
105

106         /**
107          * Returns the assignment operator corresponding to the given string,
108          * or <code>null</code> if none.
109          * <p>
110          * <code>toOperator</code> is the converse of <code>toString</code>:
111          * that is, <code>Operator.toOperator(op.toString()) == op</code> for all
112          * operators <code>op</code>.
113          * </p>
114          *
115          * @param token the character sequence for the operator
116          * @return the assignment operator, or <code>null</code> if none
117          */

118         public static Operator toOperator(String JavaDoc token) {
119             return (Operator) CODES.get(token);
120         }
121         
122         /**
123          * Map from token to operator (key type: <code>String</code>;
124          * value type: <code>Operator</code>).
125          */

126         private static final Map JavaDoc CODES;
127         static {
128             CODES = new HashMap JavaDoc(20);
129             Operator[] ops = {
130                     ASSIGN,
131                     PLUS_ASSIGN,
132                     MINUS_ASSIGN,
133                     TIMES_ASSIGN,
134                     DIVIDE_ASSIGN,
135                     BIT_AND_ASSIGN,
136                     BIT_OR_ASSIGN,
137                     BIT_XOR_ASSIGN,
138                     REMAINDER_ASSIGN,
139                     LEFT_SHIFT_ASSIGN,
140                     RIGHT_SHIFT_SIGNED_ASSIGN,
141                     RIGHT_SHIFT_UNSIGNED_ASSIGN
142                 };
143             for (int i = 0; i < ops.length; i++) {
144                 CODES.put(ops[i].toString(), ops[i]);
145             }
146         }
147     }
148     
149     /**
150      * The "leftHandSide" structural property of this node type.
151      * @since 3.0
152      */

153     public static final ChildPropertyDescriptor LEFT_HAND_SIDE_PROPERTY =
154         new ChildPropertyDescriptor(Assignment.class, "leftHandSide", Expression.class, MANDATORY, CYCLE_RISK); //$NON-NLS-1$
155

156     /**
157      * The "operator" structural property of this node type.
158      * @since 3.0
159      */

160     public static final SimplePropertyDescriptor OPERATOR_PROPERTY =
161         new SimplePropertyDescriptor(Assignment.class, "operator", Assignment.Operator.class, MANDATORY); //$NON-NLS-1$
162

163     /**
164      * The "rightHandSide" structural property of this node type.
165      * @since 3.0
166      */

167     public static final ChildPropertyDescriptor RIGHT_HAND_SIDE_PROPERTY =
168         new ChildPropertyDescriptor(Assignment.class, "rightHandSide", Expression.class, MANDATORY, CYCLE_RISK); //$NON-NLS-1$
169

170     /**
171      * A list of property descriptors (element type:
172      * {@link StructuralPropertyDescriptor}),
173      * or null if uninitialized.
174      */

175     private static final List JavaDoc PROPERTY_DESCRIPTORS;
176     
177     static {
178         List JavaDoc properyList = new ArrayList JavaDoc(4);
179         createPropertyList(Assignment.class, properyList);
180         addProperty(LEFT_HAND_SIDE_PROPERTY, properyList);
181         addProperty(OPERATOR_PROPERTY, properyList);
182         addProperty(RIGHT_HAND_SIDE_PROPERTY, properyList);
183         PROPERTY_DESCRIPTORS = reapPropertyList(properyList);
184     }
185
186     /**
187      * Returns a list of structural property descriptors for this node type.
188      * Clients must not modify the result.
189      *
190      * @param apiLevel the API level; one of the
191      * <code>AST.JLS*</code> constants
192
193      * @return a list of property descriptors (element type:
194      * {@link StructuralPropertyDescriptor})
195      * @since 3.0
196      */

197     public static List JavaDoc propertyDescriptors(int apiLevel) {
198         return PROPERTY_DESCRIPTORS;
199     }
200             
201     /**
202      * The assignment operator; defaults to Assignment.Operator.ASSIGN
203      */

204     private Assignment.Operator assignmentOperator = Assignment.Operator.ASSIGN;
205
206     /**
207      * The left hand side; lazily initialized; defaults to an unspecified,
208      * but legal, simple name.
209      */

210     private Expression leftHandSide = null;
211
212     /**
213      * The right hand side; lazily initialized; defaults to an unspecified,
214      * but legal, simple name.
215      */

216     private Expression rightHandSide = null;
217
218     /**
219      * Creates a new AST node for an assignment expression owned by the given
220      * AST. By default, the node has an assignment operator, and unspecified
221      * left and right hand sides.
222      *
223      * @param ast the AST that is to own this node
224      */

225     Assignment(AST ast) {
226         super(ast);
227     }
228
229     /* (omit javadoc for this method)
230      * Method declared on ASTNode.
231      */

232     final List JavaDoc internalStructuralPropertiesForType(int apiLevel) {
233         return propertyDescriptors(apiLevel);
234     }
235     
236     /* (omit javadoc for this method)
237      * Method declared on ASTNode.
238      */

239     final Object JavaDoc internalGetSetObjectProperty(SimplePropertyDescriptor property, boolean get, Object JavaDoc value) {
240         if (property == OPERATOR_PROPERTY) {
241             if (get) {
242                 return getOperator();
243             } else {
244                 setOperator((Operator) value);
245                 return null;
246             }
247         }
248         // allow default implementation to flag the error
249
return super.internalGetSetObjectProperty(property, get, value);
250     }
251
252     /* (omit javadoc for this method)
253      * Method declared on ASTNode.
254      */

255     final ASTNode internalGetSetChildProperty(ChildPropertyDescriptor property, boolean get, ASTNode child) {
256         if (property == LEFT_HAND_SIDE_PROPERTY) {
257             if (get) {
258                 return getLeftHandSide();
259             } else {
260                 setLeftHandSide((Expression) child);
261                 return null;
262             }
263         }
264         if (property == RIGHT_HAND_SIDE_PROPERTY) {
265             if (get) {
266                 return getRightHandSide();
267             } else {
268                 setRightHandSide((Expression) child);
269                 return null;
270             }
271         }
272         // allow default implementation to flag the error
273
return super.internalGetSetChildProperty(property, get, child);
274     }
275
276     /* (omit javadoc for this method)
277      * Method declared on ASTNode.
278      */

279     final int getNodeType0() {
280         return ASSIGNMENT;
281     }
282
283     /* (omit javadoc for this method)
284      * Method declared on ASTNode.
285      */

286     ASTNode clone0(AST target) {
287         Assignment result = new Assignment(target);
288         result.setSourceRange(this.getStartPosition(), this.getLength());
289         result.setOperator(getOperator());
290         result.setLeftHandSide((Expression) getLeftHandSide().clone(target));
291         result.setRightHandSide((Expression) getRightHandSide().clone(target));
292         return result;
293     }
294
295     /* (omit javadoc for this method)
296      * Method declared on ASTNode.
297      */

298     final boolean subtreeMatch0(ASTMatcher matcher, Object JavaDoc other) {
299         // dispatch to correct overloaded match method
300
return matcher.match(this, other);
301     }
302
303     /* (omit javadoc for this method)
304      * Method declared on ASTNode.
305      */

306     void accept0(ASTVisitor visitor) {
307         boolean visitChildren = visitor.visit(this);
308         if (visitChildren) {
309             // visit children in normal left to right reading order
310
acceptChild(visitor, getLeftHandSide());
311             acceptChild(visitor, getRightHandSide());
312         }
313         visitor.endVisit(this);
314     }
315     
316     /**
317      * Returns the operator of this assignment expression.
318      *
319      * @return the assignment operator
320      */

321     public Assignment.Operator getOperator() {
322         return this.assignmentOperator;
323     }
324
325     /**
326      * Sets the operator of this assignment expression.
327      *
328      * @param assignmentOperator the assignment operator
329      * @exception IllegalArgumentException if the argument is incorrect
330      */

331     public void setOperator(Assignment.Operator assignmentOperator) {
332         if (assignmentOperator == null) {
333             throw new IllegalArgumentException JavaDoc();
334         }
335         preValueChange(OPERATOR_PROPERTY);
336         this.assignmentOperator = assignmentOperator;
337         postValueChange(OPERATOR_PROPERTY);
338     }
339
340     /**
341      * Returns the left hand side of this assignment expression.
342      *
343      * @return the left hand side node
344      */

345     public Expression getLeftHandSide() {
346         if (this.leftHandSide == null) {
347             // lazy init must be thread-safe for readers
348
synchronized (this) {
349                 if (this.leftHandSide == null) {
350                     preLazyInit();
351                     this.leftHandSide= new SimpleName(this.ast);
352                     postLazyInit(this.leftHandSide, LEFT_HAND_SIDE_PROPERTY);
353                 }
354             }
355         }
356         return this.leftHandSide;
357     }
358         
359     /**
360      * Sets the left hand side of this assignment expression.
361      *
362      * @param expression the left hand side node
363      * @exception IllegalArgumentException if:
364      * <ul>
365      * <li>the node belongs to a different AST</li>
366      * <li>the node already has a parent</li>
367      * <li>a cycle in would be created</li>
368      * </ul>
369      */

370     public void setLeftHandSide(Expression expression) {
371         if (expression == null) {
372             throw new IllegalArgumentException JavaDoc();
373         }
374         // an Assignment may occur inside a Expression - must check cycles
375
ASTNode oldChild = this.leftHandSide;
376         preReplaceChild(oldChild, expression, LEFT_HAND_SIDE_PROPERTY);
377         this.leftHandSide = expression;
378         postReplaceChild(oldChild, expression, LEFT_HAND_SIDE_PROPERTY);
379     }
380
381     /**
382      * Returns the right hand side of this assignment expression.
383      *
384      * @return the right hand side node
385      */

386     public Expression getRightHandSide() {
387         if (this.rightHandSide == null) {
388             // lazy init must be thread-safe for readers
389
synchronized (this) {
390                 if (this.rightHandSide == null) {
391                     preLazyInit();
392                     this.rightHandSide= new SimpleName(this.ast);
393                     postLazyInit(this.rightHandSide, RIGHT_HAND_SIDE_PROPERTY);
394                 }
395             }
396         }
397         return this.rightHandSide;
398     }
399         
400     /**
401      * Sets the right hand side of this assignment expression.
402      *
403      * @param expression the right hand side node
404      * @exception IllegalArgumentException if:
405      * <ul>
406      * <li>the node belongs to a different AST</li>
407      * <li>the node already has a parent</li>
408      * <li>a cycle in would be created</li>
409      * </ul>
410      */

411     public void setRightHandSide(Expression expression) {
412         if (expression == null) {
413             throw new IllegalArgumentException JavaDoc();
414         }
415         // an Assignment may occur inside a Expression - must check cycles
416
ASTNode oldChild = this.rightHandSide;
417         preReplaceChild(oldChild, expression, RIGHT_HAND_SIDE_PROPERTY);
418         this.rightHandSide = expression;
419         postReplaceChild(oldChild, expression, RIGHT_HAND_SIDE_PROPERTY);
420     }
421
422     /* (omit javadoc for this method)
423      * Method declared on ASTNode.
424      */

425     int memSize() {
426         // treat Code as free
427
return BASE_NODE_SIZE + 3 * 4;
428     }
429     
430     /* (omit javadoc for this method)
431      * Method declared on ASTNode.
432      */

433     int treeSize() {
434         return
435             memSize()
436             + (this.leftHandSide == null ? 0 : getLeftHandSide().treeSize())
437             + (this.rightHandSide == null ? 0 : getRightHandSide().treeSize());
438     }
439 }
440
441
Popular Tags