KickJava   Java API By Example, From Geeks To Geeks.

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


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  * Switch statement AST node type.
19  * <p>
20  * <pre>
21  * SwitchStatement:
22  * <b>switch</b> <b>(</b> Expression <b>)</b>
23  * <b>{</b> { SwitchCase | Statement } } <b>}</b>
24  * SwitchCase:
25  * <b>case</b> Expression <b>:</b>
26  * <b>default</b> <b>:</b>
27  * </pre>
28  * <code>SwitchCase</code> nodes are treated as a kind of
29  * <code>Statement</code>.
30  * </p>
31  *
32  * @since 2.0
33  */

34 public class SwitchStatement extends Statement {
35             
36     /**
37      * The "expression" structural property of this node type.
38      * @since 3.0
39      */

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

43     /**
44      * The "statements" structural property of this node type.
45      * @since 3.0
46      */

47     public static final ChildListPropertyDescriptor STATEMENTS_PROPERTY =
48         new ChildListPropertyDescriptor(SwitchStatement.class, "statements", Statement.class, 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 propertyList = new ArrayList JavaDoc(3);
59         createPropertyList(SwitchStatement.class, propertyList);
60         addProperty(EXPRESSION_PROPERTY, propertyList);
61         addProperty(STATEMENTS_PROPERTY, propertyList);
62         PROPERTY_DESCRIPTORS = reapPropertyList(propertyList);
63     }
64
65     /**
66      * Returns a list of structural property descriptors for this node type.
67      * Clients must not modify the result.
68      *
69      * @param apiLevel the API level; one of the
70      * <code>AST.JLS*</code> constants
71      * @return a list of property descriptors (element type:
72      * {@link StructuralPropertyDescriptor})
73      * @since 3.0
74      */

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

83     private Expression expression = null;
84
85     /**
86      * The statements and SwitchCase nodes
87      * (element type: <code>Statement</code>).
88      * Defaults to an empty list.
89      */

90     private ASTNode.NodeList statements =
91         new ASTNode.NodeList(STATEMENTS_PROPERTY);
92     
93     /**
94      * Creates a new unparented switch statement node owned by the given
95      * AST. By default, the swicth statement has an unspecified, but legal,
96      * expression, and an empty list of switch groups.
97      * <p>
98      * N.B. This constructor is package-private.
99      * </p>
100      *
101      * @param ast the AST that is to own this node
102      */

103     SwitchStatement(AST ast) {
104         super(ast);
105     }
106
107     /* (omit javadoc for this method)
108      * Method declared on ASTNode.
109      */

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

117     final ASTNode internalGetSetChildProperty(ChildPropertyDescriptor property, boolean get, ASTNode child) {
118         if (property == EXPRESSION_PROPERTY) {
119             if (get) {
120                 return getExpression();
121             } else {
122                 setExpression((Expression) 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 List JavaDoc internalGetChildListProperty(ChildListPropertyDescriptor property) {
134         if (property == STATEMENTS_PROPERTY) {
135             return statements();
136         }
137         // allow default implementation to flag the error
138
return super.internalGetChildListProperty(property);
139     }
140     
141     /* (omit javadoc for this method)
142      * Method declared on ASTNode.
143      */

144     final int getNodeType0() {
145         return SWITCH_STATEMENT;
146     }
147
148     /* (omit javadoc for this method)
149      * Method declared on ASTNode.
150      */

151     ASTNode clone0(AST target) {
152         SwitchStatement result = new SwitchStatement(target);
153         result.setSourceRange(this.getStartPosition(), this.getLength());
154         result.copyLeadingComment(this);
155         result.setExpression((Expression) getExpression().clone(target));
156         result.statements().addAll(ASTNode.copySubtrees(target, statements()));
157         return result;
158     }
159
160     /* (omit javadoc for this method)
161      * Method declared on ASTNode.
162      */

163     final boolean subtreeMatch0(ASTMatcher matcher, Object JavaDoc other) {
164         // dispatch to correct overloaded match method
165
return matcher.match(this, other);
166     }
167
168     /* (omit javadoc for this method)
169      * Method declared on ASTNode.
170      */

171     void accept0(ASTVisitor visitor) {
172         boolean visitChildren = visitor.visit(this);
173         if (visitChildren) {
174             // visit children in normal left to right reading order
175
acceptChild(visitor, getExpression());
176             acceptChildren(visitor, this.statements);
177         }
178         visitor.endVisit(this);
179     }
180     
181     /**
182      * Returns the expression of this switch statement.
183      *
184      * @return the expression node
185      */

186     public Expression getExpression() {
187         if (this.expression == null) {
188             // lazy init must be thread-safe for readers
189
synchronized (this) {
190                 if (this.expression == null) {
191                     preLazyInit();
192                     this.expression = new SimpleName(this.ast);
193                     postLazyInit(this.expression, EXPRESSION_PROPERTY);
194                 }
195             }
196         }
197         return this.expression;
198     }
199         
200     /**
201      * Sets the expression of this switch statement.
202      *
203      * @param expression the new expression node
204      * @exception IllegalArgumentException if:
205      * <ul>
206      * <li>the node belongs to a different AST</li>
207      * <li>the node already has a parent</li>
208      * <li>a cycle in would be created</li>
209      * </ul>
210      */

211     public void setExpression(Expression expression) {
212         if (expression == null) {
213             throw new IllegalArgumentException JavaDoc();
214         }
215         ASTNode oldChild = this.expression;
216         preReplaceChild(oldChild, expression, EXPRESSION_PROPERTY);
217         this.expression = expression;
218         postReplaceChild(oldChild, expression, EXPRESSION_PROPERTY);
219     }
220     
221     /**
222      * Returns the live ordered list of statements for this switch statement.
223      * Within this list, <code>SwitchCase</code> nodes mark the start of
224      * the switch groups.
225      *
226      * @return the live list of statement nodes
227      * (element type: <code>Statement</code>)
228      */

229     public List JavaDoc statements() {
230         return this.statements;
231     }
232     
233     /* (omit javadoc for this method)
234      * Method declared on ASTNode.
235      */

236     int memSize() {
237         return super.memSize() + 2 * 4;
238     }
239     
240     /* (omit javadoc for this method)
241      * Method declared on ASTNode.
242      */

243     int treeSize() {
244         return
245             memSize()
246             + (this.expression == null ? 0 : getExpression().treeSize())
247             + this.statements.listSize();
248     }
249 }
250
Popular Tags