KickJava   Java API By Example, From Geeks To Geeks.

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


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  * Labeled statement AST node type.
19  *
20  * <pre>
21  * LabeledStatement:
22  * Identifier <b>:</b> Statement
23  * </pre>
24  *
25  * @since 2.0
26  */

27 public class LabeledStatement extends Statement {
28             
29     /**
30      * The "label" structural property of this node type.
31      * @since 3.0
32      */

33     public static final ChildPropertyDescriptor LABEL_PROPERTY =
34         new ChildPropertyDescriptor(LabeledStatement.class, "label", SimpleName.class, MANDATORY, NO_CYCLE_RISK); //$NON-NLS-1$
35

36     /**
37      * The "body" structural property of this node type.
38      * @since 3.0
39      */

40     public static final ChildPropertyDescriptor BODY_PROPERTY =
41         new ChildPropertyDescriptor(LabeledStatement.class, "body", Statement.class, MANDATORY, CYCLE_RISK); //$NON-NLS-1$
42

43     /**
44      * A list of property descriptors (element type:
45      * {@link StructuralPropertyDescriptor}),
46      * or null if uninitialized.
47      */

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

69     public static List JavaDoc propertyDescriptors(int apiLevel) {
70         return PROPERTY_DESCRIPTORS;
71     }
72             
73     /**
74      * The label; lazily initialized; defaults to a unspecified,
75      * legal Java identifier.
76      */

77     private SimpleName labelName = null;
78
79     /**
80      * The body statement; lazily initialized; defaults to an unspecified, but
81      * legal, statement.
82      */

83     private Statement body = null;
84
85     /**
86      * Creates a new AST node for a labeled statement owned by the given
87      * AST. By default, the statement has an unspecified (but legal) label
88      * and an unspecified (but legal) statement.
89      * <p>
90      * N.B. This constructor is package-private.
91      * </p>
92      *
93      * @param ast the AST that is to own this node
94      */

95     LabeledStatement(AST ast) {
96         super(ast);
97     }
98
99     /* (omit javadoc for this method)
100      * Method declared on ASTNode.
101      */

102     final List JavaDoc internalStructuralPropertiesForType(int apiLevel) {
103         return propertyDescriptors(apiLevel);
104     }
105     
106     /* (omit javadoc for this method)
107      * Method declared on ASTNode.
108      */

109     final ASTNode internalGetSetChildProperty(ChildPropertyDescriptor property, boolean get, ASTNode child) {
110         if (property == LABEL_PROPERTY) {
111             if (get) {
112                 return getLabel();
113             } else {
114                 setLabel((SimpleName) child);
115                 return null;
116             }
117         }
118         if (property == BODY_PROPERTY) {
119             if (get) {
120                 return getBody();
121             } else {
122                 setBody((Statement) child);
123                 return null;
124             }
125         }
126         // allow default implementation to flag the error
127
return super.internalGetSetChildProperty(property, get, child);
128     }
129     
130     /* (omit javadoc for this method)
131      * Method declared on ASTNode.
132      */

133     final int getNodeType0() {
134         return LABELED_STATEMENT;
135     }
136
137     /* (omit javadoc for this method)
138      * Method declared on ASTNode.
139      */

140     ASTNode clone0(AST target) {
141         LabeledStatement result = new LabeledStatement(target);
142         result.setSourceRange(this.getStartPosition(), this.getLength());
143         result.setLabel(
144             (SimpleName) ASTNode.copySubtree(target, getLabel()));
145         result.setBody(
146             (Statement) ASTNode.copySubtree(target, getBody()));
147         return result;
148     }
149
150     /* (omit javadoc for this method)
151      * Method declared on ASTNode.
152      */

153     final boolean subtreeMatch0(ASTMatcher matcher, Object JavaDoc other) {
154         // dispatch to correct overloaded match method
155
return matcher.match(this, other);
156     }
157
158     /* (omit javadoc for this method)
159      * Method declared on ASTNode.
160      */

161     void accept0(ASTVisitor visitor) {
162         boolean visitChildren = visitor.visit(this);
163         if (visitChildren) {
164             // visit children in normal left to right reading order
165
acceptChild(visitor, getLabel());
166             acceptChild(visitor, getBody());
167         }
168         visitor.endVisit(this);
169     }
170     
171     /**
172      * Returns the label of this labeled statement.
173      *
174      * @return the variable name node
175      */

176     public SimpleName getLabel() {
177         if (this.labelName == null) {
178             // lazy init must be thread-safe for readers
179
synchronized (this) {
180                 if (this.labelName == null) {
181                     preLazyInit();
182                     this.labelName= new SimpleName(this.ast);
183                     postLazyInit(this.labelName, LABEL_PROPERTY);
184                 }
185             }
186         }
187         return this.labelName;
188     }
189         
190     /**
191      * Sets the label of this labeled statement.
192      *
193      * @param label the new label
194      * @exception IllegalArgumentException if:
195      * <ul>
196      * <li>the node belongs to a different AST</li>
197      * <li>the node already has a parent</li>
198      * </ul>
199      */

200     public void setLabel(SimpleName label) {
201         if (label == null) {
202             throw new IllegalArgumentException JavaDoc();
203         }
204         ASTNode oldChild = this.labelName;
205         preReplaceChild(oldChild, label, LABEL_PROPERTY);
206         this.labelName = label;
207         postReplaceChild(oldChild, label, LABEL_PROPERTY);
208     }
209     
210     /**
211      * Returns the body of this labeled statement.
212      *
213      * @return the body statement node
214      */

215     public Statement getBody() {
216         if (this.body == null) {
217             // lazy init must be thread-safe for readers
218
synchronized (this) {
219                 if (this.body == null) {
220                     preLazyInit();
221                     this.body= new EmptyStatement(this.ast);
222                     postLazyInit(this.body, BODY_PROPERTY);
223                 }
224             }
225         }
226         return this.body;
227     }
228     
229     /**
230      * Sets the body of this labeled statement.
231      * <p>
232      * Special note: The Java language does not allow a local variable declaration
233      * to appear as the body of a labeled statement (they may only appear within a
234      * block). However, the AST will allow a <code>VariableDeclarationStatement</code>
235      * as the body of a <code>LabeledStatement</code>. To get something that will
236      * compile, be sure to embed the <code>VariableDeclarationStatement</code>
237      * inside a <code>Block</code>.
238      * </p>
239      *
240      * @param statement the body statement node
241      * @exception IllegalArgumentException if:
242      * <ul>
243      * <li>the node belongs to a different AST</li>
244      * <li>the node already has a parent</li>
245      * <li>a cycle in would be created</li>
246      * </ul>
247      */

248     public void setBody(Statement statement) {
249         if (statement == null) {
250             throw new IllegalArgumentException JavaDoc();
251         }
252         ASTNode oldChild = this.body;
253         preReplaceChild(oldChild, statement, BODY_PROPERTY);
254         this.body = statement;
255         postReplaceChild(oldChild, statement, BODY_PROPERTY);
256     }
257     
258     /* (omit javadoc for this method)
259      * Method declared on ASTNode.
260      */

261     int memSize() {
262         return super.memSize() + 2 * 4;
263     }
264     
265     /* (omit javadoc for this method)
266      * Method declared on ASTNode.
267      */

268     int treeSize() {
269         return
270             memSize()
271             + (this.labelName == null ? 0 : getLabel().treeSize())
272             + (this.body == null ? 0 : getBody().treeSize());
273     }
274 }
275
276
Popular Tags