removed unused private methods.
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / core / dom / LabeledStatement.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  * Labeled statement AST node type.
19  *
20  * <pre>
21  * LabeledStatement:
22  *    Identifier <b>:</b> Statement
23  * </pre>
24  * 
25  * @since 2.0
26  * @noinstantiate This class is not intended to be instantiated by clients.
27  */
28 public class LabeledStatement extends Statement {
29                         
30         /**
31          * The "label" structural property of this node type.
32          * @since 3.0
33          */
34         public static final ChildPropertyDescriptor LABEL_PROPERTY = 
35                 new ChildPropertyDescriptor(LabeledStatement.class, "label", SimpleName.class, MANDATORY, NO_CYCLE_RISK); //$NON-NLS-1$
36
37         /**
38          * The "body" structural property of this node type.
39          * @since 3.0
40          */
41         public static final ChildPropertyDescriptor BODY_PROPERTY = 
42                 new ChildPropertyDescriptor(LabeledStatement.class, "body", Statement.class, MANDATORY, CYCLE_RISK); //$NON-NLS-1$
43
44         /**
45          * A list of property descriptors (element type: 
46          * {@link StructuralPropertyDescriptor}),
47          * or null if uninitialized.
48          */
49         private static final List PROPERTY_DESCRIPTORS;
50         
51         static {
52                 List propertyList = new ArrayList(3);
53                 createPropertyList(LabeledStatement.class, propertyList);
54                 addProperty(LABEL_PROPERTY, propertyList);
55                 addProperty(BODY_PROPERTY, propertyList);
56                 PROPERTY_DESCRIPTORS = reapPropertyList(propertyList);
57         }
58
59         /**
60          * Returns a list of structural property descriptors for this node type.
61          * Clients must not modify the result.
62          * 
63          * @param apiLevel the API level; one of the
64          * <code>AST.JLS*</code> constants
65
66          * @return a list of property descriptors (element type: 
67          * {@link StructuralPropertyDescriptor})
68          * @since 3.0
69          */
70         public static List propertyDescriptors(int apiLevel) {
71                 return PROPERTY_DESCRIPTORS;
72         }
73                         
74         /**
75          * The label; lazily initialized; defaults to a unspecified,
76          * legal Java identifier.
77          */
78         private SimpleName labelName = null;
79
80         /**
81          * The body statement; lazily initialized; defaults to an unspecified, but 
82          * legal, statement.
83          */
84         private Statement body = null;
85
86         /**
87          * Creates a new AST node for a labeled statement owned by the given 
88          * AST. By default, the statement has an unspecified (but legal) label
89          * and an unspecified (but legal) statement.
90          * <p>
91          * N.B. This constructor is package-private.
92          * </p>
93          * 
94          * @param ast the AST that is to own this node
95          */
96         LabeledStatement(AST ast) {
97                 super(ast);
98         }
99
100         /* (omit javadoc for this method)
101          * Method declared on ASTNode.
102          */
103         final List internalStructuralPropertiesForType(int apiLevel) {
104                 return propertyDescriptors(apiLevel);
105         }
106         
107         /* (omit javadoc for this method)
108          * Method declared on ASTNode.
109          */
110         final ASTNode internalGetSetChildProperty(ChildPropertyDescriptor property, boolean get, ASTNode child) {
111                 if (property == LABEL_PROPERTY) {
112                         if (get) {
113                                 return getLabel();
114                         } else {
115                                 setLabel((SimpleName) child);
116                                 return null;
117                         }
118                 }
119                 if (property == BODY_PROPERTY) {
120                         if (get) {
121                                 return getBody();
122                         } else {
123                                 setBody((Statement) 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 int getNodeType0() {
135                 return LABELED_STATEMENT;
136         }
137
138         /* (omit javadoc for this method)
139          * Method declared on ASTNode.
140          */
141         ASTNode clone0(AST target) {
142                 LabeledStatement result = new LabeledStatement(target);
143                 result.setSourceRange(this.getStartPosition(), this.getLength());
144                 result.setLabel(
145                         (SimpleName) ASTNode.copySubtree(target, getLabel()));
146                 result.setBody(
147                         (Statement) ASTNode.copySubtree(target, getBody()));
148                 return result;
149         }
150
151         /* (omit javadoc for this method)
152          * Method declared on ASTNode.
153          */
154         final boolean subtreeMatch0(ASTMatcher matcher, Object other) {
155                 // dispatch to correct overloaded match method
156                 return matcher.match(this, other);
157         }
158
159         /* (omit javadoc for this method)
160          * Method declared on ASTNode.
161          */
162         void accept0(ASTVisitor visitor) {
163                 boolean visitChildren = visitor.visit(this);
164                 if (visitChildren) {
165                         // visit children in normal left to right reading order
166                         acceptChild(visitor, getLabel());
167                         acceptChild(visitor, getBody());
168                 }
169                 visitor.endVisit(this);
170         }
171         
172         /**
173          * Returns the label of this labeled statement.
174          * 
175          * @return the variable name node
176          */ 
177         public SimpleName getLabel() {
178                 if (this.labelName == null) {
179                         // lazy init must be thread-safe for readers
180                         synchronized (this) {
181                                 if (this.labelName == null) {
182                                         preLazyInit();
183                                         this.labelName= new SimpleName(this.ast);
184                                         postLazyInit(this.labelName, LABEL_PROPERTY);
185                                 }
186                         }
187                 }
188                 return this.labelName;
189         }
190                 
191         /**
192          * Sets the label of this labeled statement.
193          * 
194          * @param label the new label
195          * @exception IllegalArgumentException if:
196          * <ul>
197          * <li>the node belongs to a different AST</li>
198          * <li>the node already has a parent</li>
199          * </ul>
200          */ 
201         public void setLabel(SimpleName label) {
202                 if (label == null) {
203                         throw new IllegalArgumentException();
204                 }
205                 ASTNode oldChild = this.labelName;
206                 preReplaceChild(oldChild, label, LABEL_PROPERTY);
207                 this.labelName = label;
208                 postReplaceChild(oldChild, label, LABEL_PROPERTY);
209         }
210         
211         /**
212          * Returns the body of this labeled statement.
213          * 
214          * @return the body statement node
215          */ 
216         public Statement getBody() {
217                 if (this.body == null) {
218                         // lazy init must be thread-safe for readers
219                         synchronized (this) {
220                                 if (this.body == null) {
221                                         preLazyInit();
222                                         this.body= new EmptyStatement(this.ast);
223                                         postLazyInit(this.body, BODY_PROPERTY);
224                                 }
225                         }
226                 }
227                 return this.body;
228         }
229         
230         /**
231          * Sets the body of this labeled statement.
232          * <p>
233          * Special note: The Java language does not allow a local variable declaration
234          * to appear as the body of a labeled statement (they may only appear within a
235          * block). However, the AST will allow a <code>VariableDeclarationStatement</code>
236          * as the body of a <code>LabeledStatement</code>. To get something that will
237          * compile, be sure to embed the <code>VariableDeclarationStatement</code>
238          * inside a <code>Block</code>.
239          * </p>
240          * 
241          * @param statement the body statement node
242          * @exception IllegalArgumentException if:
243          * <ul>
244          * <li>the node belongs to a different AST</li>
245          * <li>the node already has a parent</li>
246          * <li>a cycle in would be created</li>
247          * </ul>
248          */ 
249         public void setBody(Statement statement) {
250                 if (statement == null) {
251                         throw new IllegalArgumentException();
252                 }
253                 ASTNode oldChild = this.body;
254                 preReplaceChild(oldChild, statement, BODY_PROPERTY);
255                 this.body = statement;
256                 postReplaceChild(oldChild, statement, BODY_PROPERTY);
257         }
258         
259         /* (omit javadoc for this method)
260          * Method declared on ASTNode.
261          */
262         int memSize() {
263                 return super.memSize() + 2 * 4;
264         }
265         
266         /* (omit javadoc for this method)
267          * Method declared on ASTNode.
268          */
269         int treeSize() {
270                 return
271                         memSize()
272                         + (this.labelName == null ? 0 : getLabel().treeSize())
273                         + (this.body == null ? 0 : getBody().treeSize());
274         }
275 }
276