KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 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  * Static or instance initializer AST node type.
19  * <pre>
20  * Initializer:
21  * [ <b>static</b> ] Block
22  * </pre>
23  *
24  * @since 2.0
25  */

26 public class Initializer extends BodyDeclaration {
27     
28     /**
29      * The "javadoc" structural property of this node type.
30      * @since 3.0
31      */

32     public static final ChildPropertyDescriptor JAVADOC_PROPERTY =
33         internalJavadocPropertyFactory(Initializer.class);
34
35     /**
36      * The "modifiers" structural property of this node type (JLS2 API only).
37      * @since 3.0
38      */

39     public static final SimplePropertyDescriptor MODIFIERS_PROPERTY =
40         internalModifiersPropertyFactory(Initializer.class);
41     
42     /**
43      * The "modifiers" structural property of this node type (added in JLS3 API).
44      * @since 3.1
45      */

46     public static final ChildListPropertyDescriptor MODIFIERS2_PROPERTY =
47         internalModifiers2PropertyFactory(Initializer.class);
48     
49     /**
50      * The "body" structural property of this node type.
51      * @since 3.0
52      */

53     public static final ChildPropertyDescriptor BODY_PROPERTY =
54         new ChildPropertyDescriptor(Initializer.class, "body", Block.class, MANDATORY, CYCLE_RISK); //$NON-NLS-1$
55

56     /**
57      * A list of property descriptors (element type:
58      * {@link StructuralPropertyDescriptor}),
59      * or null if uninitialized.
60      * @since 3.0
61      */

62     private static final List JavaDoc PROPERTY_DESCRIPTORS_2_0;
63     
64     /**
65      * A list of property descriptors (element type:
66      * {@link StructuralPropertyDescriptor}),
67      * or null if uninitialized.
68      * @since 3.1
69      */

70     private static final List JavaDoc PROPERTY_DESCRIPTORS_3_0;
71     
72     static {
73         List JavaDoc properyList = new ArrayList JavaDoc(4);
74         createPropertyList(Initializer.class, properyList);
75         addProperty(JAVADOC_PROPERTY, properyList);
76         addProperty(MODIFIERS_PROPERTY, properyList);
77         addProperty(BODY_PROPERTY, properyList);
78         PROPERTY_DESCRIPTORS_2_0 = reapPropertyList(properyList);
79         
80         properyList = new ArrayList JavaDoc(4);
81         createPropertyList(Initializer.class, properyList);
82         addProperty(JAVADOC_PROPERTY, properyList);
83         addProperty(MODIFIERS2_PROPERTY, properyList);
84         addProperty(BODY_PROPERTY, properyList);
85         PROPERTY_DESCRIPTORS_3_0 = reapPropertyList(properyList);
86     }
87
88     /**
89      * Returns a list of structural property descriptors for this node type.
90      * Clients must not modify the result.
91      *
92      * @param apiLevel the API level; one of the
93      * <code>AST.JLS&ast;</code> constants
94
95      * @return a list of property descriptors (element type:
96      * {@link StructuralPropertyDescriptor})
97      * @since 3.0
98      */

99     public static List JavaDoc propertyDescriptors(int apiLevel) {
100         if (apiLevel == AST.JLS2_INTERNAL) {
101             return PROPERTY_DESCRIPTORS_2_0;
102         } else {
103             return PROPERTY_DESCRIPTORS_3_0;
104         }
105     }
106             
107     /**
108      * The initializer body; lazily initialized; defaults to an empty block.
109      */

110     private Block body = null;
111
112     /**
113      * Creates a new AST node for an initializer declaration owned by the given
114      * AST. By default, the initializer has no modifiers and an empty block.
115      * The javadoc comment is not used for initializers.
116      * <p>
117      * N.B. This constructor is package-private.
118      * </p>
119      *
120      * @param ast the AST that is to own this node
121      */

122     Initializer(AST ast) {
123         super(ast);
124     }
125
126     /* (omit javadoc for this method)
127      * Method declared on ASTNode.
128      * @since 3.0
129      */

130     final List JavaDoc internalStructuralPropertiesForType(int apiLevel) {
131         return propertyDescriptors(apiLevel);
132     }
133     
134     /* (omit javadoc for this method)
135      * Method declared on ASTNode.
136      */

137     final int internalGetSetIntProperty(SimplePropertyDescriptor property, boolean get, int value) {
138         if (property == MODIFIERS_PROPERTY) {
139             if (get) {
140                 return getModifiers();
141             } else {
142                 internalSetModifiers(value);
143                 return 0;
144             }
145         }
146         // allow default implementation to flag the error
147
return super.internalGetSetIntProperty(property, get, value);
148     }
149
150     /* (omit javadoc for this method)
151      * Method declared on ASTNode.
152      */

153     final ASTNode internalGetSetChildProperty(ChildPropertyDescriptor property, boolean get, ASTNode child) {
154         if (property == JAVADOC_PROPERTY) {
155             if (get) {
156                 return getJavadoc();
157             } else {
158                 setJavadoc((Javadoc) child);
159                 return null;
160             }
161         }
162         if (property == BODY_PROPERTY) {
163             if (get) {
164                 return getBody();
165             } else {
166                 setBody((Block) child);
167                 return null;
168             }
169         }
170         // allow default implementation to flag the error
171
return super.internalGetSetChildProperty(property, get, child);
172     }
173     
174     /* (omit javadoc for this method)
175      * Method declared on ASTNode.
176      */

177     final List JavaDoc internalGetChildListProperty(ChildListPropertyDescriptor property) {
178         if (property == MODIFIERS2_PROPERTY) {
179             return modifiers();
180         }
181         // allow default implementation to flag the error
182
return super.internalGetChildListProperty(property);
183     }
184     
185     /* (omit javadoc for this method)
186      * Method declared on BodyDeclaration.
187      */

188     final ChildPropertyDescriptor internalJavadocProperty() {
189         return JAVADOC_PROPERTY;
190     }
191
192     /* (omit javadoc for this method)
193      * Method declared on BodyDeclaration.
194      */

195     final ChildListPropertyDescriptor internalModifiers2Property() {
196         return MODIFIERS2_PROPERTY;
197     }
198
199     /* (omit javadoc for this method)
200      * Method declared on BodyDeclaration.
201      */

202     final SimplePropertyDescriptor internalModifiersProperty() {
203         return MODIFIERS_PROPERTY;
204     }
205
206     /* (omit javadoc for this method)
207      * Method declared on ASTNode.
208      */

209     final int getNodeType0() {
210         return INITIALIZER;
211     }
212
213     /* (omit javadoc for this method)
214      * Method declared on ASTNode.
215      */

216     ASTNode clone0(AST target) {
217         Initializer result = new Initializer(target);
218         result.setSourceRange(this.getStartPosition(), this.getLength());
219         if (this.ast.apiLevel == AST.JLS2_INTERNAL) {
220             result.internalSetModifiers(getModifiers());
221         }
222         if (this.ast.apiLevel >= AST.JLS3) {
223             result.modifiers().addAll(ASTNode.copySubtrees(target, modifiers()));
224         }
225         result.setJavadoc(
226             (Javadoc) ASTNode.copySubtree(target, getJavadoc()));
227         result.setBody((Block) getBody().clone(target));
228         return result;
229     }
230
231     /* (omit javadoc for this method)
232      * Method declared on ASTNode.
233      */

234     final boolean subtreeMatch0(ASTMatcher matcher, Object JavaDoc other) {
235         // dispatch to correct overloaded match method
236
return matcher.match(this, other);
237     }
238
239     /* (omit javadoc for this method)
240      * Method declared on ASTNode.
241      */

242     void accept0(ASTVisitor visitor) {
243         boolean visitChildren = visitor.visit(this);
244         if (visitChildren) {
245             acceptChild(visitor, getJavadoc());
246             if (this.ast.apiLevel >= AST.JLS3) {
247                 acceptChildren(visitor, this.modifiers);
248             }
249             acceptChild(visitor, getBody());
250         }
251         visitor.endVisit(this);
252     }
253     
254     /**
255      * Returns the body of this initializer declaration.
256      *
257      * @return the initializer body
258      */

259     public Block getBody() {
260         if (this.body == null) {
261             // lazy init must be thread-safe for readers
262
synchronized (this) {
263                 if (this.body == null) {
264                     preLazyInit();
265                     this.body= new Block(this.ast);
266                     postLazyInit(this.body, BODY_PROPERTY);
267                 }
268             }
269         }
270         return this.body;
271     }
272     
273     /**
274      * Sets the body of this initializer declaration.
275      *
276      * @param body the block node
277      * @exception IllegalArgumentException if:
278      * <ul>
279      * <li>the node belongs to a different AST</li>
280      * <li>the node already has a parent</li>
281      * <li>a cycle in would be created</li>
282      * </ul>
283      */

284     public void setBody(Block body) {
285         if (body == null) {
286             throw new IllegalArgumentException JavaDoc();
287         }
288         ASTNode oldChild = this.body;
289         preReplaceChild(oldChild, body, BODY_PROPERTY);
290         this.body = body;
291         postReplaceChild(oldChild, body, BODY_PROPERTY);
292     }
293     
294     /* (omit javadoc for this method)
295      * Method declared on ASTNode.
296      */

297     int memSize() {
298         return super.memSize() + 1 * 4;
299     }
300     
301     /* (omit javadoc for this method)
302      * Method declared on ASTNode.
303      */

304     int treeSize() {
305         return
306             memSize()
307             + (this.optionalDocComment == null ? 0 : getJavadoc().treeSize())
308             + (this.modifiers == null ? 0 : this.modifiers.listSize())
309             + (this.body == null ? 0 : getBody().treeSize());
310     }
311 }
312
313
Popular Tags