1 /*******************************************************************************
2 * Copyright (c) 2000, 2008 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
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
12 package net.sourceforge.phpdt.core.dom;
14 import java.util.ArrayList;
15 import java.util.List;
18 * For statement AST node type.
24 * [ Expression ] <b>;</b>
25 * [ ForUpdate ] <b>)</b>
28 * Expression { <b>,</b> Expression }
30 * Expression { <b>,</b> Expression }
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
42 * @noinstantiate This class is not intended to be instantiated by clients.
44 public class ForStatement extends Statement {
47 * The "initializers" structural property of this node type.
50 public static final ChildListPropertyDescriptor INITIALIZERS_PROPERTY =
51 new ChildListPropertyDescriptor(ForStatement.class, "initializers", Expression.class, CYCLE_RISK); //$NON-NLS-1$
54 * The "expression" structural property of this node type.
57 public static final ChildPropertyDescriptor EXPRESSION_PROPERTY =
58 new ChildPropertyDescriptor(ForStatement.class, "expression", Expression.class, OPTIONAL, CYCLE_RISK); //$NON-NLS-1$
61 * The "updaters" structural property of this node type.
64 public static final ChildListPropertyDescriptor UPDATERS_PROPERTY =
65 new ChildListPropertyDescriptor(ForStatement.class, "updaters", Expression.class, CYCLE_RISK); //$NON-NLS-1$
68 * The "body" structural property of this node type.
71 public static final ChildPropertyDescriptor BODY_PROPERTY =
72 new ChildPropertyDescriptor(ForStatement.class, "body", Statement.class, MANDATORY, CYCLE_RISK); //$NON-NLS-1$
75 * A list of property descriptors (element type:
76 * {@link StructuralPropertyDescriptor}),
77 * or null if uninitialized.
79 private static final List PROPERTY_DESCRIPTORS;
82 List properyList = new ArrayList(5);
83 createPropertyList(ForStatement.class, properyList);
84 addProperty(INITIALIZERS_PROPERTY, properyList);
85 addProperty(EXPRESSION_PROPERTY, properyList);
86 addProperty(UPDATERS_PROPERTY, properyList);
87 addProperty(BODY_PROPERTY, properyList);
88 PROPERTY_DESCRIPTORS = reapPropertyList(properyList);
92 * Returns a list of structural property descriptors for this node type.
93 * Clients must not modify the result.
95 * @param apiLevel the API level; one of the
96 * <code>AST.JLS*</code> constants
98 * @return a list of property descriptors (element type:
99 * {@link StructuralPropertyDescriptor})
102 public static List propertyDescriptors(int apiLevel) {
103 return PROPERTY_DESCRIPTORS;
107 * The list of initializer expressions (element type:
108 * <code>Expression</code>). Defaults to an empty list.
110 private ASTNode.NodeList initializers =
111 new ASTNode.NodeList(INITIALIZERS_PROPERTY);
114 * The condition expression; <code>null</code> for none; defaults to none.
116 private Expression optionalConditionExpression = null;
119 * The list of update expressions (element type:
120 * <code>Expression</code>). Defaults to an empty list.
122 private ASTNode.NodeList updaters =
123 new ASTNode.NodeList(UPDATERS_PROPERTY);
126 * The body statement; lazily initialized; defaults to an empty block
129 private Statement body = null;
132 * Creates a new AST node for a for statement owned by the given AST.
133 * By default, there are no initializers, no condition expression,
134 * no updaters, and the body is an empty block.
136 * @param ast the AST that is to own this node
138 ForStatement(AST ast) {
142 /* (omit javadoc for this method)
143 * Method declared on ASTNode.
145 final List internalStructuralPropertiesForType(int apiLevel) {
146 return propertyDescriptors(apiLevel);
150 /* (omit javadoc for this method)
151 * Method declared on ASTNode.
153 final ASTNode internalGetSetChildProperty(ChildPropertyDescriptor property, boolean get, ASTNode child) {
154 if (property == EXPRESSION_PROPERTY) {
156 return getExpression();
158 setExpression((Expression) child);
162 if (property == BODY_PROPERTY) {
166 setBody((Statement) child);
170 // allow default implementation to flag the error
171 return super.internalGetSetChildProperty(property, get, child);
174 /* (omit javadoc for this method)
175 * Method declared on ASTNode.
177 final List internalGetChildListProperty(ChildListPropertyDescriptor property) {
178 if (property == INITIALIZERS_PROPERTY) {
179 return initializers();
181 if (property == UPDATERS_PROPERTY) {
184 // allow default implementation to flag the error
185 return super.internalGetChildListProperty(property);
188 /* (omit javadoc for this method)
189 * Method declared on ASTNode.
191 final int getNodeType0() {
192 return FOR_STATEMENT;
195 /* (omit javadoc for this method)
196 * Method declared on ASTNode.
198 ASTNode clone0(AST target) {
199 ForStatement result = new ForStatement(target);
200 result.setSourceRange(this.getStartPosition(), this.getLength());
201 result.copyLeadingComment(this);
202 result.initializers().addAll(ASTNode.copySubtrees(target, initializers()));
203 result.setExpression(
204 (Expression) ASTNode.copySubtree(target, getExpression()));
205 result.updaters().addAll(ASTNode.copySubtrees(target, updaters()));
207 (Statement) ASTNode.copySubtree(target, getBody()));
211 /* (omit javadoc for this method)
212 * Method declared on ASTNode.
214 final boolean subtreeMatch0(ASTMatcher matcher, Object other) {
215 // dispatch to correct overloaded match method
216 return matcher.match(this, other);
219 /* (omit javadoc for this method)
220 * Method declared on ASTNode.
222 void accept0(ASTVisitor visitor) {
223 boolean visitChildren = visitor.visit(this);
225 // visit children in normal left to right reading order
226 acceptChildren(visitor, this.initializers);
227 acceptChild(visitor, getExpression());
228 acceptChildren(visitor, this.updaters);
229 acceptChild(visitor, getBody());
231 visitor.endVisit(this);
235 * Returns the live ordered list of initializer expressions in this for
238 * The list should consist of either a list of so called statement
239 * expressions (JLS2, 14.8), or a single <code>VariableDeclarationExpression</code>.
240 * Otherwise, the for statement would have no Java source equivalent.
243 * @return the live list of initializer expressions
244 * (element type: <code>Expression</code>)
246 public List initializers() {
247 return this.initializers;
251 * Returns the condition expression of this for statement, or
252 * <code>null</code> if there is none.
254 * @return the condition expression node, or <code>null</code> if
257 public Expression getExpression() {
258 return this.optionalConditionExpression;
262 * Sets or clears the condition expression of this return statement.
264 * @param expression the condition expression node, or <code>null</code>
266 * @exception IllegalArgumentException if:
268 * <li>the node belongs to a different AST</li>
269 * <li>the node already has a parent</li>
270 * <li>a cycle in would be created</li>
273 public void setExpression(Expression expression) {
274 ASTNode oldChild = this.optionalConditionExpression;
275 preReplaceChild(oldChild, expression, EXPRESSION_PROPERTY);
276 this.optionalConditionExpression = expression;
277 postReplaceChild(oldChild, expression, EXPRESSION_PROPERTY);
281 * Returns the live ordered list of update expressions in this for
284 * The list should consist of so called statement expressions. Otherwise,
285 * the for statement would have no Java source equivalent.
288 * @return the live list of update expressions
289 * (element type: <code>Expression</code>)
291 public List updaters() {
292 return this.updaters;
296 * Returns the body of this for statement.
298 * @return the body statement node
300 public Statement getBody() {
301 if (this.body == null) {
302 // lazy init must be thread-safe for readers
303 synchronized (this) {
304 if (this.body == null) {
306 this.body = new Block(this.ast);
307 postLazyInit(this.body, BODY_PROPERTY);
315 * Sets the body of this for statement.
317 * Special note: The Java language does not allow a local variable declaration
318 * to appear as the body of a for statement (they may only appear within a
319 * block). However, the AST will allow a <code>VariableDeclarationStatement</code>
320 * as the body of a <code>ForStatement</code>. To get something that will
321 * compile, be sure to embed the <code>VariableDeclarationStatement</code>
322 * inside a <code>Block</code>.
325 * @param statement the body statement node
326 * @exception IllegalArgumentException if:
328 * <li>the node belongs to a different AST</li>
329 * <li>the node already has a parent</li>
330 * <li>a cycle in would be created</li>
333 public void setBody(Statement statement) {
334 if (statement == null) {
335 throw new IllegalArgumentException();
337 ASTNode oldChild = this.body;
338 preReplaceChild(oldChild, statement, BODY_PROPERTY);
339 this.body = statement;
340 postReplaceChild(oldChild, statement, BODY_PROPERTY);
343 /* (omit javadoc for this method)
344 * Method declared on ASTNode.
347 return super.memSize() + 4 * 4;
350 /* (omit javadoc for this method)
351 * Method declared on ASTNode.
356 + this.initializers.listSize()
357 + this.updaters.listSize()
358 + (this.optionalConditionExpression == null ? 0 : getExpression().treeSize())
359 + (this.body == null ? 0 : getBody().treeSize());