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 * Static or instance initializer AST node type.
21 * [ <b>static</b> ] Block
25 * @noinstantiate This class is not intended to be instantiated by clients.
27 public class Initializer extends BodyDeclaration {
30 * The "javadoc" structural property of this node type.
33 public static final ChildPropertyDescriptor JAVADOC_PROPERTY =
34 internalJavadocPropertyFactory(Initializer.class);
37 * The "modifiers" structural property of this node type (JLS2 API only).
40 public static final SimplePropertyDescriptor MODIFIERS_PROPERTY =
41 internalModifiersPropertyFactory(Initializer.class);
44 * The "modifiers" structural property of this node type (added in JLS3 API).
47 public static final ChildListPropertyDescriptor MODIFIERS2_PROPERTY =
48 internalModifiers2PropertyFactory(Initializer.class);
51 * The "body" structural property of this node type.
54 public static final ChildPropertyDescriptor BODY_PROPERTY =
55 new ChildPropertyDescriptor(Initializer.class, "body", Block.class, MANDATORY, CYCLE_RISK); //$NON-NLS-1$
58 * A list of property descriptors (element type:
59 * {@link StructuralPropertyDescriptor}),
60 * or null if uninitialized.
63 private static final List PROPERTY_DESCRIPTORS_2_0;
66 * A list of property descriptors (element type:
67 * {@link StructuralPropertyDescriptor}),
68 * or null if uninitialized.
71 private static final List PROPERTY_DESCRIPTORS_3_0;
74 List properyList = new ArrayList(4);
75 createPropertyList(Initializer.class, properyList);
76 addProperty(JAVADOC_PROPERTY, properyList);
77 addProperty(MODIFIERS_PROPERTY, properyList);
78 addProperty(BODY_PROPERTY, properyList);
79 PROPERTY_DESCRIPTORS_2_0 = reapPropertyList(properyList);
81 properyList = new ArrayList(4);
82 createPropertyList(Initializer.class, properyList);
83 addProperty(JAVADOC_PROPERTY, properyList);
84 addProperty(MODIFIERS2_PROPERTY, properyList);
85 addProperty(BODY_PROPERTY, properyList);
86 PROPERTY_DESCRIPTORS_3_0 = reapPropertyList(properyList);
90 * Returns a list of structural property descriptors for this node type.
91 * Clients must not modify the result.
93 * @param apiLevel the API level; one of the
94 * <code>AST.JLS*</code> constants
96 * @return a list of property descriptors (element type:
97 * {@link StructuralPropertyDescriptor})
100 public static List propertyDescriptors(int apiLevel) {
101 if (apiLevel == AST.JLS2_INTERNAL) {
102 return PROPERTY_DESCRIPTORS_2_0;
104 return PROPERTY_DESCRIPTORS_3_0;
109 * The initializer body; lazily initialized; defaults to an empty block.
111 private Block body = null;
114 * Creates a new AST node for an initializer declaration owned by the given
115 * AST. By default, the initializer has no modifiers and an empty block.
116 * The javadoc comment is not used for initializers.
118 * N.B. This constructor is package-private.
121 * @param ast the AST that is to own this node
123 Initializer(AST ast) {
127 /* (omit javadoc for this method)
128 * Method declared on ASTNode.
131 final List internalStructuralPropertiesForType(int apiLevel) {
132 return propertyDescriptors(apiLevel);
135 /* (omit javadoc for this method)
136 * Method declared on ASTNode.
138 final int internalGetSetIntProperty(SimplePropertyDescriptor property, boolean get, int value) {
139 if (property == MODIFIERS_PROPERTY) {
141 return getModifiers();
143 internalSetModifiers(value);
147 // allow default implementation to flag the error
148 return super.internalGetSetIntProperty(property, get, value);
151 /* (omit javadoc for this method)
152 * Method declared on ASTNode.
154 final ASTNode internalGetSetChildProperty(ChildPropertyDescriptor property, boolean get, ASTNode child) {
155 if (property == JAVADOC_PROPERTY) {
159 setJavadoc((Javadoc) child);
163 if (property == BODY_PROPERTY) {
167 setBody((Block) child);
171 // allow default implementation to flag the error
172 return super.internalGetSetChildProperty(property, get, child);
175 /* (omit javadoc for this method)
176 * Method declared on ASTNode.
178 final List internalGetChildListProperty(ChildListPropertyDescriptor property) {
179 if (property == MODIFIERS2_PROPERTY) {
182 // allow default implementation to flag the error
183 return super.internalGetChildListProperty(property);
186 /* (omit javadoc for this method)
187 * Method declared on BodyDeclaration.
189 final ChildPropertyDescriptor internalJavadocProperty() {
190 return JAVADOC_PROPERTY;
193 /* (omit javadoc for this method)
194 * Method declared on BodyDeclaration.
196 final ChildListPropertyDescriptor internalModifiers2Property() {
197 return MODIFIERS2_PROPERTY;
200 /* (omit javadoc for this method)
201 * Method declared on BodyDeclaration.
203 final SimplePropertyDescriptor internalModifiersProperty() {
204 return MODIFIERS_PROPERTY;
207 /* (omit javadoc for this method)
208 * Method declared on ASTNode.
210 final int getNodeType0() {
214 /* (omit javadoc for this method)
215 * Method declared on ASTNode.
217 ASTNode clone0(AST target) {
218 Initializer result = new Initializer(target);
219 result.setSourceRange(this.getStartPosition(), this.getLength());
220 if (this.ast.apiLevel == AST.JLS2_INTERNAL) {
221 result.internalSetModifiers(getModifiers());
223 if (this.ast.apiLevel >= AST.JLS3) {
224 result.modifiers().addAll(ASTNode.copySubtrees(target, modifiers()));
227 (Javadoc) ASTNode.copySubtree(target, getJavadoc()));
228 result.setBody((Block) getBody().clone(target));
232 /* (omit javadoc for this method)
233 * Method declared on ASTNode.
235 final boolean subtreeMatch0(ASTMatcher matcher, Object other) {
236 // dispatch to correct overloaded match method
237 return matcher.match(this, other);
240 /* (omit javadoc for this method)
241 * Method declared on ASTNode.
243 void accept0(ASTVisitor visitor) {
244 boolean visitChildren = visitor.visit(this);
246 acceptChild(visitor, getJavadoc());
247 if (this.ast.apiLevel >= AST.JLS3) {
248 acceptChildren(visitor, this.modifiers);
250 acceptChild(visitor, getBody());
252 visitor.endVisit(this);
256 * Returns the body of this initializer declaration.
258 * @return the initializer body
260 public Block getBody() {
261 if (this.body == null) {
262 // lazy init must be thread-safe for readers
263 synchronized (this) {
264 if (this.body == null) {
266 this.body= new Block(this.ast);
267 postLazyInit(this.body, BODY_PROPERTY);
275 * Sets the body of this initializer declaration.
277 * @param body the block node
278 * @exception IllegalArgumentException if:
280 * <li>the node belongs to a different AST</li>
281 * <li>the node already has a parent</li>
282 * <li>a cycle in would be created</li>
285 public void setBody(Block body) {
287 throw new IllegalArgumentException();
289 ASTNode oldChild = this.body;
290 preReplaceChild(oldChild, body, BODY_PROPERTY);
292 postReplaceChild(oldChild, body, BODY_PROPERTY);
295 /* (omit javadoc for this method)
296 * Method declared on ASTNode.
299 return super.memSize() + 1 * 4;
302 /* (omit javadoc for this method)
303 * Method declared on ASTNode.
308 + (this.optionalDocComment == null ? 0 : getJavadoc().treeSize())
309 + (this.modifiers == null ? 0 : this.modifiers.listSize())
310 + (this.body == null ? 0 : getBody().treeSize());