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 case AST node type. A switch case is a special kind of node used only
19 * in switch statements. It is a <code>Statement</code> in name only.
23 * <b>case</b> Expression <b>:</b>
24 * <b>default</b> <b>:</b>
29 * @noinstantiate This class is not intended to be instantiated by clients.
31 public class SwitchCase extends Statement {
34 * The "expression" structural property of this node type.
37 public static final ChildPropertyDescriptor EXPRESSION_PROPERTY =
38 new ChildPropertyDescriptor(SwitchCase.class, "expression", Expression.class, OPTIONAL, CYCLE_RISK); //$NON-NLS-1$
41 * A list of property descriptors (element type:
42 * {@link StructuralPropertyDescriptor}),
43 * or null if uninitialized.
45 private static final List PROPERTY_DESCRIPTORS;
48 List propertyList = new ArrayList(2);
49 createPropertyList(SwitchCase.class, propertyList);
50 addProperty(EXPRESSION_PROPERTY, propertyList);
51 PROPERTY_DESCRIPTORS = reapPropertyList(propertyList);
55 * Returns a list of structural property descriptors for this node type.
56 * Clients must not modify the result.
58 * @param apiLevel the API level; one of the
59 * <code>AST.JLS*</code> constants
60 * @return a list of property descriptors (element type:
61 * {@link StructuralPropertyDescriptor})
64 public static List propertyDescriptors(int apiLevel) {
65 return PROPERTY_DESCRIPTORS;
69 * The expression; <code>null</code> for none; lazily initialized (but
70 * does <b>not</b> default to none).
71 * @see #expressionInitialized
73 private Expression optionalExpression = null;
76 * Indicates whether <code>optionalExpression</code> has been initialized.
78 private boolean expressionInitialized = false;
81 * Creates a new AST node for a switch case pseudo-statement owned by the
82 * given AST. By default, there is an unspecified, but legal, expression.
84 * @param ast the AST that is to own this node
90 /* (omit javadoc for this method)
91 * Method declared on ASTNode.
93 final List internalStructuralPropertiesForType(int apiLevel) {
94 return propertyDescriptors(apiLevel);
97 /* (omit javadoc for this method)
98 * Method declared on ASTNode.
100 final ASTNode internalGetSetChildProperty(ChildPropertyDescriptor property, boolean get, ASTNode child) {
101 if (property == EXPRESSION_PROPERTY) {
103 return getExpression();
105 setExpression((Expression) child);
109 // allow default implementation to flag the error
110 return super.internalGetSetChildProperty(property, get, child);
113 /* (omit javadoc for this method)
114 * Method declared on ASTNode.
116 final int getNodeType0() {
120 /* (omit javadoc for this method)
121 * Method declared on ASTNode.
123 ASTNode clone0(AST target) {
124 SwitchCase result = new SwitchCase(target);
125 result.setSourceRange(this.getStartPosition(), this.getLength());
126 result.copyLeadingComment(this);
127 result.setExpression(
128 (Expression) ASTNode.copySubtree(target, getExpression()));
132 /* (omit javadoc for this method)
133 * Method declared on ASTNode.
135 final boolean subtreeMatch0(ASTMatcher matcher, Object other) {
136 // dispatch to correct overloaded match method
137 return matcher.match(this, other);
140 /* (omit javadoc for this method)
141 * Method declared on ASTNode.
143 void accept0(ASTVisitor visitor) {
144 boolean visitChildren = visitor.visit(this);
146 acceptChild(visitor, getExpression());
148 visitor.endVisit(this);
152 * Returns the expression of this switch case, or
153 * <code>null</code> if there is none (the "default:" case).
155 * @return the expression node, or <code>null</code> if there is none
157 public Expression getExpression() {
158 if (!this.expressionInitialized) {
159 // lazy init must be thread-safe for readers
160 synchronized (this) {
161 if (!this.expressionInitialized) {
163 this.optionalExpression = new SimpleName(this.ast);
164 this.expressionInitialized = true;
165 postLazyInit(this.optionalExpression, EXPRESSION_PROPERTY);
169 return this.optionalExpression;
173 * Sets the expression of this switch case, or clears it (turns it into
174 * the "default:" case).
176 * @param expression the expression node, or <code>null</code> to
177 * turn it into the "default:" case
178 * @exception IllegalArgumentException if:
180 * <li>the node belongs to a different AST</li>
181 * <li>the node already has a parent</li>
182 * <li>a cycle in would be created</li>
185 public void setExpression(Expression expression) {
186 ASTNode oldChild = this.optionalExpression;
187 preReplaceChild(oldChild, expression, EXPRESSION_PROPERTY);
188 this.optionalExpression = expression;
189 this.expressionInitialized = true;
190 postReplaceChild(oldChild, expression, EXPRESSION_PROPERTY);
194 * Returns whether this switch case represents the "default:" case.
196 * This convenience method is equivalent to
197 * <code>getExpression() == null</code>.
200 * @return <code>true</code> if this is the default switch case, and
201 * <code>false</code> if this is a non-default switch case
203 public boolean isDefault() {
204 return getExpression() == null;
207 /* (omit javadoc for this method)
208 * Method declared on ASTNode.
211 return super.memSize() + 2 * 4;
214 /* (omit javadoc for this method)
215 * Method declared on ASTNode.
220 + (this.optionalExpression == null ? 0 : optionalExpression.treeSize());