KickJava   Java API By Example, From Geeks To Geeks.

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


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.List JavaDoc;
16
17 /**
18  * For statement AST node type.
19  *
20  * <pre>
21  * ForStatement:
22  * <b>for</b> <b>(</b>
23  * [ ForInit ]<b>;</b>
24  * [ Expression ] <b>;</b>
25  * [ ForUpdate ] <b>)</b>
26  * Statement
27  * ForInit:
28  * Expression { <b>,</b> Expression }
29  * ForUpdate:
30  * Expression { <b>,</b> Expression }
31  * </pre>
32  * <p>
33  * Note: When variables are declared in the initializer
34  * of a for statement such as "<code>for (int a=1, b=2;;);</code>",
35  * they should be represented as a single
36  * <code>VariableDeclarationExpression</code>
37  * with two fragments, rather than being split up into a pair
38  * of expressions.
39  * </p>
40  *
41  * @since 2.0
42  */

43 public class ForStatement extends Statement {
44     
45     /**
46      * The "initializers" structural property of this node type.
47      * @since 3.0
48      */

49     public static final ChildListPropertyDescriptor INITIALIZERS_PROPERTY =
50         new ChildListPropertyDescriptor(ForStatement.class, "initializers", Expression.class, CYCLE_RISK); //$NON-NLS-1$
51

52     /**
53      * The "expression" structural property of this node type.
54      * @since 3.0
55      */

56     public static final ChildPropertyDescriptor EXPRESSION_PROPERTY =
57         new ChildPropertyDescriptor(ForStatement.class, "expression", Expression.class, OPTIONAL, CYCLE_RISK); //$NON-NLS-1$
58

59     /**
60      * The "updaters" structural property of this node type.
61      * @since 3.0
62      */

63     public static final ChildListPropertyDescriptor UPDATERS_PROPERTY =
64         new ChildListPropertyDescriptor(ForStatement.class, "updaters", Expression.class, CYCLE_RISK); //$NON-NLS-1$
65

66     /**
67      * The "body" structural property of this node type.
68      * @since 3.0
69      */

70     public static final ChildPropertyDescriptor BODY_PROPERTY =
71         new ChildPropertyDescriptor(ForStatement.class, "body", Statement.class, MANDATORY, CYCLE_RISK); //$NON-NLS-1$
72

73     /**
74      * A list of property descriptors (element type:
75      * {@link StructuralPropertyDescriptor}),
76      * or null if uninitialized.
77      */

78     private static final List JavaDoc PROPERTY_DESCRIPTORS;
79     
80     static {
81         List JavaDoc properyList = new ArrayList JavaDoc(5);
82         createPropertyList(ForStatement.class, properyList);
83         addProperty(INITIALIZERS_PROPERTY, properyList);
84         addProperty(EXPRESSION_PROPERTY, properyList);
85         addProperty(UPDATERS_PROPERTY, properyList);
86         addProperty(BODY_PROPERTY, properyList);
87         PROPERTY_DESCRIPTORS = reapPropertyList(properyList);
88     }
89
90     /**
91      * Returns a list of structural property descriptors for this node type.
92      * Clients must not modify the result.
93      *
94      * @param apiLevel the API level; one of the
95      * <code>AST.JLS*</code> constants
96
97      * @return a list of property descriptors (element type:
98      * {@link StructuralPropertyDescriptor})
99      * @since 3.0
100      */

101     public static List JavaDoc propertyDescriptors(int apiLevel) {
102         return PROPERTY_DESCRIPTORS;
103     }
104             
105     /**
106      * The list of initializer expressions (element type:
107      * <code>Expression</code>). Defaults to an empty list.
108      */

109     private ASTNode.NodeList initializers =
110         new ASTNode.NodeList(INITIALIZERS_PROPERTY);
111
112     /**
113      * The condition expression; <code>null</code> for none; defaults to none.
114      */

115     private Expression optionalConditionExpression = null;
116     
117     /**
118      * The list of update expressions (element type:
119      * <code>Expression</code>). Defaults to an empty list.
120      */

121     private ASTNode.NodeList updaters =
122         new ASTNode.NodeList(UPDATERS_PROPERTY);
123
124     /**
125      * The body statement; lazily initialized; defaults to an empty block
126      * statement.
127      */

128     private Statement body = null;
129             
130     /**
131      * Creates a new AST node for a for statement owned by the given AST.
132      * By default, there are no initializers, no condition expression,
133      * no updaters, and the body is an empty block.
134      *
135      * @param ast the AST that is to own this node
136      */

137     ForStatement(AST ast) {
138         super(ast);
139     }
140
141     /* (omit javadoc for this method)
142      * Method declared on ASTNode.
143      */

144     final List JavaDoc internalStructuralPropertiesForType(int apiLevel) {
145         return propertyDescriptors(apiLevel);
146     }
147     
148
149     /* (omit javadoc for this method)
150      * Method declared on ASTNode.
151      */

152     final ASTNode internalGetSetChildProperty(ChildPropertyDescriptor property, boolean get, ASTNode child) {
153         if (property == EXPRESSION_PROPERTY) {
154             if (get) {
155                 return getExpression();
156             } else {
157                 setExpression((Expression) child);
158                 return null;
159             }
160         }
161         if (property == BODY_PROPERTY) {
162             if (get) {
163                 return getBody();
164             } else {
165                 setBody((Statement) child);
166                 return null;
167             }
168         }
169         // allow default implementation to flag the error
170
return super.internalGetSetChildProperty(property, get, child);
171     }
172     
173     /* (omit javadoc for this method)
174      * Method declared on ASTNode.
175      */

176     final List JavaDoc internalGetChildListProperty(ChildListPropertyDescriptor property) {
177         if (property == INITIALIZERS_PROPERTY) {
178             return initializers();
179         }
180         if (property == UPDATERS_PROPERTY) {
181             return updaters();
182         }
183         // allow default implementation to flag the error
184
return super.internalGetChildListProperty(property);
185     }
186
187     /* (omit javadoc for this method)
188      * Method declared on ASTNode.
189      */

190     final int getNodeType0() {
191         return FOR_STATEMENT;
192     }
193
194     /* (omit javadoc for this method)
195      * Method declared on ASTNode.
196      */

197     ASTNode clone0(AST target) {
198         ForStatement result = new ForStatement(target);
199         result.setSourceRange(this.getStartPosition(), this.getLength());
200         result.copyLeadingComment(this);
201         result.initializers().addAll(ASTNode.copySubtrees(target, initializers()));
202         result.setExpression(
203             (Expression) ASTNode.copySubtree(target, getExpression()));
204         result.updaters().addAll(ASTNode.copySubtrees(target, updaters()));
205         result.setBody(
206             (Statement) ASTNode.copySubtree(target, getBody()));
207         return result;
208     }
209
210     /* (omit javadoc for this method)
211      * Method declared on ASTNode.
212      */

213     final boolean subtreeMatch0(ASTMatcher matcher, Object JavaDoc other) {
214         // dispatch to correct overloaded match method
215
return matcher.match(this, other);
216     }
217
218     /* (omit javadoc for this method)
219      * Method declared on ASTNode.
220      */

221     void accept0(ASTVisitor visitor) {
222         boolean visitChildren = visitor.visit(this);
223         if (visitChildren) {
224             // visit children in normal left to right reading order
225
acceptChildren(visitor, this.initializers);
226             acceptChild(visitor, getExpression());
227             acceptChildren(visitor, this.updaters);
228             acceptChild(visitor, getBody());
229         }
230         visitor.endVisit(this);
231     }
232     
233     /**
234      * Returns the live ordered list of initializer expressions in this for
235      * statement.
236      * <p>
237      * The list should consist of either a list of so called statement
238      * expressions (JLS2, 14.8), or a single <code>VariableDeclarationExpression</code>.
239      * Otherwise, the for statement would have no Java source equivalent.
240      * </p>
241      *
242      * @return the live list of initializer expressions
243      * (element type: <code>Expression</code>)
244      */

245     public List JavaDoc initializers() {
246         return this.initializers;
247     }
248     
249     /**
250      * Returns the condition expression of this for statement, or
251      * <code>null</code> if there is none.
252      *
253      * @return the condition expression node, or <code>null</code> if
254      * there is none
255      */

256     public Expression getExpression() {
257         return this.optionalConditionExpression;
258     }
259     
260     /**
261      * Sets or clears the condition expression of this return statement.
262      *
263      * @param expression the condition expression node, or <code>null</code>
264      * if there is none
265      * @exception IllegalArgumentException if:
266      * <ul>
267      * <li>the node belongs to a different AST</li>
268      * <li>the node already has a parent</li>
269      * <li>a cycle in would be created</li>
270      * </ul>
271      */

272     public void setExpression(Expression expression) {
273         ASTNode oldChild = this.optionalConditionExpression;
274         preReplaceChild(oldChild, expression, EXPRESSION_PROPERTY);
275         this.optionalConditionExpression = expression;
276         postReplaceChild(oldChild, expression, EXPRESSION_PROPERTY);
277     }
278
279     /**
280      * Returns the live ordered list of update expressions in this for
281      * statement.
282      * <p>
283      * The list should consist of so called statement expressions. Otherwise,
284      * the for statement would have no Java source equivalent.
285      * </p>
286      *
287      * @return the live list of update expressions
288      * (element type: <code>Expression</code>)
289      */

290     public List JavaDoc updaters() {
291         return this.updaters;
292     }
293     
294     /**
295      * Returns the body of this for statement.
296      *
297      * @return the body statement node
298      */

299     public Statement getBody() {
300         if (this.body == null) {
301             // lazy init must be thread-safe for readers
302
synchronized (this) {
303                 if (this.body == null) {
304                     preLazyInit();
305                     this.body = new Block(this.ast);
306                     postLazyInit(this.body, BODY_PROPERTY);
307                 }
308             }
309         }
310         return this.body;
311     }
312     
313     /**
314      * Sets the body of this for statement.
315      * <p>
316      * Special note: The Java language does not allow a local variable declaration
317      * to appear as the body of a for statement (they may only appear within a
318      * block). However, the AST will allow a <code>VariableDeclarationStatement</code>
319      * as the body of a <code>ForStatement</code>. To get something that will
320      * compile, be sure to embed the <code>VariableDeclarationStatement</code>
321      * inside a <code>Block</code>.
322      * </p>
323      *
324      * @param statement the body statement node
325      * @exception IllegalArgumentException if:
326      * <ul>
327      * <li>the node belongs to a different AST</li>
328      * <li>the node already has a parent</li>
329      * <li>a cycle in would be created</li>
330      * </ul>
331      */

332     public void setBody(Statement statement) {
333         if (statement == null) {
334             throw new IllegalArgumentException JavaDoc();
335         }
336         ASTNode oldChild = this.body;
337         preReplaceChild(oldChild, statement, BODY_PROPERTY);
338         this.body = statement;
339         postReplaceChild(oldChild, statement, BODY_PROPERTY);
340     }
341     
342     /* (omit javadoc for this method)
343      * Method declared on ASTNode.
344      */

345     int memSize() {
346         return super.memSize() + 4 * 4;
347     }
348     
349     /* (omit javadoc for this method)
350      * Method declared on ASTNode.
351      */

352     int treeSize() {
353         return
354             memSize()
355             + this.initializers.listSize()
356             + this.updaters.listSize()
357             + (this.optionalConditionExpression == null ? 0 : getExpression().treeSize())
358             + (this.body == null ? 0 : getBody().treeSize());
359     }
360 }
361
Popular Tags