1 /*******************************************************************************
2 * Copyright (c) 2000, 2008 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
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
12 package net.sourceforge.phpdt.core.dom;
14 import java.util.ArrayList;
15 import java.util.Iterator;
16 import java.util.List;
19 * Local variable declaration expression AST node type.
21 * This kind of node collects together several variable declaration fragments
22 * (<code>VariableDeclarationFragment</code>) into a single expression
23 * (<code>Expression</code>), all sharing the same modifiers and base type.
24 * This type of node can be used as the initializer of a
25 * <code>ForStatement</code>, or wrapped in an <code>ExpressionStatement</code>
26 * to form the equivalent of a <code>VariableDeclarationStatement</code>.
30 * VariableDeclarationExpression:
31 * { Modifier } Type VariableDeclarationFragment
32 * { <b>,</b> VariableDeclarationFragment }
34 * For JLS3, the modifier flags were replaced by
35 * a list of modifier nodes (intermixed with annotations):
37 * VariableDeclarationExpression:
38 * { ExtendedModifier } Type VariableDeclarationFragment
39 * { <b>,</b> VariableDeclarationFragment }
43 * @noinstantiate This class is not intended to be instantiated by clients.
45 public class VariableDeclarationExpression extends Expression {
48 * The "modifiers" structural property of this node type (JLS2 API only).
51 public static final SimplePropertyDescriptor MODIFIERS_PROPERTY =
52 new SimplePropertyDescriptor(VariableDeclarationExpression.class, "modifiers", int.class, MANDATORY); //$NON-NLS-1$
55 * The "modifiers" structural property of this node type (added in JLS3 API).
58 public static final ChildListPropertyDescriptor MODIFIERS2_PROPERTY =
59 new ChildListPropertyDescriptor(VariableDeclarationExpression.class, "modifiers", IExtendedModifier.class, CYCLE_RISK); //$NON-NLS-1$
62 * The "type" structural property of this node type.
65 public static final ChildPropertyDescriptor TYPE_PROPERTY =
66 new ChildPropertyDescriptor(VariableDeclarationExpression.class, "type", Type.class, MANDATORY, NO_CYCLE_RISK); //$NON-NLS-1$
69 * The "fragments" structural property of this node type).
72 public static final ChildListPropertyDescriptor FRAGMENTS_PROPERTY =
73 new ChildListPropertyDescriptor(VariableDeclarationExpression.class, "fragments", VariableDeclarationFragment.class, CYCLE_RISK); //$NON-NLS-1$
76 * A list of property descriptors (element type:
77 * {@link StructuralPropertyDescriptor}),
78 * or null if uninitialized.
81 private static final List PROPERTY_DESCRIPTORS_2_0;
84 * A list of property descriptors (element type:
85 * {@link StructuralPropertyDescriptor}),
86 * or null if uninitialized.
89 private static final List PROPERTY_DESCRIPTORS_3_0;
92 List propertyList = new ArrayList(4);
93 createPropertyList(VariableDeclarationExpression.class, propertyList);
94 addProperty(MODIFIERS_PROPERTY, propertyList);
95 addProperty(TYPE_PROPERTY, propertyList);
96 addProperty(FRAGMENTS_PROPERTY, propertyList);
97 PROPERTY_DESCRIPTORS_2_0 = reapPropertyList(propertyList);
99 propertyList = new ArrayList(4);
100 createPropertyList(VariableDeclarationExpression.class, propertyList);
101 addProperty(MODIFIERS2_PROPERTY, propertyList);
102 addProperty(TYPE_PROPERTY, propertyList);
103 addProperty(FRAGMENTS_PROPERTY, propertyList);
104 PROPERTY_DESCRIPTORS_3_0 = reapPropertyList(propertyList);
108 * Returns a list of structural property descriptors for this node type.
109 * Clients must not modify the result.
111 * @param apiLevel the API level; one of the
112 * <code>AST.JLS*</code> constants
114 * @return a list of property descriptors (element type:
115 * {@link StructuralPropertyDescriptor})
118 public static List propertyDescriptors(int apiLevel) {
119 if (apiLevel == AST.JLS2_INTERNAL) {
120 return PROPERTY_DESCRIPTORS_2_0;
122 return PROPERTY_DESCRIPTORS_3_0;
127 * The extended modifiers (element type: <code>IExtendedModifier</code>).
128 * Null in JLS2. Added in JLS3; defaults to an empty list
132 private ASTNode.NodeList modifiers = null;
135 * The modifier flags; bit-wise or of Modifier flags.
136 * Defaults to none. Not used in 3.0.
138 private int modifierFlags = Modifier.NONE;
141 * The base type; lazily initialized; defaults to an unspecified,
144 private Type baseType = null;
147 * The list of variable declaration fragments (element type:
148 * <code VariableDeclarationFragment</code>). Defaults to an empty list.
150 private ASTNode.NodeList variableDeclarationFragments =
151 new ASTNode.NodeList(FRAGMENTS_PROPERTY);
154 * Creates a new unparented local variable declaration expression node
155 * owned by the given AST. By default, the variable declaration has: no
156 * modifiers, an unspecified (but legal) type, and an empty list of variable
157 * declaration fragments (which is syntactically illegal).
159 * N.B. This constructor is package-private.
162 * @param ast the AST that is to own this node
164 VariableDeclarationExpression(AST ast) {
166 if (ast.apiLevel >= AST.JLS3) {
167 this.modifiers = new ASTNode.NodeList(MODIFIERS2_PROPERTY);
171 /* (omit javadoc for this method)
172 * Method declared on ASTNode.
174 final List internalStructuralPropertiesForType(int apiLevel) {
175 return propertyDescriptors(apiLevel);
178 /* (omit javadoc for this method)
179 * Method declared on ASTNode.
181 final int internalGetSetIntProperty(SimplePropertyDescriptor property, boolean get, int value) {
182 if (property == MODIFIERS_PROPERTY) {
184 return getModifiers();
190 // allow default implementation to flag the error
191 return super.internalGetSetIntProperty(property, get, value);
194 /* (omit javadoc for this method)
195 * Method declared on ASTNode.
197 final ASTNode internalGetSetChildProperty(ChildPropertyDescriptor property, boolean get, ASTNode child) {
198 if (property == TYPE_PROPERTY) {
202 setType((Type) child);
206 // allow default implementation to flag the error
207 return super.internalGetSetChildProperty(property, get, child);
210 /* (omit javadoc for this method)
211 * Method declared on ASTNode.
213 final List internalGetChildListProperty(ChildListPropertyDescriptor property) {
214 if (property == MODIFIERS2_PROPERTY) {
217 if (property == FRAGMENTS_PROPERTY) {
220 // allow default implementation to flag the error
221 return super.internalGetChildListProperty(property);
224 /* (omit javadoc for this method)
225 * Method declared on ASTNode.
227 final int getNodeType0() {
228 return VARIABLE_DECLARATION_EXPRESSION;
231 /* (omit javadoc for this method)
232 * Method declared on ASTNode.
234 ASTNode clone0(AST target) {
235 VariableDeclarationExpression result =
236 new VariableDeclarationExpression(target);
237 result.setSourceRange(this.getStartPosition(), this.getLength());
238 if (this.ast.apiLevel == AST.JLS2_INTERNAL) {
239 result.setModifiers(getModifiers());
241 if (this.ast.apiLevel >= AST.JLS3) {
242 result.modifiers().addAll(ASTNode.copySubtrees(target, modifiers()));
244 result.setType((Type) getType().clone(target));
245 result.fragments().addAll(
246 ASTNode.copySubtrees(target, fragments()));
251 /* (omit javadoc for this method)
252 * Method declared on ASTNode.
254 final boolean subtreeMatch0(ASTMatcher matcher, Object other) {
255 // dispatch to correct overloaded match method
256 return matcher.match(this, other);
259 /* (omit javadoc for this method)
260 * Method declared on ASTNode.
262 void accept0(ASTVisitor visitor) {
263 boolean visitChildren = visitor.visit(this);
265 // visit children in normal left to right reading order
266 if (this.ast.apiLevel >= AST.JLS3) {
267 acceptChildren(visitor, this.modifiers);
269 acceptChild(visitor, getType());
270 acceptChildren(visitor, variableDeclarationFragments);
272 visitor.endVisit(this);
276 * Returns the live ordered list of modifiers and annotations
277 * of this declaration (added in JLS3 API).
279 * Note that the final modifier is the only meaningful modifier for local
280 * variable declarations.
283 * @return the live list of modifiers and annotations
284 * (element type: <code>IExtendedModifier</code>)
285 * @exception UnsupportedOperationException if this operation is used in
289 public List modifiers() {
290 // more efficient than just calling unsupportedIn2() to check
291 if (this.modifiers == null) {
294 return this.modifiers;
298 * Returns the modifiers explicitly specified on this declaration.
300 * In the JLS3 API, this method is a convenience method that
301 * computes these flags from <code>modifiers()</code>.
304 * @return the bit-wise or of <code>Modifier</code> constants
307 public int getModifiers() {
308 // more efficient than checking getAST().API_LEVEL
309 if (this.modifiers == null) {
310 // JLS2 behavior - bona fide property
311 return this.modifierFlags;
313 // JLS3 behavior - convenient method
314 // performance could be improved by caching computed flags
315 // but this would require tracking changes to this.modifiers
316 int computedModifierFlags = Modifier.NONE;
317 for (Iterator it = modifiers().iterator(); it.hasNext(); ) {
318 Object x = it.next();
319 if (x instanceof Modifier) {
320 computedModifierFlags |= ((Modifier) x).getKeyword().toFlagValue();
323 return computedModifierFlags;
328 * Sets the modifiers explicitly specified on this declaration (JLS2 API only).
330 * Note that the final modifier is the only meaningful modifier for local
331 * variable declarations.
334 * @param modifiers the given modifiers (bit-wise or of <code>Modifier</code> constants)
335 * @exception UnsupportedOperationException if this operation is used in
336 * an AST later than JLS2
338 * @deprecated In the JLS3 API, this method is replaced by
339 * {@link #modifiers()} which contains a list of a <code>Modifier</code> nodes.
341 public void setModifiers(int modifiers) {
342 internalSetModifiers(modifiers);
346 * Internal synonym for deprecated method. Used to avoid
347 * deprecation warnings.
350 /*package*/ final void internalSetModifiers(int pmodifiers) {
352 preValueChange(MODIFIERS_PROPERTY);
353 this.modifierFlags = pmodifiers;
354 postValueChange(MODIFIERS_PROPERTY);
358 * Returns the base type declared in this variable declaration.
360 * N.B. The individual child variable declaration fragments may specify
361 * additional array dimensions. So the type of the variable are not
362 * necessarily exactly this type.
365 * @return the base type
367 public Type getType() {
368 if (this.baseType == null) {
369 // lazy init must be thread-safe for readers
370 synchronized (this) {
371 if (this.baseType == null) {
373 this.baseType = this.ast.newPrimitiveType(PrimitiveType.INT);
374 postLazyInit(this.baseType, TYPE_PROPERTY);
378 return this.baseType;
382 * Sets the base type declared in this variable declaration to the given
385 * @param type the new base type
386 * @exception IllegalArgumentException if:
388 * <li>the node belongs to a different AST</li>
389 * <li>the node already has a parent</li>
392 public void setType(Type type) {
394 throw new IllegalArgumentException();
396 ASTNode oldChild = this.baseType;
397 preReplaceChild(oldChild, type, TYPE_PROPERTY);
398 this.baseType = type;
399 postReplaceChild(oldChild, type, TYPE_PROPERTY);
403 * Returns the live list of variable declaration fragments in this
404 * expression. Adding and removing nodes from this list affects this node
405 * dynamically. All nodes in this list must be
406 * <code>VariableDeclarationFragment</code>s; attempts to add any other
407 * type of node will trigger an exception.
409 * @return the live list of variable declaration fragments in this
410 * expression (element type: <code>VariableDeclarationFragment</code>)
412 public List fragments() {
413 return this.variableDeclarationFragments;
416 /* (omit javadoc for this method)
417 * Method declared on ASTNode.
420 // treat Operator as free
421 return BASE_NODE_SIZE + 4 * 4;
424 /* (omit javadoc for this method)
425 * Method declared on ASTNode.
430 + (this.modifiers == null ? 0 : this.modifiers.listSize())
431 + (this.baseType == null ? 0 : getType().treeSize())
432 + this.variableDeclarationFragments.listSize();