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.List;
18 * Field declaration node type.
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.
26 * [Javadoc] { ExtendedModifier } Type VariableDeclarationFragment
27 * { <b>,</b> VariableDeclarationFragment } <b>;</b>
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 ";".
37 * @noinstantiate This class is not intended to be instantiated by clients.
39 public class FieldDeclaration extends BodyDeclaration {
42 * The "javadoc" structural property of this node type.
45 public static final ChildPropertyDescriptor JAVADOC_PROPERTY =
46 internalJavadocPropertyFactory(FieldDeclaration.class);
49 * The "modifiers" structural property of this node type (JLS2 API only).
52 public static final SimplePropertyDescriptor MODIFIERS_PROPERTY =
53 internalModifiersPropertyFactory(FieldDeclaration.class);
56 * The "modifiers" structural property of this node type (added in JLS3 API).
59 public static final ChildListPropertyDescriptor MODIFIERS2_PROPERTY =
60 internalModifiers2PropertyFactory(FieldDeclaration.class);
63 * The "type" structural property of this node type.
66 public static final ChildPropertyDescriptor TYPE_PROPERTY =
67 new ChildPropertyDescriptor(FieldDeclaration.class, "type", Type.class, MANDATORY, NO_CYCLE_RISK); //$NON-NLS-1$
70 * The "fragments" structural property of this node type).
73 public static final ChildListPropertyDescriptor FRAGMENTS_PROPERTY =
74 new ChildListPropertyDescriptor(FieldDeclaration.class, "fragments", VariableDeclarationFragment.class, CYCLE_RISK); //$NON-NLS-1$
77 * A list of property descriptors (element type:
78 * {@link StructuralPropertyDescriptor}),
79 * or null if uninitialized.
82 private static final List PROPERTY_DESCRIPTORS_2_0;
85 * A list of property descriptors (element type:
86 * {@link StructuralPropertyDescriptor}),
87 * or null if uninitialized.
90 private static final List PROPERTY_DESCRIPTORS_3_0;
93 List properyList = new ArrayList(5);
94 createPropertyList(FieldDeclaration.class, properyList);
95 addProperty(JAVADOC_PROPERTY, properyList);
96 addProperty(MODIFIERS_PROPERTY, properyList);
97 addProperty(TYPE_PROPERTY, properyList);
98 addProperty(FRAGMENTS_PROPERTY, properyList);
99 PROPERTY_DESCRIPTORS_2_0 = reapPropertyList(properyList);
101 properyList = new ArrayList(5);
102 createPropertyList(FieldDeclaration.class, properyList);
103 addProperty(JAVADOC_PROPERTY, properyList);
104 addProperty(MODIFIERS2_PROPERTY, properyList);
105 addProperty(TYPE_PROPERTY, properyList);
106 addProperty(FRAGMENTS_PROPERTY, properyList);
107 PROPERTY_DESCRIPTORS_3_0 = reapPropertyList(properyList);
111 * Returns a list of structural property descriptors for this node type.
112 * Clients must not modify the result.
114 * @param apiLevel the API level; one of the
115 * <code>AST.JLS*</code> constants
117 * @return a list of property descriptors (element type:
118 * {@link StructuralPropertyDescriptor})
121 public static List propertyDescriptors(int apiLevel) {
122 if (apiLevel == AST.JLS2_INTERNAL) {
123 return PROPERTY_DESCRIPTORS_2_0;
125 return PROPERTY_DESCRIPTORS_3_0;
130 * The base type; lazily initialized; defaults to an unspecified,
133 private Type baseType = null;
136 * The list of variable declaration fragments (element type:
137 * <code VariableDeclarationFragment</code>). Defaults to an empty list.
139 private ASTNode.NodeList variableDeclarationFragments =
140 new ASTNode.NodeList(FRAGMENTS_PROPERTY);
143 * Creates a new unparented field declaration statement node owned
144 * by the given AST. By default, the field declaration has: no modifiers,
145 * an unspecified (but legal) type, and an empty list of variable
146 * declaration fragments (which is syntactically illegal).
148 * N.B. This constructor is package-private.
151 * @param ast the AST that is to own this node
153 FieldDeclaration(AST ast) {
157 /* (omit javadoc for this method)
158 * Method declared on ASTNode.
161 final List internalStructuralPropertiesForType(int apiLevel) {
162 return propertyDescriptors(apiLevel);
165 /* (omit javadoc for this method)
166 * Method declared on ASTNode.
168 final int internalGetSetIntProperty(SimplePropertyDescriptor property, boolean get, int value) {
169 if (property == MODIFIERS_PROPERTY) {
171 return getModifiers();
173 internalSetModifiers(value);
177 // allow default implementation to flag the error
178 return super.internalGetSetIntProperty(property, get, value);
181 /* (omit javadoc for this method)
182 * Method declared on ASTNode.
184 final ASTNode internalGetSetChildProperty(ChildPropertyDescriptor property, boolean get, ASTNode child) {
185 if (property == JAVADOC_PROPERTY) {
189 setJavadoc((Javadoc) child);
193 if (property == TYPE_PROPERTY) {
197 setType((Type) child);
201 // allow default implementation to flag the error
202 return super.internalGetSetChildProperty(property, get, child);
205 /* (omit javadoc for this method)
206 * Method declared on ASTNode.
208 final List internalGetChildListProperty(ChildListPropertyDescriptor property) {
209 if (property == MODIFIERS2_PROPERTY) {
212 if (property == FRAGMENTS_PROPERTY) {
215 // allow default implementation to flag the error
216 return super.internalGetChildListProperty(property);
219 /* (omit javadoc for this method)
220 * Method declared on BodyDeclaration.
222 final ChildPropertyDescriptor internalJavadocProperty() {
223 return JAVADOC_PROPERTY;
226 /* (omit javadoc for this method)
227 * Method declared on BodyDeclaration.
229 final SimplePropertyDescriptor internalModifiersProperty() {
230 return MODIFIERS_PROPERTY;
233 /* (omit javadoc for this method)
234 * Method declared on BodyDeclaration.
236 final ChildListPropertyDescriptor internalModifiers2Property() {
237 return MODIFIERS2_PROPERTY;
240 /* (omit javadoc for this method)
241 * Method declared on ASTNode.
243 final int getNodeType0() {
244 return FIELD_DECLARATION;
247 /* (omit javadoc for this method)
248 * Method declared on ASTNode.
250 ASTNode clone0(AST target) {
251 FieldDeclaration result = new FieldDeclaration(target);
252 result.setSourceRange(this.getStartPosition(), this.getLength());
254 (Javadoc) ASTNode.copySubtree(target, getJavadoc()));
255 if (this.ast.apiLevel == AST.JLS2_INTERNAL) {
256 result.internalSetModifiers(getModifiers());
258 if (this.ast.apiLevel >= AST.JLS3) {
259 result.modifiers().addAll(ASTNode.copySubtrees(target, modifiers()));
261 result.setType((Type) getType().clone(target));
262 result.fragments().addAll(
263 ASTNode.copySubtrees(target, fragments()));
267 /* (omit javadoc for this method)
268 * Method declared on ASTNode.
270 final boolean subtreeMatch0(ASTMatcher matcher, Object other) {
271 // dispatch to correct overloaded match method
272 return matcher.match(this, other);
275 /* (omit javadoc for this method)
276 * Method declared on ASTNode.
278 void accept0(ASTVisitor visitor) {
279 boolean visitChildren = visitor.visit(this);
281 // visit children in normal left to right reading order
282 acceptChild(visitor, getJavadoc());
283 if (this.ast.apiLevel >= AST.JLS3) {
284 acceptChildren(visitor, this.modifiers);
286 acceptChild(visitor, getType());
287 acceptChildren(visitor, this.variableDeclarationFragments);
289 visitor.endVisit(this);
293 * Returns the base type declared in this field declaration.
295 * N.B. The individual child variable declaration fragments may specify
296 * additional array dimensions. So the type of the variable are not
297 * necessarily exactly this type.
300 * @return the base type
302 public Type getType() {
303 if (this.baseType == null) {
304 // lazy init must be thread-safe for readers
305 synchronized (this) {
306 if (this.baseType == null) {
308 this.baseType = this.ast.newPrimitiveType(PrimitiveType.INT);
309 postLazyInit(this.baseType, TYPE_PROPERTY);
313 return this.baseType;
317 * Sets the base type declared in this field declaration to the given type.
319 * @param type the new base type
320 * @exception IllegalArgumentException if:
322 * <li>the node belongs to a different AST</li>
323 * <li>the node already has a parent</li>
326 public void setType(Type type) {
328 throw new IllegalArgumentException();
330 ASTNode oldChild = this.baseType;
331 preReplaceChild(oldChild, type, TYPE_PROPERTY);
332 this.baseType = type;
333 postReplaceChild(oldChild, type, TYPE_PROPERTY);
337 * Returns the live list of variable declaration fragments in this field
338 * declaration. Adding and removing nodes from this list affects this node
339 * dynamically. All nodes in this list must be
340 * <code>VariableDeclarationFragment</code>s; attempts to add any other
341 * type of node will trigger an exception.
343 * @return the live list of variable declaration fragments in this
344 * statement (element type: <code>VariableDeclarationFragment</code>)
346 public List fragments() {
347 return this.variableDeclarationFragments;
350 /* (omit javadoc for this method)
351 * Method declared on ASTNode.
354 return super.memSize() + 2 * 4;
357 /* (omit javadoc for this method)
358 * Method declared on ASTNode.
363 + (this.optionalDocComment == null ? 0 : getJavadoc().treeSize())
364 + (this.modifiers == null ? 0 : this.modifiers.listSize())
365 + (this.baseType == null ? 0 : getType().treeSize())
366 + this.variableDeclarationFragments.listSize();