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.List;
014:
015: /**
016: * Synchronized statement AST node type.
017: *
018: * <pre>
019: * SynchronizedStatement:
020: * <b>synchronized</b> <b>(</b> Expression <b>)</b> Block
021: * </pre>
022: *
023: * @since 2.0
024: */
025: public class SynchronizedStatement extends Statement {
026:
027: /**
028: * The "expression" structural property of this node type.
029: * @since 3.0
030: */
031: public static final ChildPropertyDescriptor EXPRESSION_PROPERTY = new ChildPropertyDescriptor(
032: SynchronizedStatement.class,
033: "expression", Expression.class, MANDATORY, CYCLE_RISK); //$NON-NLS-1$
034:
035: /**
036: * The "body" structural property of this node type.
037: * @since 3.0
038: */
039: public static final ChildPropertyDescriptor BODY_PROPERTY = new ChildPropertyDescriptor(
040: SynchronizedStatement.class,
041: "body", Block.class, MANDATORY, CYCLE_RISK); //$NON-NLS-1$
042:
043: /**
044: * A list of property descriptors (element type:
045: * {@link StructuralPropertyDescriptor}),
046: * or null if uninitialized.
047: */
048: private static final List PROPERTY_DESCRIPTORS;
049:
050: static {
051: List propertyList = new ArrayList(3);
052: createPropertyList(SynchronizedStatement.class, propertyList);
053: addProperty(EXPRESSION_PROPERTY, propertyList);
054: addProperty(BODY_PROPERTY, propertyList);
055: PROPERTY_DESCRIPTORS = reapPropertyList(propertyList);
056: }
057:
058: /**
059: * Returns a list of structural property descriptors for this node type.
060: * Clients must not modify the result.
061: *
062: * @param apiLevel the API level; one of the
063: * <code>AST.JLS*</code> constants
064:
065: * @return a list of property descriptors (element type:
066: * {@link StructuralPropertyDescriptor})
067: * @since 3.0
068: */
069: public static List propertyDescriptors(int apiLevel) {
070: return PROPERTY_DESCRIPTORS;
071: }
072:
073: /**
074: * The expression; lazily initialized; defaults to an unspecified, but
075: * legal, expression.
076: */
077: private Expression expression = null;
078:
079: /**
080: * The body; lazily initialized; defaults to an empty block.
081: */
082: private Block body = null;
083:
084: /**
085: * Creates a new unparented synchronized statement node owned by the given
086: * AST. By default, the expression is unspecified, but legal, and the
087: * blody is an empty block.
088: * <p>
089: * N.B. This constructor is package-private.
090: * </p>
091: *
092: * @param ast the AST that is to own this node
093: */
094: SynchronizedStatement(AST ast) {
095: super (ast);
096: }
097:
098: /* (omit javadoc for this method)
099: * Method declared on ASTNode.
100: */
101: final List internalStructuralPropertiesForType(int apiLevel) {
102: return propertyDescriptors(apiLevel);
103: }
104:
105: /* (omit javadoc for this method)
106: * Method declared on ASTNode.
107: */
108: final ASTNode internalGetSetChildProperty(
109: ChildPropertyDescriptor property, boolean get, ASTNode child) {
110: if (property == EXPRESSION_PROPERTY) {
111: if (get) {
112: return getExpression();
113: } else {
114: setExpression((Expression) child);
115: return null;
116: }
117: }
118: if (property == BODY_PROPERTY) {
119: if (get) {
120: return getBody();
121: } else {
122: setBody((Block) child);
123: return null;
124: }
125: }
126: // allow default implementation to flag the error
127: return super .internalGetSetChildProperty(property, get, child);
128: }
129:
130: /* (omit javadoc for this method)
131: * Method declared on ASTNode.
132: */
133: final int getNodeType0() {
134: return SYNCHRONIZED_STATEMENT;
135: }
136:
137: /* (omit javadoc for this method)
138: * Method declared on ASTNode.
139: */
140: ASTNode clone0(AST target) {
141: SynchronizedStatement result = new SynchronizedStatement(target);
142: result
143: .setSourceRange(this .getStartPosition(), this
144: .getLength());
145: result.copyLeadingComment(this );
146: result
147: .setExpression((Expression) getExpression().clone(
148: target));
149: result.setBody((Block) getBody().clone(target));
150: return result;
151: }
152:
153: /* (omit javadoc for this method)
154: * Method declared on ASTNode.
155: */
156: final boolean subtreeMatch0(ASTMatcher matcher, Object other) {
157: // dispatch to correct overloaded match method
158: return matcher.match(this , other);
159: }
160:
161: /* (omit javadoc for this method)
162: * Method declared on ASTNode.
163: */
164: void accept0(ASTVisitor visitor) {
165: boolean visitChildren = visitor.visit(this );
166: if (visitChildren) {
167: // visit children in normal left to right reading order
168: acceptChild(visitor, getExpression());
169: acceptChild(visitor, getBody());
170: }
171: visitor.endVisit(this );
172: }
173:
174: /**
175: * Returns the expression of this synchronized statement.
176: *
177: * @return the expression node
178: */
179: public Expression getExpression() {
180: if (this .expression == null) {
181: // lazy init must be thread-safe for readers
182: synchronized (this ) {
183: if (this .expression == null) {
184: preLazyInit();
185: this .expression = new SimpleName(this .ast);
186: postLazyInit(this .expression, EXPRESSION_PROPERTY);
187: }
188: }
189: }
190: return this .expression;
191: }
192:
193: /**
194: * Sets the expression of this synchronized statement.
195: *
196: * @param expression the expression node
197: * @exception IllegalArgumentException if:
198: * <ul>
199: * <li>the node belongs to a different AST</li>
200: * <li>the node already has a parent</li>
201: * <li>a cycle in would be created</li>
202: * </ul>
203: */
204: public void setExpression(Expression expression) {
205: if (expression == null) {
206: throw new IllegalArgumentException();
207: }
208: ASTNode oldChild = this .expression;
209: preReplaceChild(oldChild, expression, EXPRESSION_PROPERTY);
210: this .expression = expression;
211: postReplaceChild(oldChild, expression, EXPRESSION_PROPERTY);
212: }
213:
214: /**
215: * Returns the body of this synchronized statement.
216: *
217: * @return the body block node
218: */
219: public Block getBody() {
220: if (this .body == null) {
221: // lazy init must be thread-safe for readers
222: synchronized (this ) {
223: if (this .body == null) {
224: preLazyInit();
225: this .body = new Block(this .ast);
226: postLazyInit(this .body, BODY_PROPERTY);
227: }
228: }
229: }
230: return this .body;
231: }
232:
233: /**
234: * Sets the body of this synchronized statement.
235: *
236: * @param block the body statement node
237: * @exception IllegalArgumentException if:
238: * <ul>
239: * <li>the node belongs to a different AST</li>
240: * <li>the node already has a parent</li>
241: * <li>a cycle in would be created</li>
242: * </ul>
243: */
244: public void setBody(Block block) {
245: if (block == null) {
246: throw new IllegalArgumentException();
247: }
248: ASTNode oldChild = this .body;
249: preReplaceChild(oldChild, block, BODY_PROPERTY);
250: this .body = block;
251: postReplaceChild(oldChild, block, BODY_PROPERTY);
252: }
253:
254: /* (omit javadoc for this method)
255: * Method declared on ASTNode.
256: */
257: int memSize() {
258: return super .memSize() + 2 * 4;
259: }
260:
261: /* (omit javadoc for this method)
262: * Method declared on ASTNode.
263: */
264: int treeSize() {
265: return memSize()
266: + (this .expression == null ? 0 : getExpression()
267: .treeSize())
268: + (this .body == null ? 0 : getBody().treeSize());
269: }
270: }
|