KickJava   Java API By Example, From Geeks To Geeks.

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


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.Iterator JavaDoc;
16 import java.util.List JavaDoc;
17
18 /**
19  * Local variable declaration statement AST node type.
20  * <p>
21  * This kind of node collects several variable declaration fragments
22  * (<code>VariableDeclarationFragment</code>) into a statement
23  * (<code>Statement</code>), all sharing the same modifiers and base type.
24  * </p>
25  * For JLS2:
26  * <pre>
27  * VariableDeclarationStatement:
28  * { Modifier } Type VariableDeclarationFragment
29  * { <b>,</b> VariableDeclarationFragment } <b>;</b>
30  * </pre>
31  * For JLS3, the modifier flags were replaced by
32  * a list of modifier nodes (intermixed with annotations):
33  * <pre>
34  * VariableDeclarationStatement:
35  * { ExtendedModifier } Type VariableDeclarationFragment
36  * { <b>,</b> VariableDeclarationFragment } <b>;</b>
37  * </pre>
38  * <p>
39  * Note: This type of node is a convenience of sorts.
40  * An equivalent way to represent the same statement is to use
41  * a <code>VariableDeclarationExpression</code>
42  * wrapped in an <code>ExpressionStatement</code>.
43  * </p>
44  *
45  * @since 2.0
46  */

47 public class VariableDeclarationStatement extends Statement {
48     
49     /**
50      * The "modifiers" structural property of this node type (JLS2 API only).
51      * @since 3.0
52      */

53     public static final SimplePropertyDescriptor MODIFIERS_PROPERTY =
54         new SimplePropertyDescriptor(VariableDeclarationStatement.class, "modifiers", int.class, MANDATORY); //$NON-NLS-1$
55

56     /**
57      * The "modifiers" structural property of this node type (added in JLS3 API).
58      * @since 3.1
59      */

60     public static final ChildListPropertyDescriptor MODIFIERS2_PROPERTY =
61         new ChildListPropertyDescriptor(VariableDeclarationStatement.class, "modifiers", IExtendedModifier.class, CYCLE_RISK); //$NON-NLS-1$
62

63     /**
64      * The "type" structural property of this node type.
65      * @since 3.0
66      */

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

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

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

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

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

91     private static final List JavaDoc PROPERTY_DESCRIPTORS_3_0;
92     
93     static {
94         List JavaDoc propertyList = new ArrayList JavaDoc(4);
95         createPropertyList(VariableDeclarationStatement.class, propertyList);
96         addProperty(MODIFIERS_PROPERTY, propertyList);
97         addProperty(TYPE_PROPERTY, propertyList);
98         addProperty(FRAGMENTS_PROPERTY, propertyList);
99         PROPERTY_DESCRIPTORS_2_0 = reapPropertyList(propertyList);
100         
101         propertyList = new ArrayList JavaDoc(4);
102         createPropertyList(VariableDeclarationStatement.class, propertyList);
103         addProperty(MODIFIERS2_PROPERTY, propertyList);
104         addProperty(TYPE_PROPERTY, propertyList);
105         addProperty(FRAGMENTS_PROPERTY, propertyList);
106         PROPERTY_DESCRIPTORS_3_0 = reapPropertyList(propertyList);
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 extended modifiers (element type: <code>IExtendedModifier</code>).
130      * Null in JLS2. Added in JLS3; defaults to an empty list
131      * (see constructor).
132      * @since 3.1
133      */

134     private ASTNode.NodeList modifiers = null;
135     
136     /**
137      * The modifier flagss; bit-wise or of Modifier flags.
138      * Defaults to none. Not used in JLS3.
139      */

140     private int modifierFlags = Modifier.NONE;
141         
142     /**
143      * The base type; lazily initialized; defaults to an unspecified,
144      * legal type.
145      */

146     private Type baseType = null;
147
148     /**
149      * The list of variable variable declaration fragments (element type:
150      * <code VariableDeclarationFragment</code>). Defaults to an empty list.
151      */

152     private ASTNode.NodeList variableDeclarationFragments =
153         new ASTNode.NodeList(FRAGMENTS_PROPERTY);
154
155     /**
156      * Creates a new unparented local variable declaration statement node owned
157      * by the given AST. By default, the variable declaration has: no modifiers,
158      * an unspecified (but legal) type, and an empty list of variable
159      * declaration fragments (which is syntactically illegal).
160      * <p>
161      * N.B. This constructor is package-private.
162      * </p>
163      *
164      * @param ast the AST that is to own this node
165      */

166     VariableDeclarationStatement(AST ast) {
167         super(ast);
168         if (ast.apiLevel >= AST.JLS3) {
169             this.modifiers = new ASTNode.NodeList(MODIFIERS2_PROPERTY);
170         }
171     }
172
173     /* (omit javadoc for this method)
174      * Method declared on ASTNode.
175      */

176     final List JavaDoc internalStructuralPropertiesForType(int apiLevel) {
177         return propertyDescriptors(apiLevel);
178     }
179     
180     /* (omit javadoc for this method)
181      * Method declared on ASTNode.
182      */

183     final int internalGetSetIntProperty(SimplePropertyDescriptor property, boolean get, int value) {
184         if (property == MODIFIERS_PROPERTY) {
185             if (get) {
186                 return getModifiers();
187             } else {
188                 setModifiers(value);
189                 return 0;
190             }
191         }
192         // allow default implementation to flag the error
193
return super.internalGetSetIntProperty(property, get, value);
194     }
195     
196     /* (omit javadoc for this method)
197      * Method declared on ASTNode.
198      */

199     final ASTNode internalGetSetChildProperty(ChildPropertyDescriptor property, boolean get, ASTNode child) {
200         if (property == TYPE_PROPERTY) {
201             if (get) {
202                 return getType();
203             } else {
204                 setType((Type) child);
205                 return null;
206             }
207         }
208         // allow default implementation to flag the error
209
return super.internalGetSetChildProperty(property, get, child);
210     }
211     
212     /* (omit javadoc for this method)
213      * Method declared on ASTNode.
214      */

215     final List JavaDoc internalGetChildListProperty(ChildListPropertyDescriptor property) {
216         if (property == MODIFIERS2_PROPERTY) {
217             return modifiers();
218         }
219         if (property == FRAGMENTS_PROPERTY) {
220             return fragments();
221         }
222         // allow default implementation to flag the error
223
return super.internalGetChildListProperty(property);
224     }
225         
226     /* (omit javadoc for this method)
227      * Method declared on ASTNode.
228      */

229     final int getNodeType0() {
230         return VARIABLE_DECLARATION_STATEMENT;
231     }
232
233     /* (omit javadoc for this method)
234      * Method declared on ASTNode.
235      */

236     ASTNode clone0(AST target) {
237         VariableDeclarationStatement result =
238             new VariableDeclarationStatement(target);
239         result.setSourceRange(this.getStartPosition(), this.getLength());
240         result.copyLeadingComment(this);
241         if (this.ast.apiLevel == AST.JLS2_INTERNAL) {
242             result.setModifiers(getModifiers());
243         }
244         if (this.ast.apiLevel >= AST.JLS3) {
245             result.modifiers().addAll(ASTNode.copySubtrees(target, modifiers()));
246         }
247         result.setType((Type) getType().clone(target));
248         result.fragments().addAll(
249             ASTNode.copySubtrees(target, fragments()));
250         return result;
251     }
252     
253     /* (omit javadoc for this method)
254      * Method declared on ASTNode.
255      */

256     final boolean subtreeMatch0(ASTMatcher matcher, Object JavaDoc other) {
257         // dispatch to correct overloaded match method
258
return matcher.match(this, other);
259     }
260
261     /* (omit javadoc for this method)
262      * Method declared on ASTNode.
263      */

264     void accept0(ASTVisitor visitor) {
265         boolean visitChildren = visitor.visit(this);
266         if (visitChildren) {
267             // visit children in normal left to right reading order
268
if (this.ast.apiLevel >= AST.JLS3) {
269                 acceptChildren(visitor, this.modifiers);
270             }
271             acceptChild(visitor, getType());
272             acceptChildren(visitor, this.variableDeclarationFragments);
273         }
274         visitor.endVisit(this);
275     }
276     
277     /**
278      * Returns the live ordered list of modifiers and annotations
279      * of this declaration (added in JLS3 API).
280      * <p>
281      * Note that the final modifier is the only meaningful modifier for local
282      * variable declarations.
283      * </p>
284      *
285      * @return the live list of modifiers and annotations
286      * (element type: <code>IExtendedModifier</code>)
287      * @exception UnsupportedOperationException if this operation is used in
288      * a JLS2 AST
289      * @since 3.1
290      */

291     public List JavaDoc modifiers() {
292         // more efficient than just calling unsupportedIn2() to check
293
if (this.modifiers == null) {
294             unsupportedIn2();
295         }
296         return this.modifiers;
297     }
298     
299     /**
300      * Returns the modifiers explicitly specified on this declaration.
301      * <p>
302      * In the JLS3 API, this method is a convenience method that
303      * computes these flags from <code>modifiers()</code>.
304      * </p>
305      *
306      * @return the bit-wise or of <code>Modifier</code> constants
307      * @see Modifier
308      */

309     public int getModifiers() {
310         // more efficient than checking getAST().API_LEVEL
311
if (this.modifiers == null) {
312             // JLS2 behavior - bona fide property
313
return this.modifierFlags;
314         } else {
315             // JLS3 behavior - convenience method
316
// performance could be improved by caching computed flags
317
// but this would require tracking changes to this.modifiers
318
int computedModifierFlags = Modifier.NONE;
319             for (Iterator JavaDoc it = modifiers().iterator(); it.hasNext(); ) {
320                 Object JavaDoc x = it.next();
321                 if (x instanceof Modifier) {
322                     computedModifierFlags |= ((Modifier) x).getKeyword().toFlagValue();
323                 }
324             }
325             return computedModifierFlags;
326         }
327     }
328
329     /**
330      * Sets the modifiers explicitly specified on this declaration (JLS2 API only).
331      * <p>
332      * Note that the final modifier is the only meaningful modifier for local
333      * variable declarations.
334      * </p>
335      *
336      * @param modifiers the given modifiers (bit-wise or of <code>Modifier</code> constants)
337      * @exception UnsupportedOperationException if this operation is used in
338      * an AST later than JLS2
339      * @see Modifier
340      * @deprecated In the JLS3 API, this method is replaced by
341      * {@link #modifiers()} which contains a list of a <code>Modifier</code> nodes.
342      */

343     public void setModifiers(int modifiers) {
344         internalSetModifiers(modifiers);
345     }
346     
347     /**
348      * Internal synonym for deprecated method. Used to avoid
349      * deprecation warnings.
350      * @since 3.1
351      */

352     /*package*/ final void internalSetModifiers(int pmodifiers) {
353         supportedOnlyIn2();
354         preValueChange(MODIFIERS_PROPERTY);
355         this.modifierFlags = pmodifiers;
356         postValueChange(MODIFIERS_PROPERTY);
357     }
358
359     /**
360      * Returns the base type declared in this variable declaration statement.
361      * <p>
362      * N.B. The individual child variable declaration fragments may specify
363      * additional array dimensions. So the type of the variable are not
364      * necessarily exactly this type.
365      * </p>
366      *
367      * @return the base type
368      */

369     public Type getType() {
370         if (this.baseType == null) {
371             // lazy init must be thread-safe for readers
372
synchronized (this) {
373                 if (this.baseType == null) {
374                     preLazyInit();
375                     this.baseType = this.ast.newPrimitiveType(PrimitiveType.INT);
376                     postLazyInit(this.baseType, TYPE_PROPERTY);
377                 }
378             }
379         }
380         return this.baseType;
381     }
382
383     /**
384      * Sets the base type declared in this variable declaration statement to
385      * the given type.
386      *
387      * @param type the new base type
388      * @exception IllegalArgumentException if:
389      * <ul>
390      * <li>the node belongs to a different AST</li>
391      * <li>the node already has a parent</li>
392      * </ul>
393      */

394     public void setType(Type type) {
395         if (type == null) {
396             throw new IllegalArgumentException JavaDoc();
397         }
398         ASTNode oldChild = this.baseType;
399         preReplaceChild(oldChild, type, TYPE_PROPERTY);
400         this.baseType = type;
401         postReplaceChild(oldChild, type, TYPE_PROPERTY);
402     }
403
404     /**
405      * Returns the live list of variable declaration fragments in this statement.
406      * Adding and removing nodes from this list affects this node dynamically.
407      * All nodes in this list must be <code>VariableDeclarationFragment</code>s;
408      * attempts to add any other type of node will trigger an
409      * exception.
410      *
411      * @return the live list of variable declaration fragments in this
412      * statement (element type: <code>VariableDeclarationFragment</code>)
413      */

414     public List JavaDoc fragments() {
415         return this.variableDeclarationFragments;
416     }
417     
418     /* (omit javadoc for this method)
419      * Method declared on ASTNode.
420      */

421     int memSize() {
422         return super.memSize() + 4 * 4;
423     }
424     
425     /* (omit javadoc for this method)
426      * Method declared on ASTNode.
427      */

428     int treeSize() {
429         return
430             memSize()
431             + (this.modifiers == null ? 0 : this.modifiers.listSize())
432             + (this.baseType == null ? 0 : getType().treeSize())
433             + this.variableDeclarationFragments.listSize();
434     }
435 }
436
437
Popular Tags