KickJava   Java API By Example, From Geeks To Geeks.

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


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  * While statement AST node type.
19  *
20  * <pre>
21  * WhileStatement:
22  * <b>while</b> <b>(</b> Expression <b>)</b> Statement
23  * </pre>
24  *
25  * @since 2.0
26  */

27 public class WhileStatement 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(WhileStatement.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(WhileStatement.class, "body", Statement.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(WhileStatement.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 statement; lazily initialized; defaults to an empty block
81      * statement.
82      */

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

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

102     final List JavaDoc internalStructuralPropertiesForType(int apiLevel) {
103         return propertyDescriptors(apiLevel);
104     }
105     
106     /* (omit javadoc for this method)
107      * Method declared on ASTNode.
108      */

109     final ASTNode internalGetSetChildProperty(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((Statement) 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 WHILE_STATEMENT;
135     }
136
137     /* (omit javadoc for this method)
138      * Method declared on ASTNode.
139      */

140     ASTNode clone0(AST target) {
141         WhileStatement result = new WhileStatement(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));
146         return result;
147     }
148
149     /* (omit javadoc for this method)
150      * Method declared on ASTNode.
151      */

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

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

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) {
180                     preLazyInit();
181                     this.expression = new SimpleName(this.ast);
182                     postLazyInit(this.expression, EXPRESSION_PROPERTY);
183                 }
184             }
185         }
186         return this.expression;
187     }
188     
189     /**
190      * Sets the expression of this while statement.
191      *
192      * @param expression the expression node
193      * @exception IllegalArgumentException if:
194      * <ul>
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>
198      * </ul>
199      */

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

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) {
220                     preLazyInit();
221                     this.body = new Block(this.ast);
222                     postLazyInit(this.body, BODY_PROPERTY);
223                 }
224             }
225         }
226         return this.body;
227     }
228     
229     /**
230      * Sets the body of this while statement.
231      * <p>
232      * Special note: The Java language does not allow a local variable declaration
233      * to appear as the body of a while 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>WhileStatement</code>. To get something that will
236      * compile, be sure to embed the <code>VariableDeclarationStatement</code>
237      * inside a <code>Block</code>.
238      * </p>
239      *
240      * @param statement the body statement node
241      * @exception IllegalArgumentException if:
242      * <ul>
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>
246      * </ul>
247      */

248     public void setBody(Statement statement) {
249         if (statement == null) {
250             throw new IllegalArgumentException JavaDoc();
251         }
252         ASTNode oldChild = this.body;
253         preReplaceChild(oldChild, statement, BODY_PROPERTY);
254         this.body = statement;
255         postReplaceChild(oldChild, statement, BODY_PROPERTY);
256     }
257     
258     /* (omit javadoc for this method)
259      * Method declared on ASTNode.
260      */

261     int memSize() {
262         return super.memSize() + 2 * 4;
263     }
264     
265     /* (omit javadoc for this method)
266      * Method declared on ASTNode.
267      */

268     int treeSize() {
269         return
270             memSize()
271             + (this.expression == null ? 0 : getExpression().treeSize())
272             + (this.body == null ? 0 : getBody().treeSize());
273     }
274 }
275
276
Popular Tags