001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.jdt.core.dom;
011:
012: import java.util.ArrayList;
013: import java.util.HashMap;
014: import java.util.List;
015: import java.util.Map;
016:
017: /**
018: * Assignment expression AST node type.
019: *
020: * <pre>
021: * Assignment:
022: * Expression AssignmentOperator Expression
023: * </pre>
024: *
025: * @since 2.0
026: */
027: public class Assignment extends Expression {
028:
029: /**
030: * Assignment operators (typesafe enumeration).
031: * <pre>
032: * AssignmentOperator:<code>
033: * <b>=</b> ASSIGN
034: * <b>+=</b> PLUS_ASSIGN
035: * <b>-=</b> MINUS_ASSIGN
036: * <b>*=</b> TIMES_ASSIGN
037: * <b>/=</b> DIVIDE_ASSIGN
038: * <b>&=</b> BIT_AND_ASSIGN
039: * <b>|=</b> BIT_OR_ASSIGN
040: * <b>^=</b> BIT_XOR_ASSIGN
041: * <b>%=</b> REMAINDER_ASSIGN
042: * <b><<=</b> LEFT_SHIFT_ASSIGN
043: * <b>>>=</b> RIGHT_SHIFT_SIGNED_ASSIGN
044: * <b>>>>=</b> RIGHT_SHIFT_UNSIGNED_ASSIGN</code>
045: * </pre>
046: */
047: public static class Operator {
048:
049: /**
050: * The name of the operator
051: */
052: private String op;
053:
054: /**
055: * Creates a new assignment operator with the given name.
056: * <p>
057: * Note: this constructor is private. The only instances
058: * ever created are the ones for the standard operators.
059: * </p>
060: *
061: * @param op the character sequence for the operator
062: */
063: private Operator(String op) {
064: this .op = op;
065: }
066:
067: /**
068: * Returns the character sequence for the operator.
069: *
070: * @return the character sequence for the operator
071: */
072: public String toString() {
073: return op;
074: }
075:
076: /** = operator. */
077: public static final Operator ASSIGN = new Operator("=");//$NON-NLS-1$
078: /** += operator. */
079: public static final Operator PLUS_ASSIGN = new Operator("+=");//$NON-NLS-1$
080: /** -= operator. */
081: public static final Operator MINUS_ASSIGN = new Operator("-=");//$NON-NLS-1$
082: /** *= operator. */
083: public static final Operator TIMES_ASSIGN = new Operator("*=");//$NON-NLS-1$
084: /** /= operator. */
085: public static final Operator DIVIDE_ASSIGN = new Operator("/=");//$NON-NLS-1$
086: /** &= operator. */
087: public static final Operator BIT_AND_ASSIGN = new Operator("&=");//$NON-NLS-1$
088: /** |= operator. */
089: public static final Operator BIT_OR_ASSIGN = new Operator("|=");//$NON-NLS-1$
090: /** ^= operator. */
091: public static final Operator BIT_XOR_ASSIGN = new Operator("^=");//$NON-NLS-1$
092: /** %= operator. */
093: public static final Operator REMAINDER_ASSIGN = new Operator(
094: "%=");//$NON-NLS-1$
095: /** <<== operator. */
096: public static final Operator LEFT_SHIFT_ASSIGN = new Operator(
097: "<<=");//$NON-NLS-1$
098: /** >>= operator. */
099: public static final Operator RIGHT_SHIFT_SIGNED_ASSIGN = new Operator(
100: ">>=");//$NON-NLS-1$
101: /** >>>= operator. */
102: public static final Operator RIGHT_SHIFT_UNSIGNED_ASSIGN = new Operator(
103: ">>>=");//$NON-NLS-1$
104:
105: /**
106: * Returns the assignment operator corresponding to the given string,
107: * or <code>null</code> if none.
108: * <p>
109: * <code>toOperator</code> is the converse of <code>toString</code>:
110: * that is, <code>Operator.toOperator(op.toString()) == op</code> for all
111: * operators <code>op</code>.
112: * </p>
113: *
114: * @param token the character sequence for the operator
115: * @return the assignment operator, or <code>null</code> if none
116: */
117: public static Operator toOperator(String token) {
118: return (Operator) CODES.get(token);
119: }
120:
121: /**
122: * Map from token to operator (key type: <code>String</code>;
123: * value type: <code>Operator</code>).
124: */
125: private static final Map CODES;
126: static {
127: CODES = new HashMap(20);
128: Operator[] ops = { ASSIGN, PLUS_ASSIGN, MINUS_ASSIGN,
129: TIMES_ASSIGN, DIVIDE_ASSIGN, BIT_AND_ASSIGN,
130: BIT_OR_ASSIGN, BIT_XOR_ASSIGN, REMAINDER_ASSIGN,
131: LEFT_SHIFT_ASSIGN, RIGHT_SHIFT_SIGNED_ASSIGN,
132: RIGHT_SHIFT_UNSIGNED_ASSIGN };
133: for (int i = 0; i < ops.length; i++) {
134: CODES.put(ops[i].toString(), ops[i]);
135: }
136: }
137: }
138:
139: /**
140: * The "leftHandSide" structural property of this node type.
141: * @since 3.0
142: */
143: public static final ChildPropertyDescriptor LEFT_HAND_SIDE_PROPERTY = new ChildPropertyDescriptor(
144: Assignment.class,
145: "leftHandSide", Expression.class, MANDATORY, CYCLE_RISK); //$NON-NLS-1$
146:
147: /**
148: * The "operator" structural property of this node type.
149: * @since 3.0
150: */
151: public static final SimplePropertyDescriptor OPERATOR_PROPERTY = new SimplePropertyDescriptor(
152: Assignment.class,
153: "operator", Assignment.Operator.class, MANDATORY); //$NON-NLS-1$
154:
155: /**
156: * The "rightHandSide" structural property of this node type.
157: * @since 3.0
158: */
159: public static final ChildPropertyDescriptor RIGHT_HAND_SIDE_PROPERTY = new ChildPropertyDescriptor(
160: Assignment.class,
161: "rightHandSide", Expression.class, MANDATORY, CYCLE_RISK); //$NON-NLS-1$
162:
163: /**
164: * A list of property descriptors (element type:
165: * {@link StructuralPropertyDescriptor}),
166: * or null if uninitialized.
167: */
168: private static final List PROPERTY_DESCRIPTORS;
169:
170: static {
171: List properyList = new ArrayList(4);
172: createPropertyList(Assignment.class, properyList);
173: addProperty(LEFT_HAND_SIDE_PROPERTY, properyList);
174: addProperty(OPERATOR_PROPERTY, properyList);
175: addProperty(RIGHT_HAND_SIDE_PROPERTY, properyList);
176: PROPERTY_DESCRIPTORS = reapPropertyList(properyList);
177: }
178:
179: /**
180: * Returns a list of structural property descriptors for this node type.
181: * Clients must not modify the result.
182: *
183: * @param apiLevel the API level; one of the
184: * <code>AST.JLS*</code> constants
185:
186: * @return a list of property descriptors (element type:
187: * {@link StructuralPropertyDescriptor})
188: * @since 3.0
189: */
190: public static List propertyDescriptors(int apiLevel) {
191: return PROPERTY_DESCRIPTORS;
192: }
193:
194: /**
195: * The assignment operator; defaults to Assignment.Operator.ASSIGN
196: */
197: private Assignment.Operator assignmentOperator = Assignment.Operator.ASSIGN;
198:
199: /**
200: * The left hand side; lazily initialized; defaults to an unspecified,
201: * but legal, simple name.
202: */
203: private Expression leftHandSide = null;
204:
205: /**
206: * The right hand side; lazily initialized; defaults to an unspecified,
207: * but legal, simple name.
208: */
209: private Expression rightHandSide = null;
210:
211: /**
212: * Creates a new AST node for an assignment expression owned by the given
213: * AST. By default, the node has an assignment operator, and unspecified
214: * left and right hand sides.
215: *
216: * @param ast the AST that is to own this node
217: */
218: Assignment(AST ast) {
219: super (ast);
220: }
221:
222: /* (omit javadoc for this method)
223: * Method declared on ASTNode.
224: */
225: final List internalStructuralPropertiesForType(int apiLevel) {
226: return propertyDescriptors(apiLevel);
227: }
228:
229: /* (omit javadoc for this method)
230: * Method declared on ASTNode.
231: */
232: final Object internalGetSetObjectProperty(
233: SimplePropertyDescriptor property, boolean get, Object value) {
234: if (property == OPERATOR_PROPERTY) {
235: if (get) {
236: return getOperator();
237: } else {
238: setOperator((Operator) value);
239: return null;
240: }
241: }
242: // allow default implementation to flag the error
243: return super .internalGetSetObjectProperty(property, get, value);
244: }
245:
246: /* (omit javadoc for this method)
247: * Method declared on ASTNode.
248: */
249: final ASTNode internalGetSetChildProperty(
250: ChildPropertyDescriptor property, boolean get, ASTNode child) {
251: if (property == LEFT_HAND_SIDE_PROPERTY) {
252: if (get) {
253: return getLeftHandSide();
254: } else {
255: setLeftHandSide((Expression) child);
256: return null;
257: }
258: }
259: if (property == RIGHT_HAND_SIDE_PROPERTY) {
260: if (get) {
261: return getRightHandSide();
262: } else {
263: setRightHandSide((Expression) child);
264: return null;
265: }
266: }
267: // allow default implementation to flag the error
268: return super .internalGetSetChildProperty(property, get, child);
269: }
270:
271: /* (omit javadoc for this method)
272: * Method declared on ASTNode.
273: */
274: final int getNodeType0() {
275: return ASSIGNMENT;
276: }
277:
278: /* (omit javadoc for this method)
279: * Method declared on ASTNode.
280: */
281: ASTNode clone0(AST target) {
282: Assignment result = new Assignment(target);
283: result
284: .setSourceRange(this .getStartPosition(), this
285: .getLength());
286: result.setOperator(getOperator());
287: result.setLeftHandSide((Expression) getLeftHandSide().clone(
288: target));
289: result.setRightHandSide((Expression) getRightHandSide().clone(
290: target));
291: return result;
292: }
293:
294: /* (omit javadoc for this method)
295: * Method declared on ASTNode.
296: */
297: final boolean subtreeMatch0(ASTMatcher matcher, Object other) {
298: // dispatch to correct overloaded match method
299: return matcher.match(this , other);
300: }
301:
302: /* (omit javadoc for this method)
303: * Method declared on ASTNode.
304: */
305: void accept0(ASTVisitor visitor) {
306: boolean visitChildren = visitor.visit(this );
307: if (visitChildren) {
308: // visit children in normal left to right reading order
309: acceptChild(visitor, getLeftHandSide());
310: acceptChild(visitor, getRightHandSide());
311: }
312: visitor.endVisit(this );
313: }
314:
315: /**
316: * Returns the operator of this assignment expression.
317: *
318: * @return the assignment operator
319: */
320: public Assignment.Operator getOperator() {
321: return this .assignmentOperator;
322: }
323:
324: /**
325: * Sets the operator of this assignment expression.
326: *
327: * @param assignmentOperator the assignment operator
328: * @exception IllegalArgumentException if the argument is incorrect
329: */
330: public void setOperator(Assignment.Operator assignmentOperator) {
331: if (assignmentOperator == null) {
332: throw new IllegalArgumentException();
333: }
334: preValueChange(OPERATOR_PROPERTY);
335: this .assignmentOperator = assignmentOperator;
336: postValueChange(OPERATOR_PROPERTY);
337: }
338:
339: /**
340: * Returns the left hand side of this assignment expression.
341: *
342: * @return the left hand side node
343: */
344: public Expression getLeftHandSide() {
345: if (this .leftHandSide == null) {
346: // lazy init must be thread-safe for readers
347: synchronized (this ) {
348: if (this .leftHandSide == null) {
349: preLazyInit();
350: this .leftHandSide = new SimpleName(this .ast);
351: postLazyInit(this .leftHandSide,
352: 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();
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,
394: RIGHT_HAND_SIDE_PROPERTY);
395: }
396: }
397: }
398: return this .rightHandSide;
399: }
400:
401: /**
402: * Sets the right hand side of this assignment expression.
403: *
404: * @param expression the right hand side node
405: * @exception IllegalArgumentException if:
406: * <ul>
407: * <li>the node belongs to a different AST</li>
408: * <li>the node already has a parent</li>
409: * <li>a cycle in would be created</li>
410: * </ul>
411: */
412: public void setRightHandSide(Expression expression) {
413: if (expression == null) {
414: throw new IllegalArgumentException();
415: }
416: // an Assignment may occur inside a Expression - must check cycles
417: ASTNode oldChild = this .rightHandSide;
418: preReplaceChild(oldChild, expression, RIGHT_HAND_SIDE_PROPERTY);
419: this .rightHandSide = expression;
420: postReplaceChild(oldChild, expression, RIGHT_HAND_SIDE_PROPERTY);
421: }
422:
423: /* (omit javadoc for this method)
424: * Method declared on ASTNode.
425: */
426: int memSize() {
427: // treat Code as free
428: return BASE_NODE_SIZE + 3 * 4;
429: }
430:
431: /* (omit javadoc for this method)
432: * Method declared on ASTNode.
433: */
434: int treeSize() {
435: return memSize()
436: + (this .leftHandSide == null ? 0 : getLeftHandSide()
437: .treeSize())
438: + (this .rightHandSide == null ? 0 : getRightHandSide()
439: .treeSize());
440: }
441: }
|