KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 2004 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.List JavaDoc;
16
17 /**
18  * Synchronized statement AST node type.
19  *
20  * <pre>
21  * SynchronizedStatement:
22  * <b>synchronized</b> <b>(</b> Expression <b>)</b> Block
23  * </pre>
24  *
25  * @since 2.0
26  */

27 public class SynchronizedStatement extends Statement {
28
29     /**
30      * The "expression" structural property of this node type.
31      * @since 3.0
32      */

33     public static final ChildPropertyDescriptor EXPRESSION_PROPERTY =
34         new ChildPropertyDescriptor(SynchronizedStatement.class, "expression", Expression.class, MANDATORY, CYCLE_RISK); //$NON-NLS-1$
35

36     /**
37      * The "body" structural property of this node type.
38      * @since 3.0
39      */

40     public static final ChildPropertyDescriptor BODY_PROPERTY =
41         new ChildPropertyDescriptor(SynchronizedStatement.class, "body", Block.class, MANDATORY, CYCLE_RISK); //$NON-NLS-1$
42

43     /**
44      * A list of property descriptors (element type:
45      * {@link StructuralPropertyDescriptor}),
46      * or null if uninitialized.
47      */

48     private static final List JavaDoc PROPERTY_DESCRIPTORS;
49     
50     static {
51         List JavaDoc propertyList = new ArrayList JavaDoc(3);
52         createPropertyList(SynchronizedStatement.class, propertyList);
53         addProperty(EXPRESSION_PROPERTY, propertyList);
54         addProperty(BODY_PROPERTY, propertyList);
55         PROPERTY_DESCRIPTORS = reapPropertyList(propertyList);
56     }
57
58     /**
59      * Returns a list of structural property descriptors for this node type.
60      * Clients must not modify the result.
61      *
62      * @param apiLevel the API level; one of the
63      * <code>AST.JLS&ast;</code> constants
64
65      * @return a list of property descriptors (element type:
66      * {@link StructuralPropertyDescriptor})
67      * @since 3.0
68      */

69     public static List JavaDoc propertyDescriptors(int apiLevel) {
70         return PROPERTY_DESCRIPTORS;
71     }
72             
73     /**
74      * The expression; lazily initialized; defaults to an unspecified, but
75      * legal, expression.
76      */

77     private Expression expression = null;
78
79     /**
80      * The body; lazily initialized; defaults to an empty block.
81      */

82     private Block body = null;
83
84     /**
85      * Creates a new unparented synchronized statement node owned by the given
86      * AST. By default, the expression is unspecified, but legal, and the
87      * blody is an empty block.
88      * <p>
89      * N.B. This constructor is package-private.
90      * </p>
91      *
92      * @param ast the AST that is to own this node
93      */

94     SynchronizedStatement(AST ast) {
95         super(ast);
96     }
97
98     /* (omit javadoc for this method)
99      * Method declared on ASTNode.
100      */

101     final List JavaDoc 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(ChildPropertyDescriptor property, boolean get, ASTNode child) {
109         if (property == EXPRESSION_PROPERTY) {
110             if (get) {
111                 return getExpression();
112             } else {
113                 setExpression((Expression) child);
114                 return null;
115             }
116         }
117         if (property == BODY_PROPERTY) {
118             if (get) {
119                 return getBody();
120             } else {
121                 setBody((Block) child);
122                 return null;
123             }
124         }
125         // allow default implementation to flag the error
126
return super.internalGetSetChildProperty(property, get, child);
127     }
128     
129     /* (omit javadoc for this method)
130      * Method declared on ASTNode.
131      */

132     final int getNodeType0() {
133         return SYNCHRONIZED_STATEMENT;
134     }
135
136     /* (omit javadoc for this method)
137      * Method declared on ASTNode.
138      */

139     ASTNode clone0(AST target) {
140         SynchronizedStatement result = new SynchronizedStatement(target);
141         result.setSourceRange(this.getStartPosition(), this.getLength());
142         result.copyLeadingComment(this);
143         result.setExpression((Expression) getExpression().clone(target));
144         result.setBody((Block) getBody().clone(target));
145         return result;
146     }
147
148     /* (omit javadoc for this method)
149      * Method declared on ASTNode.
150      */

151     final boolean subtreeMatch0(ASTMatcher matcher, Object JavaDoc other) {
152         // dispatch to correct overloaded match method
153
return matcher.match(this, other);
154     }
155
156     /* (omit javadoc for this method)
157      * Method declared on ASTNode.
158      */

159     void accept0(ASTVisitor visitor) {
160         boolean visitChildren = visitor.visit(this);
161         if (visitChildren) {
162             // visit children in normal left to right reading order
163
acceptChild(visitor, getExpression());
164             acceptChild(visitor, getBody());
165         }
166         visitor.endVisit(this);
167     }
168     
169     /**
170      * Returns the expression of this synchronized statement.
171      *
172      * @return the expression node
173      */

174     public Expression getExpression() {
175         if (this.expression == null) {
176             // lazy init must be thread-safe for readers
177
synchronized (this) {
178                 if (this.expression == null) {
179                     preLazyInit();
180                     this.expression = new SimpleName(this.ast);
181                     postLazyInit(this.expression, EXPRESSION_PROPERTY);
182                 }
183             }
184         }
185         return this.expression;
186     }
187     
188     /**
189      * Sets the expression of this synchronized statement.
190      *
191      * @param expression the expression node
192      * @exception IllegalArgumentException if:
193      * <ul>
194      * <li>the node belongs to a different AST</li>
195      * <li>the node already has a parent</li>
196      * <li>a cycle in would be created</li>
197      * </ul>
198      */

199     public void setExpression(Expression expression) {
200         if (expression == null) {
201             throw new IllegalArgumentException JavaDoc();
202         }
203         ASTNode oldChild = this.expression;
204         preReplaceChild(oldChild, expression, EXPRESSION_PROPERTY);
205         this.expression = expression;
206         postReplaceChild(oldChild, expression, EXPRESSION_PROPERTY);
207     }
208
209     /**
210      * Returns the body of this synchronized statement.
211      *
212      * @return the body block node
213      */

214     public Block getBody() {
215         if (this.body == null) {
216             // lazy init must be thread-safe for readers
217
synchronized (this) {
218                 if (this.body == null) {
219                     preLazyInit();
220                     this.body = new Block(this.ast);
221                     postLazyInit(this.body, BODY_PROPERTY);
222                 }
223             }
224         }
225         return this.body;
226     }
227     
228     /**
229      * Sets the body of this synchronized statement.
230      *
231      * @param block the body statement node
232      * @exception IllegalArgumentException if:
233      * <ul>
234      * <li>the node belongs to a different AST</li>
235      * <li>the node already has a parent</li>
236      * <li>a cycle in would be created</li>
237      * </ul>
238      */

239     public void setBody(Block block) {
240         if (block == null) {
241             throw new IllegalArgumentException JavaDoc();
242         }
243         ASTNode oldChild = this.body;
244         preReplaceChild(oldChild, block, BODY_PROPERTY);
245         this.body = block;
246         postReplaceChild(oldChild, block, BODY_PROPERTY);
247     }
248     
249     /* (omit javadoc for this method)
250      * Method declared on ASTNode.
251      */

252     int memSize() {
253         return super.memSize() + 2 * 4;
254     }
255     
256     /* (omit javadoc for this method)
257      * Method declared on ASTNode.
258      */

259     int treeSize() {
260         return
261             memSize()
262             + (this.expression == null ? 0 : getExpression().treeSize())
263             + (this.body == null ? 0 : getBody().treeSize());
264     }
265 }
266
Popular Tags