KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 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  * Enhanced For statement AST node type (added in JLS3 API).
19  *
20  * <pre>
21  * EnhancedForStatement:
22  * <b>for</b> <b>(</b> FormalParameter <b>:</b> Expression <b>)</b>
23  * Statement
24  * </pre>
25  * The FormalParameter is represented by a <code>SingleVariableDeclaration</code>
26  * (without an initializer).
27  *
28  * @since 3.1
29  */

30 public class EnhancedForStatement extends Statement {
31     
32     /**
33      * The "parameter" structural property of this node type.
34      */

35     public static final ChildPropertyDescriptor PARAMETER_PROPERTY =
36         new ChildPropertyDescriptor(EnhancedForStatement.class, "parameter", SingleVariableDeclaration.class, MANDATORY, CYCLE_RISK); //$NON-NLS-1$
37

38     /**
39      * The "expression" structural property of this node type.
40      */

41     public static final ChildPropertyDescriptor EXPRESSION_PROPERTY =
42         new ChildPropertyDescriptor(EnhancedForStatement.class, "expression", Expression.class, MANDATORY, CYCLE_RISK); //$NON-NLS-1$
43

44     /**
45      * The "body" structural property of this node type.
46      */

47     public static final ChildPropertyDescriptor BODY_PROPERTY =
48         new ChildPropertyDescriptor(EnhancedForStatement.class, "body", Statement.class, MANDATORY, CYCLE_RISK); //$NON-NLS-1$
49

50     /**
51      * A list of property descriptors (element type:
52      * {@link StructuralPropertyDescriptor}),
53      * or null if uninitialized.
54      */

55     private static final List JavaDoc PROPERTY_DESCRIPTORS;
56     
57     static {
58         List JavaDoc properyList = new ArrayList JavaDoc(4);
59         createPropertyList(EnhancedForStatement.class, properyList);
60         addProperty(PARAMETER_PROPERTY, properyList);
61         addProperty(EXPRESSION_PROPERTY, properyList);
62         addProperty(BODY_PROPERTY, properyList);
63         PROPERTY_DESCRIPTORS = reapPropertyList(properyList);
64     }
65
66     /**
67      * Returns a list of structural property descriptors for this node type.
68      * Clients must not modify the result.
69      *
70      * @param apiLevel the API level; one of the
71      * <code>AST.JLS&ast;</code> constants
72
73      * @return a list of property descriptors (element type:
74      * {@link StructuralPropertyDescriptor})
75      */

76     public static List JavaDoc propertyDescriptors(int apiLevel) {
77         return PROPERTY_DESCRIPTORS;
78     }
79             
80     /**
81      * The parameter; lazily initialized; defaults to a unspecified,
82      * legal node.
83      */

84     private SingleVariableDeclaration parameter = null;
85
86     /**
87      * The expression; lazily initialized; defaults to a unspecified, but legal,
88      * expression.
89      */

90     private Expression expression = null;
91
92     /**
93      * The body statement; lazily initialized; defaults to an empty block
94      * statement.
95      */

96     private Statement body = null;
97             
98     /**
99      * Creates a new AST node for an enchanced for statement owned by the
100      * given AST. By default, the parameter and expression are unspecified
101      * but legal subtrees, and the body is an empty block.
102      *
103      * @param ast the AST that is to own this node
104      */

105     EnhancedForStatement(AST ast) {
106         super(ast);
107         unsupportedIn2();
108     }
109
110     /* (omit javadoc for this method)
111      * Method declared on ASTNode.
112      */

113     final List JavaDoc internalStructuralPropertiesForType(int apiLevel) {
114         return propertyDescriptors(apiLevel);
115     }
116     
117     /* (omit javadoc for this method)
118      * Method declared on ASTNode.
119      */

120     final ASTNode internalGetSetChildProperty(ChildPropertyDescriptor property, boolean get, ASTNode child) {
121         if (property == PARAMETER_PROPERTY) {
122             if (get) {
123                 return getParameter();
124             } else {
125                 setParameter((SingleVariableDeclaration) child);
126                 return null;
127             }
128         }
129         if (property == EXPRESSION_PROPERTY) {
130             if (get) {
131                 return getExpression();
132             } else {
133                 setExpression((Expression) child);
134                 return null;
135             }
136         }
137         if (property == BODY_PROPERTY) {
138             if (get) {
139                 return getBody();
140             } else {
141                 setBody((Statement) child);
142                 return null;
143             }
144         }
145         // allow default implementation to flag the error
146
return super.internalGetSetChildProperty(property, get, child);
147     }
148     
149     /* (omit javadoc for this method)
150      * Method declared on ASTNode.
151      */

152     final int getNodeType0() {
153         return ENHANCED_FOR_STATEMENT;
154     }
155
156     /* (omit javadoc for this method)
157      * Method declared on ASTNode.
158      */

159     ASTNode clone0(AST target) {
160         EnhancedForStatement result = new EnhancedForStatement(target);
161         result.setSourceRange(this.getStartPosition(), this.getLength());
162         result.copyLeadingComment(this);
163         result.setParameter((SingleVariableDeclaration) getParameter().clone(target));
164         result.setExpression((Expression) getExpression().clone(target));
165         result.setBody(
166             (Statement) ASTNode.copySubtree(target, getBody()));
167         return result;
168     }
169
170     /* (omit javadoc for this method)
171      * Method declared on ASTNode.
172      */

173     final boolean subtreeMatch0(ASTMatcher matcher, Object JavaDoc other) {
174         // dispatch to correct overloaded match method
175
return matcher.match(this, other);
176     }
177
178     /* (omit javadoc for this method)
179      * Method declared on ASTNode.
180      */

181     void accept0(ASTVisitor visitor) {
182         boolean visitChildren = visitor.visit(this);
183         if (visitChildren) {
184             // visit children in normal left to right reading order
185
acceptChild(visitor, getParameter());
186             acceptChild(visitor, getExpression());
187             acceptChild(visitor, getBody());
188         }
189         visitor.endVisit(this);
190     }
191     
192     /**
193      * Returns the formal parameter in this enhanced for statement.
194      *
195      * @return the parameter
196      */

197     public SingleVariableDeclaration getParameter() {
198         if (this.parameter == null) {
199             // lazy init must be thread-safe for readers
200
synchronized (this) {
201                 if (this.parameter == null) {
202                     preLazyInit();
203                     this.parameter = this.ast.newSingleVariableDeclaration();
204                     postLazyInit(this.parameter, PARAMETER_PROPERTY);
205                 }
206             }
207         }
208         return this.parameter;
209     }
210
211     /**
212      * Sets the formal parameter in this enhanced for statement.
213      *
214      * @param parameter the new parameter
215      * @exception IllegalArgumentException if:
216      * <ul>
217      * <li>the node belongs to a different AST</li>
218      * <li>the node already has a parent</li>
219      * </ul>
220      */

221     public void setParameter(SingleVariableDeclaration parameter) {
222         if (parameter == null) {
223             throw new IllegalArgumentException JavaDoc();
224         }
225         ASTNode oldChild = this.parameter;
226         preReplaceChild(oldChild, parameter, PARAMETER_PROPERTY);
227         this.parameter = parameter;
228         postReplaceChild(oldChild, parameter, PARAMETER_PROPERTY);
229     }
230     
231     /**
232      * Returns the expression of this enhanced for statement.
233      *
234      * @return the expression node
235      */

236     public Expression getExpression() {
237         if (this.expression == null) {
238             // lazy init must be thread-safe for readers
239
synchronized (this) {
240                 if (this.expression == null) {
241                     preLazyInit();
242                     this.expression = new SimpleName(this.ast);
243                     postLazyInit(this.expression, EXPRESSION_PROPERTY);
244                 }
245             }
246         }
247         return this.expression;
248     }
249         
250     /**
251      * Sets the expression of this enhanced for statement.
252      *
253      * @param expression the new expression node
254      * @exception IllegalArgumentException if:
255      * <ul>
256      * <li>the node belongs to a different AST</li>
257      * <li>the node already has a parent</li>
258      * <li>a cycle in would be created</li>
259      * </ul>
260      */

261     public void setExpression(Expression expression) {
262         if (expression == null) {
263             throw new IllegalArgumentException JavaDoc();
264         }
265         ASTNode oldChild = this.expression;
266         preReplaceChild(oldChild, expression, EXPRESSION_PROPERTY);
267         this.expression = expression;
268         postReplaceChild(oldChild, expression, EXPRESSION_PROPERTY);
269     }
270
271     /**
272      * Returns the body of this enchanced for statement.
273      *
274      * @return the body statement node
275      */

276     public Statement getBody() {
277         if (this.body == null) {
278             // lazy init must be thread-safe for readers
279
synchronized (this) {
280                 if (this.body == null) {
281                     preLazyInit();
282                     this.body = new Block(this.ast);
283                     postLazyInit(this.body, BODY_PROPERTY);
284                 }
285             }
286         }
287         return this.body;
288     }
289     
290     /**
291      * Sets the body of this enhanced for statement.
292      *
293      * @param statement the body statement node
294      * @exception IllegalArgumentException if:
295      * <ul>
296      * <li>the node belongs to a different AST</li>
297      * <li>the node already has a parent</li>
298      * <li>a cycle in would be created</li>
299      * </ul>
300      */

301     public void setBody(Statement statement) {
302         if (statement == null) {
303             throw new IllegalArgumentException JavaDoc();
304         }
305         ASTNode oldChild = this.body;
306         preReplaceChild(oldChild, statement, BODY_PROPERTY);
307         this.body = statement;
308         postReplaceChild(oldChild, statement, BODY_PROPERTY);
309     }
310     
311     /* (omit javadoc for this method)
312      * Method declared on ASTNode.
313      */

314     int memSize() {
315         return super.memSize() + 3 * 4;
316     }
317     
318     /* (omit javadoc for this method)
319      * Method declared on ASTNode.
320      */

321     int treeSize() {
322         return
323             memSize()
324             + (this.parameter == null ? 0 : getParameter().treeSize())
325             + (this.expression == null ? 0 : getExpression().treeSize())
326             + (this.body == null ? 0 : getBody().treeSize());
327     }
328 }
329
Popular Tags