KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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  * Variable declaration fragment AST node type, used in field declarations,
19  * local variable declarations, and <code>ForStatement</code> initializers.
20  * It contrast to <code>SingleVariableDeclaration</code>, fragments are
21  * missing the modifiers and the type; these are located in the fragment's
22  * parent node.
23  *
24  * <pre>
25  * VariableDeclarationFragment:
26  * Identifier { <b>[</b><b>]</b> } [ <b>=</b> Expression ]
27  * </pre>
28  *
29  * @since 2.0
30  */

31 public class VariableDeclarationFragment extends VariableDeclaration {
32         
33     /**
34      * The "name" structural property of this node type.
35      * @since 3.0
36      */

37     public static final ChildPropertyDescriptor NAME_PROPERTY =
38         new ChildPropertyDescriptor(VariableDeclarationFragment.class, "name", SimpleName.class, MANDATORY, NO_CYCLE_RISK); //$NON-NLS-1$
39

40     /**
41      * The "extraDimensions" structural property of this node type.
42      * @since 3.0
43      */

44     public static final SimplePropertyDescriptor EXTRA_DIMENSIONS_PROPERTY =
45         new SimplePropertyDescriptor(VariableDeclarationFragment.class, "extraDimensions", int.class, MANDATORY); //$NON-NLS-1$
46

47     /**
48      * The "initializer" structural property of this node type.
49      * @since 3.0
50      */

51     public static final ChildPropertyDescriptor INITIALIZER_PROPERTY =
52         new ChildPropertyDescriptor(VariableDeclarationFragment.class, "initializer", Expression.class, OPTIONAL, CYCLE_RISK); //$NON-NLS-1$
53

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

60     private static final List JavaDoc PROPERTY_DESCRIPTORS;
61         
62     static {
63         List JavaDoc propertyList = new ArrayList JavaDoc(4);
64         createPropertyList(VariableDeclarationFragment.class, propertyList);
65         addProperty(NAME_PROPERTY, propertyList);
66         addProperty(EXTRA_DIMENSIONS_PROPERTY, propertyList);
67         addProperty(INITIALIZER_PROPERTY, propertyList);
68         PROPERTY_DESCRIPTORS = reapPropertyList(propertyList);
69     }
70
71     /**
72      * Returns a list of structural property descriptors for this node type.
73      * Clients must not modify the result.
74      *
75      * @param apiLevel the API level; one of the
76      * <code>AST.JLS*</code> constants
77      * @return a list of property descriptors (element type:
78      * {@link StructuralPropertyDescriptor})
79      * @since 3.0
80      */

81     public static List JavaDoc propertyDescriptors(int apiLevel) {
82         return PROPERTY_DESCRIPTORS;
83     }
84             
85     /**
86      * The variable name; lazily initialized; defaults to an unspecified,
87      * legal Java identifier.
88      */

89     private SimpleName variableName = null;
90
91     /**
92      * The number of extra array dimensions that this variable has;
93      * defaults to 0.
94      */

95     private int extraArrayDimensions = 0;
96
97     /**
98      * The initializer expression, or <code>null</code> if none;
99      * defaults to none.
100      */

101     private Expression optionalInitializer = null;
102     
103     /**
104      * Creates a new AST node for a variable declaration fragment owned by the
105      * given AST. By default, the variable declaration has: an unspecified
106      * (but legal) variable name, no initializer, and no extra array dimensions.
107      * <p>
108      * N.B. This constructor is package-private.
109      * </p>
110      *
111      * @param ast the AST that is to own this node
112      */

113     VariableDeclarationFragment(AST ast) {
114         super(ast);
115     }
116
117     /* (omit javadoc for this method)
118      * Method declared on VariableDeclaration.
119      * @since 3.1
120      */

121     final SimplePropertyDescriptor internalExtraDimensionsProperty() {
122         return EXTRA_DIMENSIONS_PROPERTY;
123     }
124
125     /* (omit javadoc for this method)
126      * Method declared on VariableDeclaration.
127      * @since 3.1
128      */

129     final ChildPropertyDescriptor internalInitializerProperty() {
130         return INITIALIZER_PROPERTY;
131     }
132
133     /* (omit javadoc for this method)
134      * Method declared on VariableDeclaration.
135      * @since 3.1
136      */

137     final ChildPropertyDescriptor internalNameProperty() {
138         return NAME_PROPERTY;
139     }
140
141     /* (omit javadoc for this method)
142      * Method declared on ASTNode.
143      */

144     final List JavaDoc internalStructuralPropertiesForType(int apiLevel) {
145         return propertyDescriptors(apiLevel);
146     }
147     
148     /* (omit javadoc for this method)
149      * Method declared on ASTNode.
150      */

151     final int internalGetSetIntProperty(SimplePropertyDescriptor property, boolean get, int value) {
152         if (property == EXTRA_DIMENSIONS_PROPERTY) {
153             if (get) {
154                 return getExtraDimensions();
155             } else {
156                 setExtraDimensions(value);
157                 return 0;
158             }
159         }
160         // allow default implementation to flag the error
161
return super.internalGetSetIntProperty(property, get, value);
162     }
163     
164     /* (omit javadoc for this method)
165      * Method declared on ASTNode.
166      */

167     final ASTNode internalGetSetChildProperty(ChildPropertyDescriptor property, boolean get, ASTNode child) {
168         if (property == NAME_PROPERTY) {
169             if (get) {
170                 return getName();
171             } else {
172                 setName((SimpleName) child);
173                 return null;
174             }
175         }
176         if (property == INITIALIZER_PROPERTY) {
177             if (get) {
178                 return getInitializer();
179             } else {
180                 setInitializer((Expression) child);
181                 return null;
182             }
183         }
184         // allow default implementation to flag the error
185
return super.internalGetSetChildProperty(property, get, child);
186     }
187     
188     /* (omit javadoc for this method)
189      * Method declared on ASTNode.
190      */

191     final int getNodeType0() {
192         return VARIABLE_DECLARATION_FRAGMENT;
193     }
194
195     /* (omit javadoc for this method)
196      * Method declared on ASTNode.
197      */

198     ASTNode clone0(AST target) {
199         VariableDeclarationFragment result = new VariableDeclarationFragment(target);
200         result.setSourceRange(this.getStartPosition(), this.getLength());
201         result.setName((SimpleName) getName().clone(target));
202         result.setExtraDimensions(getExtraDimensions());
203         result.setInitializer(
204             (Expression) ASTNode.copySubtree(target, getInitializer()));
205         return result;
206     }
207
208     /* (omit javadoc for this method)
209      * Method declared on ASTNode.
210      */

211     final boolean subtreeMatch0(ASTMatcher matcher, Object JavaDoc other) {
212         // dispatch to correct overloaded match method
213
return matcher.match(this, other);
214     }
215
216     /* (omit javadoc for this method)
217      * Method declared on ASTNode.
218      */

219     void accept0(ASTVisitor visitor) {
220         boolean visitChildren = visitor.visit(this);
221         if (visitChildren) {
222             // visit children in normal left to right reading order
223
acceptChild(visitor, getName());
224             acceptChild(visitor, getInitializer());
225         }
226         visitor.endVisit(this);
227     }
228     
229     /* (omit javadoc for this method)
230      * Method declared on VariableDeclaration.
231      */

232     public SimpleName getName() {
233         if (this.variableName == null) {
234             // lazy init must be thread-safe for readers
235
synchronized (this) {
236                 if (this.variableName == null) {
237                     preLazyInit();
238                     this.variableName = new SimpleName(this.ast);
239                     postLazyInit(this.variableName, NAME_PROPERTY);
240                 }
241             }
242         }
243         return this.variableName;
244     }
245         
246     /* (omit javadoc for this method)
247      * Method declared on VariableDeclaration.
248      */

249     public void setName(SimpleName variableName) {
250         if (variableName == null) {
251             throw new IllegalArgumentException JavaDoc();
252         }
253         ASTNode oldChild = this.variableName;
254         preReplaceChild(oldChild, variableName, NAME_PROPERTY);
255         this.variableName = variableName;
256         postReplaceChild(oldChild, variableName, NAME_PROPERTY);
257     }
258
259     /**
260      * Returns the number of extra array dimensions this variable has over
261      * and above the type specified in the enclosing declaration.
262      * <p>
263      * For example, in the AST for <code>int[] i, j[], k[][]</code> the
264      * variable declaration fragments for the variables <code>i</code>,
265      * <code>j</code>, and <code>k</code>, have 0, 1, and 2 extra array
266      * dimensions, respectively.
267      * </p>
268      *
269      * @return the number of extra array dimensions this variable has over
270      * and above the type specified in the enclosing declaration
271      * @since 2.0
272      */

273     public int getExtraDimensions() {
274         return this.extraArrayDimensions;
275     }
276
277     /**
278      * Sets the number of extra array dimensions this variable has over
279      * and above the type specified in the enclosing declaration.
280      * <p>
281      * For example, in the AST for <code>int[] i, j[], k[][]</code> the
282      * variable declaration fragments for the variables <code>i</code>,
283      * <code>j</code>, and <code>k</code>, have 0, 1, and 2 extra array
284      * dimensions, respectively.
285      * </p>
286      *
287      * @param dimensions the given dimensions
288      * @since 2.0
289      */

290     public void setExtraDimensions(int dimensions) {
291         if (dimensions < 0) {
292             throw new IllegalArgumentException JavaDoc();
293         }
294         preValueChange(EXTRA_DIMENSIONS_PROPERTY);
295         this.extraArrayDimensions = dimensions;
296         postValueChange(EXTRA_DIMENSIONS_PROPERTY);
297     }
298
299     /* (omit javadoc for this method)
300      * Method declared on VariableDeclaration.
301      */

302     public Expression getInitializer() {
303         return this.optionalInitializer;
304     }
305     
306     /* (omit javadoc for this method)
307      * Method declared on VariableDeclaration.
308      */

309     public void setInitializer(Expression initializer) {
310         ASTNode oldChild = this.optionalInitializer;
311         preReplaceChild(oldChild, initializer, INITIALIZER_PROPERTY);
312         this.optionalInitializer = initializer;
313         postReplaceChild(oldChild, initializer, INITIALIZER_PROPERTY);
314     }
315
316     /* (omit javadoc for this method)
317      * Method declared on ASTNode.
318      */

319     int memSize() {
320         // treat Operator as free
321
return BASE_NODE_SIZE + 3 * 4;
322     }
323     
324     /* (omit javadoc for this method)
325      * Method declared on ASTNode.
326      */

327     int treeSize() {
328         return
329             memSize()
330             + (this.variableName == null ? 0 : getName().treeSize())
331             + (this.optionalInitializer == null ? 0 : getInitializer().treeSize());
332     }
333 }
334
Popular Tags