Initial upgrade to Platform/JDT 3.4.1
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / core / dom / SwitchStatement.java
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
7  *
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  *******************************************************************************/
11
12 package net.sourceforge.phpdt.core.dom;
13
14 import java.util.ArrayList;
15 import java.util.List;
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  * @noinstantiate This class is not intended to be instantiated by clients.
34  */
35 public class SwitchStatement extends Statement {
36                         
37         /**
38          * The "expression" structural property of this node type.
39          * @since 3.0
40          */
41         public static final ChildPropertyDescriptor EXPRESSION_PROPERTY = 
42                 new ChildPropertyDescriptor(SwitchStatement.class, "expression", Expression.class, MANDATORY, CYCLE_RISK); //$NON-NLS-1$
43
44         /**
45          * The "statements" structural property of this node type.
46          * @since 3.0
47          */
48         public static final ChildListPropertyDescriptor STATEMENTS_PROPERTY = 
49                 new ChildListPropertyDescriptor(SwitchStatement.class, "statements", Statement.class, CYCLE_RISK); //$NON-NLS-1$
50
51         /**
52          * A list of property descriptors (element type: 
53          * {@link StructuralPropertyDescriptor}),
54          * or null if uninitialized.
55          */
56         private static final List PROPERTY_DESCRIPTORS;
57         
58         static {
59                 List propertyList = new ArrayList(3);
60                 createPropertyList(SwitchStatement.class, propertyList);
61                 addProperty(EXPRESSION_PROPERTY, propertyList);
62                 addProperty(STATEMENTS_PROPERTY, propertyList);
63                 PROPERTY_DESCRIPTORS = reapPropertyList(propertyList);
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*</code> constants
72          * @return a list of property descriptors (element type: 
73          * {@link StructuralPropertyDescriptor})
74          * @since 3.0
75          */
76         public static List propertyDescriptors(int apiLevel) {
77                 return PROPERTY_DESCRIPTORS;
78         }
79                         
80         /**
81          * The expression; lazily initialized; defaults to a unspecified, but legal,
82          * expression.
83          */
84         private Expression expression = null;
85
86         /**
87          * The statements and SwitchCase nodes
88          * (element type: <code>Statement</code>).
89          * Defaults to an empty list.
90          */
91         private ASTNode.NodeList statements =
92                 new ASTNode.NodeList(STATEMENTS_PROPERTY);
93         
94         /**
95          * Creates a new unparented switch statement node owned by the given 
96          * AST. By default, the swicth statement has an unspecified, but legal,
97          * expression, and an empty list of switch groups.
98          * <p>
99          * N.B. This constructor is package-private.
100          * </p>
101          * 
102          * @param ast the AST that is to own this node
103          */
104         SwitchStatement(AST ast) {
105                 super(ast);
106         }
107
108         /* (omit javadoc for this method)
109          * Method declared on ASTNode.
110          */
111         final List internalStructuralPropertiesForType(int apiLevel) {
112                 return propertyDescriptors(apiLevel);
113         }
114         
115         /* (omit javadoc for this method)
116          * Method declared on ASTNode.
117          */
118         final ASTNode internalGetSetChildProperty(ChildPropertyDescriptor property, boolean get, ASTNode child) {
119                 if (property == EXPRESSION_PROPERTY) {
120                         if (get) {
121                                 return getExpression();
122                         } else {
123                                 setExpression((Expression) child);
124                                 return null;
125                         }
126                 }
127                 // allow default implementation to flag the error
128                 return super.internalGetSetChildProperty(property, get, child);
129         }
130         
131         /* (omit javadoc for this method)
132          * Method declared on ASTNode.
133          */
134         final List internalGetChildListProperty(ChildListPropertyDescriptor property) {
135                 if (property == STATEMENTS_PROPERTY) {
136                         return statements();
137                 }
138                 // allow default implementation to flag the error
139                 return super.internalGetChildListProperty(property);
140         }
141         
142         /* (omit javadoc for this method)
143          * Method declared on ASTNode.
144          */
145         final int getNodeType0() {
146                 return SWITCH_STATEMENT;
147         }
148
149         /* (omit javadoc for this method)
150          * Method declared on ASTNode.
151          */
152         ASTNode clone0(AST target) {
153                 SwitchStatement result = new SwitchStatement(target);
154                 result.setSourceRange(this.getStartPosition(), this.getLength());
155                 result.copyLeadingComment(this);
156                 result.setExpression((Expression) getExpression().clone(target));
157                 result.statements().addAll(ASTNode.copySubtrees(target, statements()));
158                 return result;
159         }
160
161         /* (omit javadoc for this method)
162          * Method declared on ASTNode.
163          */
164         final boolean subtreeMatch0(ASTMatcher matcher, Object other) {
165                 // dispatch to correct overloaded match method
166                 return matcher.match(this, other);
167         }
168
169         /* (omit javadoc for this method)
170          * Method declared on ASTNode.
171          */
172         void accept0(ASTVisitor visitor) {
173                 boolean visitChildren = visitor.visit(this);
174                 if (visitChildren) {
175                         // visit children in normal left to right reading order
176                         acceptChild(visitor, getExpression());
177                         acceptChildren(visitor, this.statements);
178                 }
179                 visitor.endVisit(this);
180         }
181         
182         /**
183          * Returns the expression of this switch statement.
184          * 
185          * @return the expression node
186          */ 
187         public Expression getExpression() {
188                 if (this.expression == null) {
189                         // lazy init must be thread-safe for readers
190                         synchronized (this) {
191                                 if (this.expression == null) {
192                                         preLazyInit();
193                                         this.expression = new SimpleName(this.ast);
194                                         postLazyInit(this.expression, EXPRESSION_PROPERTY);
195                                 }
196                         }
197                 }
198                 return this.expression;
199         }
200                 
201         /**
202          * Sets the expression of this switch statement.
203          * 
204          * @param expression the new expression node
205          * @exception IllegalArgumentException if:
206          * <ul>
207          * <li>the node belongs to a different AST</li>
208          * <li>the node already has a parent</li>
209          * <li>a cycle in would be created</li>
210          * </ul>
211          */ 
212         public void setExpression(Expression expression) {
213                 if (expression == null) {
214                         throw new IllegalArgumentException();
215                 }
216                 ASTNode oldChild = this.expression;
217                 preReplaceChild(oldChild, expression, EXPRESSION_PROPERTY);
218                 this.expression = expression;
219                 postReplaceChild(oldChild, expression, EXPRESSION_PROPERTY);
220         }
221         
222         /**
223          * Returns the live ordered list of statements for this switch statement.
224          * Within this list, <code>SwitchCase</code> nodes mark the start of 
225          * the switch groups.
226          * 
227          * @return the live list of statement nodes
228          *    (element type: <code>Statement</code>)
229          */ 
230         public List statements() {
231                 return this.statements;
232         }
233         
234         /* (omit javadoc for this method)
235          * Method declared on ASTNode.
236          */
237         int memSize() {
238                 return super.memSize() + 2 * 4;
239         }
240         
241         /* (omit javadoc for this method)
242          * Method declared on ASTNode.
243          */
244         int treeSize() {
245                 return
246                         memSize()
247                         + (this.expression == null ? 0 : getExpression().treeSize())
248                         + this.statements.listSize();
249         }
250 }