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 * Switch statement AST node type.
22 * <b>switch</b> <b>(</b> Expression <b>)</b>
23 * <b>{</b> { SwitchCase | Statement } } <b>}</b>
25 * <b>case</b> Expression <b>:</b>
26 * <b>default</b> <b>:</b>
28 * <code>SwitchCase</code> nodes are treated as a kind of
29 * <code>Statement</code>.
33 * @noinstantiate This class is not intended to be instantiated by clients.
35 public class SwitchStatement extends Statement {
38 * The "expression" structural property of this node type.
41 public static final ChildPropertyDescriptor EXPRESSION_PROPERTY =
42 new ChildPropertyDescriptor(SwitchStatement.class, "expression", Expression.class, MANDATORY, CYCLE_RISK); //$NON-NLS-1$
45 * The "statements" structural property of this node type.
48 public static final ChildListPropertyDescriptor STATEMENTS_PROPERTY =
49 new ChildListPropertyDescriptor(SwitchStatement.class, "statements", Statement.class, CYCLE_RISK); //$NON-NLS-1$
52 * A list of property descriptors (element type:
53 * {@link StructuralPropertyDescriptor}),
54 * or null if uninitialized.
56 private static final List PROPERTY_DESCRIPTORS;
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);
67 * Returns a list of structural property descriptors for this node type.
68 * Clients must not modify the result.
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})
76 public static List propertyDescriptors(int apiLevel) {
77 return PROPERTY_DESCRIPTORS;
81 * The expression; lazily initialized; defaults to a unspecified, but legal,
84 private Expression expression = null;
87 * The statements and SwitchCase nodes
88 * (element type: <code>Statement</code>).
89 * Defaults to an empty list.
91 private ASTNode.NodeList statements =
92 new ASTNode.NodeList(STATEMENTS_PROPERTY);
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.
99 * N.B. This constructor is package-private.
102 * @param ast the AST that is to own this node
104 SwitchStatement(AST ast) {
108 /* (omit javadoc for this method)
109 * Method declared on ASTNode.
111 final List internalStructuralPropertiesForType(int apiLevel) {
112 return propertyDescriptors(apiLevel);
115 /* (omit javadoc for this method)
116 * Method declared on ASTNode.
118 final ASTNode internalGetSetChildProperty(ChildPropertyDescriptor property, boolean get, ASTNode child) {
119 if (property == EXPRESSION_PROPERTY) {
121 return getExpression();
123 setExpression((Expression) child);
127 // allow default implementation to flag the error
128 return super.internalGetSetChildProperty(property, get, child);
131 /* (omit javadoc for this method)
132 * Method declared on ASTNode.
134 final List internalGetChildListProperty(ChildListPropertyDescriptor property) {
135 if (property == STATEMENTS_PROPERTY) {
138 // allow default implementation to flag the error
139 return super.internalGetChildListProperty(property);
142 /* (omit javadoc for this method)
143 * Method declared on ASTNode.
145 final int getNodeType0() {
146 return SWITCH_STATEMENT;
149 /* (omit javadoc for this method)
150 * Method declared on ASTNode.
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()));
161 /* (omit javadoc for this method)
162 * Method declared on ASTNode.
164 final boolean subtreeMatch0(ASTMatcher matcher, Object other) {
165 // dispatch to correct overloaded match method
166 return matcher.match(this, other);
169 /* (omit javadoc for this method)
170 * Method declared on ASTNode.
172 void accept0(ASTVisitor visitor) {
173 boolean visitChildren = visitor.visit(this);
175 // visit children in normal left to right reading order
176 acceptChild(visitor, getExpression());
177 acceptChildren(visitor, this.statements);
179 visitor.endVisit(this);
183 * Returns the expression of this switch statement.
185 * @return the expression node
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) {
193 this.expression = new SimpleName(this.ast);
194 postLazyInit(this.expression, EXPRESSION_PROPERTY);
198 return this.expression;
202 * Sets the expression of this switch statement.
204 * @param expression the new expression node
205 * @exception IllegalArgumentException if:
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>
212 public void setExpression(Expression expression) {
213 if (expression == null) {
214 throw new IllegalArgumentException();
216 ASTNode oldChild = this.expression;
217 preReplaceChild(oldChild, expression, EXPRESSION_PROPERTY);
218 this.expression = expression;
219 postReplaceChild(oldChild, expression, EXPRESSION_PROPERTY);
223 * Returns the live ordered list of statements for this switch statement.
224 * Within this list, <code>SwitchCase</code> nodes mark the start of
227 * @return the live list of statement nodes
228 * (element type: <code>Statement</code>)
230 public List statements() {
231 return this.statements;
234 /* (omit javadoc for this method)
235 * Method declared on ASTNode.
238 return super.memSize() + 2 * 4;
241 /* (omit javadoc for this method)
242 * Method declared on ASTNode.
247 + (this.expression == null ? 0 : getExpression().treeSize())
248 + this.statements.listSize();