KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 2004 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 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.
20  * <p>
21  * <pre>
22  * SwitchCase:
23  * <b>case</b> Expression <b>:</b>
24  * <b>default</b> <b>:</b>
25  * </pre>
26  * </p>
27  *
28  * @since 2.0
29  */

30 public class SwitchCase extends Statement {
31     
32     /**
33      * The "expression" structural property of this node type.
34      * @since 3.0
35      */

36     public static final ChildPropertyDescriptor EXPRESSION_PROPERTY =
37         new ChildPropertyDescriptor(SwitchCase.class, "expression", Expression.class, OPTIONAL, CYCLE_RISK); //$NON-NLS-1$
38

39     /**
40      * A list of property descriptors (element type:
41      * {@link StructuralPropertyDescriptor}),
42      * or null if uninitialized.
43      */

44     private static final List JavaDoc PROPERTY_DESCRIPTORS;
45     
46     static {
47         List JavaDoc propertyList = new ArrayList JavaDoc(2);
48         createPropertyList(SwitchCase.class, propertyList);
49         addProperty(EXPRESSION_PROPERTY, propertyList);
50         PROPERTY_DESCRIPTORS = reapPropertyList(propertyList);
51     }
52
53     /**
54      * Returns a list of structural property descriptors for this node type.
55      * Clients must not modify the result.
56      *
57      * @param apiLevel the API level; one of the
58      * <code>AST.JLS&ast;</code> constants
59      * @return a list of property descriptors (element type:
60      * {@link StructuralPropertyDescriptor})
61      * @since 3.0
62      */

63     public static List JavaDoc propertyDescriptors(int apiLevel) {
64         return PROPERTY_DESCRIPTORS;
65     }
66             
67     /**
68      * The expression; <code>null</code> for none; lazily initialized (but
69      * does <b>not</b> default to none).
70      * @see #expressionInitialized
71      */

72     private Expression optionalExpression = null;
73
74     /**
75      * Indicates whether <code>optionalExpression</code> has been initialized.
76      */

77     private boolean expressionInitialized = false;
78     
79     /**
80      * Creates a new AST node for a switch case pseudo-statement owned by the
81      * given AST. By default, there is an unspecified, but legal, expression.
82      *
83      * @param ast the AST that is to own this node
84      */

85     SwitchCase(AST ast) {
86         super(ast);
87     }
88
89     /* (omit javadoc for this method)
90      * Method declared on ASTNode.
91      */

92     final List JavaDoc internalStructuralPropertiesForType(int apiLevel) {
93         return propertyDescriptors(apiLevel);
94     }
95     
96     /* (omit javadoc for this method)
97      * Method declared on ASTNode.
98      */

99     final ASTNode internalGetSetChildProperty(ChildPropertyDescriptor property, boolean get, ASTNode child) {
100         if (property == EXPRESSION_PROPERTY) {
101             if (get) {
102                 return getExpression();
103             } else {
104                 setExpression((Expression) child);
105                 return null;
106             }
107         }
108         // allow default implementation to flag the error
109
return super.internalGetSetChildProperty(property, get, child);
110     }
111     
112     /* (omit javadoc for this method)
113      * Method declared on ASTNode.
114      */

115     final int getNodeType0() {
116         return SWITCH_CASE;
117     }
118
119     /* (omit javadoc for this method)
120      * Method declared on ASTNode.
121      */

122     ASTNode clone0(AST target) {
123         SwitchCase result = new SwitchCase(target);
124         result.setSourceRange(this.getStartPosition(), this.getLength());
125         result.copyLeadingComment(this);
126         result.setExpression(
127             (Expression) ASTNode.copySubtree(target, getExpression()));
128         return result;
129     }
130
131     /* (omit javadoc for this method)
132      * Method declared on ASTNode.
133      */

134     final boolean subtreeMatch0(ASTMatcher matcher, Object JavaDoc other) {
135         // dispatch to correct overloaded match method
136
return matcher.match(this, other);
137     }
138
139     /* (omit javadoc for this method)
140      * Method declared on ASTNode.
141      */

142     void accept0(ASTVisitor visitor) {
143         boolean visitChildren = visitor.visit(this);
144         if (visitChildren) {
145             acceptChild(visitor, getExpression());
146         }
147         visitor.endVisit(this);
148     }
149     
150     /**
151      * Returns the expression of this switch case, or
152      * <code>null</code> if there is none (the "default:" case).
153      *
154      * @return the expression node, or <code>null</code> if there is none
155      */

156     public Expression getExpression() {
157         if (!this.expressionInitialized) {
158             // lazy init must be thread-safe for readers
159
synchronized (this) {
160                 if (!this.expressionInitialized) {
161                     preLazyInit();
162                     this.optionalExpression = new SimpleName(this.ast);
163                     this.expressionInitialized = true;
164                     postLazyInit(this.optionalExpression, EXPRESSION_PROPERTY);
165                 }
166             }
167         }
168         return this.optionalExpression;
169     }
170     
171     /**
172      * Sets the expression of this switch case, or clears it (turns it into
173      * the "default:" case).
174      *
175      * @param expression the expression node, or <code>null</code> to
176      * turn it into the "default:" case
177      * @exception IllegalArgumentException if:
178      * <ul>
179      * <li>the node belongs to a different AST</li>
180      * <li>the node already has a parent</li>
181      * <li>a cycle in would be created</li>
182      * </ul>
183      */

184     public void setExpression(Expression expression) {
185         ASTNode oldChild = this.optionalExpression;
186         preReplaceChild(oldChild, expression, EXPRESSION_PROPERTY);
187         this.optionalExpression = expression;
188         this.expressionInitialized = true;
189         postReplaceChild(oldChild, expression, EXPRESSION_PROPERTY);
190     }
191
192     /**
193      * Returns whether this switch case represents the "default:" case.
194      * <p>
195      * This convenience method is equivalent to
196      * <code>getExpression() == null</code>.
197      * </p>
198      *
199      * @return <code>true</code> if this is the default switch case, and
200      * <code>false</code> if this is a non-default switch case
201      */

202     public boolean isDefault() {
203         return getExpression() == null;
204     }
205     
206     /* (omit javadoc for this method)
207      * Method declared on ASTNode.
208      */

209     int memSize() {
210         return super.memSize() + 2 * 4;
211     }
212     
213     /* (omit javadoc for this method)
214      * Method declared on ASTNode.
215      */

216     int treeSize() {
217         return
218             memSize()
219             + (this.optionalExpression == null ? 0 : optionalExpression.treeSize());
220     }
221 }
222
Popular Tags