KickJava   Java API By Example, From Geeks To Geeks.

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


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  * Field declaration node type.
19  * <p>
20  * This kind of node collects several variable declaration fragments
21  * (<code>VariableDeclarationFragment</code>) into a single body declaration
22  * (<code>BodyDeclaration</code>), all sharing the same modifiers and base type.
23  * </p>
24  * <pre>
25  * FieldDeclaration:
26  * [Javadoc] { ExtendedModifier } Type VariableDeclarationFragment
27  * { <b>,</b> VariableDeclarationFragment } <b>;</b>
28  * </pre>
29  * <p>
30  * When a Javadoc comment is present, the source range begins with the first
31  * character of the "/**" comment delimiter. When there is no Javadoc comment,
32  * the source range begins with the first character of the initial modifier or
33  * type. The source range extends through the last character of the final ";".
34  * </p>
35  *
36  * @since 2.0
37  */

38 public class FieldDeclaration extends BodyDeclaration {
39     
40     /**
41      * The "javadoc" structural property of this node type.
42      * @since 3.0
43      */

44     public static final ChildPropertyDescriptor JAVADOC_PROPERTY =
45         internalJavadocPropertyFactory(FieldDeclaration.class);
46
47     /**
48      * The "modifiers" structural property of this node type (JLS2 API only).
49      * @since 3.0
50      */

51     public static final SimplePropertyDescriptor MODIFIERS_PROPERTY =
52         internalModifiersPropertyFactory(FieldDeclaration.class);
53     
54     /**
55      * The "modifiers" structural property of this node type (added in JLS3 API).
56      * @since 3.1
57      */

58     public static final ChildListPropertyDescriptor MODIFIERS2_PROPERTY =
59         internalModifiers2PropertyFactory(FieldDeclaration.class);
60     
61     /**
62      * The "type" structural property of this node type.
63      * @since 3.0
64      */

65     public static final ChildPropertyDescriptor TYPE_PROPERTY =
66         new ChildPropertyDescriptor(FieldDeclaration.class, "type", Type.class, MANDATORY, NO_CYCLE_RISK); //$NON-NLS-1$
67

68     /**
69      * The "fragments" structural property of this node type).
70      * @since 3.0
71      */

72     public static final ChildListPropertyDescriptor FRAGMENTS_PROPERTY =
73         new ChildListPropertyDescriptor(FieldDeclaration.class, "fragments", VariableDeclarationFragment.class, CYCLE_RISK); //$NON-NLS-1$
74

75     /**
76      * A list of property descriptors (element type:
77      * {@link StructuralPropertyDescriptor}),
78      * or null if uninitialized.
79      * @since 3.0
80      */

81     private static final List JavaDoc PROPERTY_DESCRIPTORS_2_0;
82     
83     /**
84      * A list of property descriptors (element type:
85      * {@link StructuralPropertyDescriptor}),
86      * or null if uninitialized.
87      * @since 3.1
88      */

89     private static final List JavaDoc PROPERTY_DESCRIPTORS_3_0;
90     
91     static {
92         List JavaDoc properyList = new ArrayList JavaDoc(5);
93         createPropertyList(FieldDeclaration.class, properyList);
94         addProperty(JAVADOC_PROPERTY, properyList);
95         addProperty(MODIFIERS_PROPERTY, properyList);
96         addProperty(TYPE_PROPERTY, properyList);
97         addProperty(FRAGMENTS_PROPERTY, properyList);
98         PROPERTY_DESCRIPTORS_2_0 = reapPropertyList(properyList);
99         
100         properyList = new ArrayList JavaDoc(5);
101         createPropertyList(FieldDeclaration.class, properyList);
102         addProperty(JAVADOC_PROPERTY, properyList);
103         addProperty(MODIFIERS2_PROPERTY, properyList);
104         addProperty(TYPE_PROPERTY, properyList);
105         addProperty(FRAGMENTS_PROPERTY, properyList);
106         PROPERTY_DESCRIPTORS_3_0 = reapPropertyList(properyList);
107     }
108
109     /**
110      * Returns a list of structural property descriptors for this node type.
111      * Clients must not modify the result.
112      *
113      * @param apiLevel the API level; one of the
114      * <code>AST.JLS&ast;</code> constants
115
116      * @return a list of property descriptors (element type:
117      * {@link StructuralPropertyDescriptor})
118      * @since 3.0
119      */

120     public static List JavaDoc propertyDescriptors(int apiLevel) {
121         if (apiLevel == AST.JLS2_INTERNAL) {
122             return PROPERTY_DESCRIPTORS_2_0;
123         } else {
124             return PROPERTY_DESCRIPTORS_3_0;
125         }
126     }
127             
128     /**
129      * The base type; lazily initialized; defaults to an unspecified,
130      * legal type.
131      */

132     private Type baseType = null;
133
134     /**
135      * The list of variable declaration fragments (element type:
136      * <code VariableDeclarationFragment</code>). Defaults to an empty list.
137      */

138     private ASTNode.NodeList variableDeclarationFragments =
139         new ASTNode.NodeList(FRAGMENTS_PROPERTY);
140
141     /**
142      * Creates a new unparented field declaration statement node owned
143      * by the given AST. By default, the field declaration has: no modifiers,
144      * an unspecified (but legal) type, and an empty list of variable
145      * declaration fragments (which is syntactically illegal).
146      * <p>
147      * N.B. This constructor is package-private.
148      * </p>
149      *
150      * @param ast the AST that is to own this node
151      */

152     FieldDeclaration(AST ast) {
153         super(ast);
154     }
155
156     /* (omit javadoc for this method)
157      * Method declared on ASTNode.
158      * @since 3.0
159      */

160     final List JavaDoc internalStructuralPropertiesForType(int apiLevel) {
161         return propertyDescriptors(apiLevel);
162     }
163     
164     /* (omit javadoc for this method)
165      * Method declared on ASTNode.
166      */

167     final int internalGetSetIntProperty(SimplePropertyDescriptor property, boolean get, int value) {
168         if (property == MODIFIERS_PROPERTY) {
169             if (get) {
170                 return getModifiers();
171             } else {
172                 internalSetModifiers(value);
173                 return 0;
174             }
175         }
176         // allow default implementation to flag the error
177
return super.internalGetSetIntProperty(property, get, value);
178     }
179
180     /* (omit javadoc for this method)
181      * Method declared on ASTNode.
182      */

183     final ASTNode internalGetSetChildProperty(ChildPropertyDescriptor property, boolean get, ASTNode child) {
184         if (property == JAVADOC_PROPERTY) {
185             if (get) {
186                 return getJavadoc();
187             } else {
188                 setJavadoc((Javadoc) child);
189                 return null;
190             }
191         }
192         if (property == TYPE_PROPERTY) {
193             if (get) {
194                 return getType();
195             } else {
196                 setType((Type) child);
197                 return null;
198             }
199         }
200         // allow default implementation to flag the error
201
return super.internalGetSetChildProperty(property, get, child);
202     }
203     
204     /* (omit javadoc for this method)
205      * Method declared on ASTNode.
206      */

207     final List JavaDoc internalGetChildListProperty(ChildListPropertyDescriptor property) {
208         if (property == MODIFIERS2_PROPERTY) {
209             return modifiers();
210         }
211         if (property == FRAGMENTS_PROPERTY) {
212             return fragments();
213         }
214         // allow default implementation to flag the error
215
return super.internalGetChildListProperty(property);
216     }
217     
218     /* (omit javadoc for this method)
219      * Method declared on BodyDeclaration.
220      */

221     final ChildPropertyDescriptor internalJavadocProperty() {
222         return JAVADOC_PROPERTY;
223     }
224
225     /* (omit javadoc for this method)
226      * Method declared on BodyDeclaration.
227      */

228     final SimplePropertyDescriptor internalModifiersProperty() {
229         return MODIFIERS_PROPERTY;
230     }
231
232     /* (omit javadoc for this method)
233      * Method declared on BodyDeclaration.
234      */

235     final ChildListPropertyDescriptor internalModifiers2Property() {
236         return MODIFIERS2_PROPERTY;
237     }
238
239     /* (omit javadoc for this method)
240      * Method declared on ASTNode.
241      */

242     final int getNodeType0() {
243         return FIELD_DECLARATION;
244     }
245
246     /* (omit javadoc for this method)
247      * Method declared on ASTNode.
248      */

249     ASTNode clone0(AST target) {
250         FieldDeclaration result = new FieldDeclaration(target);
251         result.setSourceRange(this.getStartPosition(), this.getLength());
252         result.setJavadoc(
253             (Javadoc) ASTNode.copySubtree(target, getJavadoc()));
254         if (this.ast.apiLevel == AST.JLS2_INTERNAL) {
255             result.internalSetModifiers(getModifiers());
256         }
257         if (this.ast.apiLevel >= AST.JLS3) {
258             result.modifiers().addAll(ASTNode.copySubtrees(target, modifiers()));
259         }
260         result.setType((Type) getType().clone(target));
261         result.fragments().addAll(
262             ASTNode.copySubtrees(target, fragments()));
263         return result;
264     }
265     
266     /* (omit javadoc for this method)
267      * Method declared on ASTNode.
268      */

269     final boolean subtreeMatch0(ASTMatcher matcher, Object JavaDoc other) {
270         // dispatch to correct overloaded match method
271
return matcher.match(this, other);
272     }
273     
274     /* (omit javadoc for this method)
275      * Method declared on ASTNode.
276      */

277     void accept0(ASTVisitor visitor) {
278         boolean visitChildren = visitor.visit(this);
279         if (visitChildren) {
280             // visit children in normal left to right reading order
281
acceptChild(visitor, getJavadoc());
282             if (this.ast.apiLevel >= AST.JLS3) {
283                 acceptChildren(visitor, this.modifiers);
284             }
285             acceptChild(visitor, getType());
286             acceptChildren(visitor, this.variableDeclarationFragments);
287         }
288         visitor.endVisit(this);
289     }
290     
291     /**
292      * Returns the base type declared in this field declaration.
293      * <p>
294      * N.B. The individual child variable declaration fragments may specify
295      * additional array dimensions. So the type of the variable are not
296      * necessarily exactly this type.
297      * </p>
298      *
299      * @return the base type
300      */

301     public Type getType() {
302         if (this.baseType == null) {
303             // lazy init must be thread-safe for readers
304
synchronized (this) {
305                 if (this.baseType == null) {
306                     preLazyInit();
307                     this.baseType = this.ast.newPrimitiveType(PrimitiveType.INT);
308                     postLazyInit(this.baseType, TYPE_PROPERTY);
309                 }
310             }
311         }
312         return this.baseType;
313     }
314
315     /**
316      * Sets the base type declared in this field declaration to the given type.
317      *
318      * @param type the new base type
319      * @exception IllegalArgumentException if:
320      * <ul>
321      * <li>the node belongs to a different AST</li>
322      * <li>the node already has a parent</li>
323      * </ul>
324      */

325     public void setType(Type type) {
326         if (type == null) {
327             throw new IllegalArgumentException JavaDoc();
328         }
329         ASTNode oldChild = this.baseType;
330         preReplaceChild(oldChild, type, TYPE_PROPERTY);
331         this.baseType = type;
332         postReplaceChild(oldChild, type, TYPE_PROPERTY);
333     }
334
335     /**
336      * Returns the live list of variable declaration fragments in this field
337      * declaration. Adding and removing nodes from this list affects this node
338      * dynamically. All nodes in this list must be
339      * <code>VariableDeclarationFragment</code>s; attempts to add any other
340      * type of node will trigger an exception.
341      *
342      * @return the live list of variable declaration fragments in this
343      * statement (element type: <code>VariableDeclarationFragment</code>)
344      */

345     public List JavaDoc fragments() {
346         return this.variableDeclarationFragments;
347     }
348         
349     /* (omit javadoc for this method)
350      * Method declared on ASTNode.
351      */

352     int memSize() {
353         return super.memSize() + 2 * 4;
354     }
355     
356     /* (omit javadoc for this method)
357      * Method declared on ASTNode.
358      */

359     int treeSize() {
360         return
361             memSize()
362             + (this.optionalDocComment == null ? 0 : getJavadoc().treeSize())
363             + (this.modifiers == null ? 0 : this.modifiers.listSize())
364             + (this.baseType == null ? 0 : getType().treeSize())
365             + this.variableDeclarationFragments.listSize();
366     }
367 }
368
Popular Tags