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 * Do statement AST node type.
22 * <b>do</b> Statement <b>while</b> <b>(</b> Expression <b>)</b> <b>;</b>
26 * @noinstantiate This class is not intended to be instantiated by clients.
28 public class DoStatement extends Statement {
31 * The "expression" structural property of this node type.
34 public static final ChildPropertyDescriptor EXPRESSION_PROPERTY =
35 new ChildPropertyDescriptor(DoStatement.class, "expression", Expression.class, MANDATORY, CYCLE_RISK); //$NON-NLS-1$
38 * The "body" structural property of this node type.
41 public static final ChildPropertyDescriptor BODY_PROPERTY =
42 new ChildPropertyDescriptor(DoStatement.class, "body", Statement.class, MANDATORY, CYCLE_RISK); //$NON-NLS-1$
45 * A list of property descriptors (element type:
46 * {@link StructuralPropertyDescriptor}),
47 * or null if uninitialized.
49 private static final List PROPERTY_DESCRIPTORS;
52 List properyList = new ArrayList(3);
53 createPropertyList(DoStatement.class, properyList);
54 addProperty(EXPRESSION_PROPERTY, properyList);
55 addProperty(BODY_PROPERTY, properyList);
56 PROPERTY_DESCRIPTORS = reapPropertyList(properyList);
60 * Returns a list of structural property descriptors for this node type.
61 * Clients must not modify the result.
63 * @param apiLevel the API level; one of the
64 * <code>AST.JLS*</code> constants
66 * @return a list of property descriptors (element type:
67 * {@link StructuralPropertyDescriptor})
70 public static List propertyDescriptors(int apiLevel) {
71 return PROPERTY_DESCRIPTORS;
75 * The expression; lazily initialized; defaults to an unspecified, but
78 private Expression expression = null;
81 * The body statement; lazily initialized; defaults to an empty block.
83 private Statement body = null;
86 * Creates a new unparented do statement node owned by the given
87 * AST. By default, the expresssion is unspecified, but legal,
88 * and the body statement is an empty block.
90 * N.B. This constructor is package-private.
93 * @param ast the AST that is to own this node
95 DoStatement(AST ast) {
99 /* (omit javadoc for this method)
100 * Method declared on ASTNode.
102 final List internalStructuralPropertiesForType(int apiLevel) {
103 return propertyDescriptors(apiLevel);
106 /* (omit javadoc for this method)
107 * Method declared on ASTNode.
109 final ASTNode internalGetSetChildProperty(ChildPropertyDescriptor property, boolean get, ASTNode child) {
110 if (property == EXPRESSION_PROPERTY) {
112 return getExpression();
114 setExpression((Expression) child);
118 if (property == BODY_PROPERTY) {
122 setBody((Statement) child);
126 // allow default implementation to flag the error
127 return super.internalGetSetChildProperty(property, get, child);
130 /* (omit javadoc for this method)
131 * Method declared on ASTNode.
133 final int getNodeType0() {
137 /* (omit javadoc for this method)
138 * Method declared on ASTNode.
140 ASTNode clone0(AST target) {
141 DoStatement result = new DoStatement(target);
142 result.setSourceRange(this.getStartPosition(), this.getLength());
143 result.copyLeadingComment(this);
144 result.setExpression((Expression) getExpression().clone(target));
145 result.setBody((Statement) getBody().clone(target));
149 /* (omit javadoc for this method)
150 * Method declared on ASTNode.
152 final boolean subtreeMatch0(ASTMatcher matcher, Object other) {
153 // dispatch to correct overloaded match method
154 return matcher.match(this, other);
157 /* (omit javadoc for this method)
158 * Method declared on ASTNode.
160 void accept0(ASTVisitor visitor) {
161 boolean visitChildren = visitor.visit(this);
163 // visit children in normal left to right reading order
164 acceptChild(visitor, getBody());
165 acceptChild(visitor, getExpression());
167 visitor.endVisit(this);
171 * Returns the expression of this do statement.
173 * @return the expression node
175 public Expression getExpression() {
176 if (this.expression == null) {
177 // lazy init must be thread-safe for readers
178 synchronized (this) {
179 if (this.expression == null) {
181 this.expression = new SimpleName(this.ast);
182 postLazyInit(this.expression, EXPRESSION_PROPERTY);
186 return this.expression;
190 * Sets the expression of this do statement.
192 * @param expression the expression node
193 * @exception IllegalArgumentException if:
195 * <li>the node belongs to a different AST</li>
196 * <li>the node already has a parent</li>
197 * <li>a cycle in would be created</li>
200 public void setExpression(Expression expression) {
201 if (expression == null) {
202 throw new IllegalArgumentException();
204 ASTNode oldChild = this.expression;
205 preReplaceChild(oldChild, expression, EXPRESSION_PROPERTY);
206 this.expression = expression;
207 postReplaceChild(oldChild, expression, EXPRESSION_PROPERTY);
211 * Returns the body of this do statement.
213 * @return the body statement node
215 public Statement getBody() {
216 if (this.body == null) {
217 // lazy init must be thread-safe for readers
218 synchronized (this) {
219 if (this.body == null) {
221 this.body = new Block(this.ast);
222 postLazyInit(this.body, BODY_PROPERTY);
230 * Sets the body of this do statement.
232 * Special note: The Java language does not allow a local variable declaration
233 * to appear as the body of a do statement (they may only appear within a
234 * block). However, the AST will allow a <code>VariableDeclarationStatement</code>
235 * as the body of a <code>DoStatement</code>. To get something that will
236 * compile, be sure to embed the <code>VariableDeclarationStatement</code>
237 * inside a <code>Block</code>.
240 * @param statement the body statement node
241 * @exception IllegalArgumentException if:
243 * <li>the node belongs to a different AST</li>
244 * <li>the node already has a parent</li>
245 * <li>a cycle in would be created</li>
248 public void setBody(Statement statement) {
249 if (statement == null) {
250 throw new IllegalArgumentException();
252 ASTNode oldChild = this.body;
253 preReplaceChild(oldChild, statement, BODY_PROPERTY);
254 this.body = statement;
255 postReplaceChild(oldChild, statement, BODY_PROPERTY);
258 /* (omit javadoc for this method)
259 * Method declared on ASTNode.
262 return super.memSize() + 2 * 4;
265 /* (omit javadoc for this method)
266 * Method declared on ASTNode.
271 + (this.expression == null ? 0 : getExpression().treeSize())
272 + (this.body == null ? 0 : getBody().treeSize());