001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.jdt.core.dom;
011:
012: import java.util.ArrayList;
013: import java.util.List;
014:
015: /**
016: * Variable declaration fragment AST node type, used in field declarations,
017: * local variable declarations, and <code>ForStatement</code> initializers.
018: * It contrast to <code>SingleVariableDeclaration</code>, fragments are
019: * missing the modifiers and the type; these are located in the fragment's
020: * parent node.
021: *
022: * <pre>
023: * VariableDeclarationFragment:
024: * Identifier { <b>[</b><b>]</b> } [ <b>=</b> Expression ]
025: * </pre>
026: *
027: * @since 2.0
028: */
029: public class VariableDeclarationFragment extends VariableDeclaration {
030:
031: /**
032: * The "name" structural property of this node type.
033: * @since 3.0
034: */
035: public static final ChildPropertyDescriptor NAME_PROPERTY = new ChildPropertyDescriptor(
036: VariableDeclarationFragment.class,
037: "name", SimpleName.class, MANDATORY, NO_CYCLE_RISK); //$NON-NLS-1$
038:
039: /**
040: * The "extraDimensions" structural property of this node type.
041: * @since 3.0
042: */
043: public static final SimplePropertyDescriptor EXTRA_DIMENSIONS_PROPERTY = new SimplePropertyDescriptor(
044: VariableDeclarationFragment.class,
045: "extraDimensions", int.class, MANDATORY); //$NON-NLS-1$
046:
047: /**
048: * The "initializer" structural property of this node type.
049: * @since 3.0
050: */
051: public static final ChildPropertyDescriptor INITIALIZER_PROPERTY = new ChildPropertyDescriptor(
052: VariableDeclarationFragment.class,
053: "initializer", Expression.class, OPTIONAL, CYCLE_RISK); //$NON-NLS-1$
054:
055: /**
056: * A list of property descriptors (element type:
057: * {@link StructuralPropertyDescriptor}),
058: * or null if uninitialized.
059: * @since 3.0
060: */
061: private static final List PROPERTY_DESCRIPTORS;
062:
063: static {
064: List propertyList = new ArrayList(4);
065: createPropertyList(VariableDeclarationFragment.class,
066: propertyList);
067: addProperty(NAME_PROPERTY, propertyList);
068: addProperty(EXTRA_DIMENSIONS_PROPERTY, propertyList);
069: addProperty(INITIALIZER_PROPERTY, propertyList);
070: PROPERTY_DESCRIPTORS = reapPropertyList(propertyList);
071: }
072:
073: /**
074: * Returns a list of structural property descriptors for this node type.
075: * Clients must not modify the result.
076: *
077: * @param apiLevel the API level; one of the
078: * <code>AST.JLS*</code> constants
079: * @return a list of property descriptors (element type:
080: * {@link StructuralPropertyDescriptor})
081: * @since 3.0
082: */
083: public static List propertyDescriptors(int apiLevel) {
084: return PROPERTY_DESCRIPTORS;
085: }
086:
087: /**
088: * The variable name; lazily initialized; defaults to an unspecified,
089: * legal Java identifier.
090: */
091: private SimpleName variableName = null;
092:
093: /**
094: * The number of extra array dimensions that this variable has;
095: * defaults to 0.
096: */
097: private int extraArrayDimensions = 0;
098:
099: /**
100: * The initializer expression, or <code>null</code> if none;
101: * defaults to none.
102: */
103: private Expression optionalInitializer = null;
104:
105: /**
106: * Creates a new AST node for a variable declaration fragment owned by the
107: * given AST. By default, the variable declaration has: an unspecified
108: * (but legal) variable name, no initializer, and no extra array dimensions.
109: * <p>
110: * N.B. This constructor is package-private.
111: * </p>
112: *
113: * @param ast the AST that is to own this node
114: */
115: VariableDeclarationFragment(AST ast) {
116: super (ast);
117: }
118:
119: /* (omit javadoc for this method)
120: * Method declared on VariableDeclaration.
121: * @since 3.1
122: */
123: final SimplePropertyDescriptor internalExtraDimensionsProperty() {
124: return EXTRA_DIMENSIONS_PROPERTY;
125: }
126:
127: /* (omit javadoc for this method)
128: * Method declared on VariableDeclaration.
129: * @since 3.1
130: */
131: final ChildPropertyDescriptor internalInitializerProperty() {
132: return INITIALIZER_PROPERTY;
133: }
134:
135: /* (omit javadoc for this method)
136: * Method declared on VariableDeclaration.
137: * @since 3.1
138: */
139: final ChildPropertyDescriptor internalNameProperty() {
140: return NAME_PROPERTY;
141: }
142:
143: /* (omit javadoc for this method)
144: * Method declared on ASTNode.
145: */
146: final List internalStructuralPropertiesForType(int apiLevel) {
147: return propertyDescriptors(apiLevel);
148: }
149:
150: /* (omit javadoc for this method)
151: * Method declared on ASTNode.
152: */
153: final int internalGetSetIntProperty(
154: SimplePropertyDescriptor property, boolean get, int value) {
155: if (property == EXTRA_DIMENSIONS_PROPERTY) {
156: if (get) {
157: return getExtraDimensions();
158: } else {
159: setExtraDimensions(value);
160: return 0;
161: }
162: }
163: // allow default implementation to flag the error
164: return super .internalGetSetIntProperty(property, get, value);
165: }
166:
167: /* (omit javadoc for this method)
168: * Method declared on ASTNode.
169: */
170: final ASTNode internalGetSetChildProperty(
171: ChildPropertyDescriptor property, boolean get, ASTNode child) {
172: if (property == NAME_PROPERTY) {
173: if (get) {
174: return getName();
175: } else {
176: setName((SimpleName) child);
177: return null;
178: }
179: }
180: if (property == INITIALIZER_PROPERTY) {
181: if (get) {
182: return getInitializer();
183: } else {
184: setInitializer((Expression) child);
185: return null;
186: }
187: }
188: // allow default implementation to flag the error
189: return super .internalGetSetChildProperty(property, get, child);
190: }
191:
192: /* (omit javadoc for this method)
193: * Method declared on ASTNode.
194: */
195: final int getNodeType0() {
196: return VARIABLE_DECLARATION_FRAGMENT;
197: }
198:
199: /* (omit javadoc for this method)
200: * Method declared on ASTNode.
201: */
202: ASTNode clone0(AST target) {
203: VariableDeclarationFragment result = new VariableDeclarationFragment(
204: target);
205: result
206: .setSourceRange(this .getStartPosition(), this
207: .getLength());
208: result.setName((SimpleName) getName().clone(target));
209: result.setExtraDimensions(getExtraDimensions());
210: result.setInitializer((Expression) ASTNode.copySubtree(target,
211: getInitializer()));
212: return result;
213: }
214:
215: /* (omit javadoc for this method)
216: * Method declared on ASTNode.
217: */
218: final boolean subtreeMatch0(ASTMatcher matcher, Object other) {
219: // dispatch to correct overloaded match method
220: return matcher.match(this , other);
221: }
222:
223: /* (omit javadoc for this method)
224: * Method declared on ASTNode.
225: */
226: void accept0(ASTVisitor visitor) {
227: boolean visitChildren = visitor.visit(this );
228: if (visitChildren) {
229: // visit children in normal left to right reading order
230: acceptChild(visitor, getName());
231: acceptChild(visitor, getInitializer());
232: }
233: visitor.endVisit(this );
234: }
235:
236: /* (omit javadoc for this method)
237: * Method declared on VariableDeclaration.
238: */
239: public SimpleName getName() {
240: if (this .variableName == null) {
241: // lazy init must be thread-safe for readers
242: synchronized (this ) {
243: if (this .variableName == null) {
244: preLazyInit();
245: this .variableName = new SimpleName(this .ast);
246: postLazyInit(this .variableName, NAME_PROPERTY);
247: }
248: }
249: }
250: return this .variableName;
251: }
252:
253: /* (omit javadoc for this method)
254: * Method declared on VariableDeclaration.
255: */
256: public void setName(SimpleName variableName) {
257: if (variableName == null) {
258: throw new IllegalArgumentException();
259: }
260: ASTNode oldChild = this .variableName;
261: preReplaceChild(oldChild, variableName, NAME_PROPERTY);
262: this .variableName = variableName;
263: postReplaceChild(oldChild, variableName, NAME_PROPERTY);
264: }
265:
266: /**
267: * Returns the number of extra array dimensions this variable has over
268: * and above the type specified in the enclosing declaration.
269: * <p>
270: * For example, in the AST for <code>int[] i, j[], k[][]</code> the
271: * variable declaration fragments for the variables <code>i</code>,
272: * <code>j</code>, and <code>k</code>, have 0, 1, and 2 extra array
273: * dimensions, respectively.
274: * </p>
275: *
276: * @return the number of extra array dimensions this variable has over
277: * and above the type specified in the enclosing declaration
278: * @since 2.0
279: */
280: public int getExtraDimensions() {
281: return this .extraArrayDimensions;
282: }
283:
284: /**
285: * Sets the number of extra array dimensions this variable has over
286: * and above the type specified in the enclosing declaration.
287: * <p>
288: * For example, in the AST for <code>int[] i, j[], k[][]</code> the
289: * variable declaration fragments for the variables <code>i</code>,
290: * <code>j</code>, and <code>k</code>, have 0, 1, and 2 extra array
291: * dimensions, respectively.
292: * </p>
293: *
294: * @param dimensions the given dimensions
295: * @since 2.0
296: */
297: public void setExtraDimensions(int dimensions) {
298: if (dimensions < 0) {
299: throw new IllegalArgumentException();
300: }
301: preValueChange(EXTRA_DIMENSIONS_PROPERTY);
302: this .extraArrayDimensions = dimensions;
303: postValueChange(EXTRA_DIMENSIONS_PROPERTY);
304: }
305:
306: /* (omit javadoc for this method)
307: * Method declared on VariableDeclaration.
308: */
309: public Expression getInitializer() {
310: return this .optionalInitializer;
311: }
312:
313: /* (omit javadoc for this method)
314: * Method declared on VariableDeclaration.
315: */
316: public void setInitializer(Expression initializer) {
317: ASTNode oldChild = this .optionalInitializer;
318: preReplaceChild(oldChild, initializer, INITIALIZER_PROPERTY);
319: this .optionalInitializer = initializer;
320: postReplaceChild(oldChild, initializer, INITIALIZER_PROPERTY);
321: }
322:
323: /* (omit javadoc for this method)
324: * Method declared on ASTNode.
325: */
326: int memSize() {
327: // treat Operator as free
328: return BASE_NODE_SIZE + 3 * 4;
329: }
330:
331: /* (omit javadoc for this method)
332: * Method declared on ASTNode.
333: */
334: int treeSize() {
335: return memSize()
336: + (this .variableName == null ? 0 : getName().treeSize())
337: + (this .optionalInitializer == null ? 0
338: : getInitializer().treeSize());
339: }
340: }
|