001: /*******************************************************************************
002: * Copyright (c) 2000, 2005 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: * Static or instance initializer AST node type.
017: * <pre>
018: * Initializer:
019: * [ <b>static</b> ] Block
020: * </pre>
021: *
022: * @since 2.0
023: */
024: public class Initializer extends BodyDeclaration {
025:
026: /**
027: * The "javadoc" structural property of this node type.
028: * @since 3.0
029: */
030: public static final ChildPropertyDescriptor JAVADOC_PROPERTY = internalJavadocPropertyFactory(Initializer.class);
031:
032: /**
033: * The "modifiers" structural property of this node type (JLS2 API only).
034: * @since 3.0
035: */
036: public static final SimplePropertyDescriptor MODIFIERS_PROPERTY = internalModifiersPropertyFactory(Initializer.class);
037:
038: /**
039: * The "modifiers" structural property of this node type (added in JLS3 API).
040: * @since 3.1
041: */
042: public static final ChildListPropertyDescriptor MODIFIERS2_PROPERTY = internalModifiers2PropertyFactory(Initializer.class);
043:
044: /**
045: * The "body" structural property of this node type.
046: * @since 3.0
047: */
048: public static final ChildPropertyDescriptor BODY_PROPERTY = new ChildPropertyDescriptor(
049: Initializer.class,
050: "body", Block.class, MANDATORY, CYCLE_RISK); //$NON-NLS-1$
051:
052: /**
053: * A list of property descriptors (element type:
054: * {@link StructuralPropertyDescriptor}),
055: * or null if uninitialized.
056: * @since 3.0
057: */
058: private static final List PROPERTY_DESCRIPTORS_2_0;
059:
060: /**
061: * A list of property descriptors (element type:
062: * {@link StructuralPropertyDescriptor}),
063: * or null if uninitialized.
064: * @since 3.1
065: */
066: private static final List PROPERTY_DESCRIPTORS_3_0;
067:
068: static {
069: List properyList = new ArrayList(4);
070: createPropertyList(Initializer.class, properyList);
071: addProperty(JAVADOC_PROPERTY, properyList);
072: addProperty(MODIFIERS_PROPERTY, properyList);
073: addProperty(BODY_PROPERTY, properyList);
074: PROPERTY_DESCRIPTORS_2_0 = reapPropertyList(properyList);
075:
076: properyList = new ArrayList(4);
077: createPropertyList(Initializer.class, properyList);
078: addProperty(JAVADOC_PROPERTY, properyList);
079: addProperty(MODIFIERS2_PROPERTY, properyList);
080: addProperty(BODY_PROPERTY, properyList);
081: PROPERTY_DESCRIPTORS_3_0 = reapPropertyList(properyList);
082: }
083:
084: /**
085: * Returns a list of structural property descriptors for this node type.
086: * Clients must not modify the result.
087: *
088: * @param apiLevel the API level; one of the
089: * <code>AST.JLS*</code> constants
090:
091: * @return a list of property descriptors (element type:
092: * {@link StructuralPropertyDescriptor})
093: * @since 3.0
094: */
095: public static List propertyDescriptors(int apiLevel) {
096: if (apiLevel == AST.JLS2_INTERNAL) {
097: return PROPERTY_DESCRIPTORS_2_0;
098: } else {
099: return PROPERTY_DESCRIPTORS_3_0;
100: }
101: }
102:
103: /**
104: * The initializer body; lazily initialized; defaults to an empty block.
105: */
106: private Block body = null;
107:
108: /**
109: * Creates a new AST node for an initializer declaration owned by the given
110: * AST. By default, the initializer has no modifiers and an empty block.
111: * The javadoc comment is not used for initializers.
112: * <p>
113: * N.B. This constructor is package-private.
114: * </p>
115: *
116: * @param ast the AST that is to own this node
117: */
118: Initializer(AST ast) {
119: super (ast);
120: }
121:
122: /* (omit javadoc for this method)
123: * Method declared on ASTNode.
124: * @since 3.0
125: */
126: final List internalStructuralPropertiesForType(int apiLevel) {
127: return propertyDescriptors(apiLevel);
128: }
129:
130: /* (omit javadoc for this method)
131: * Method declared on ASTNode.
132: */
133: final int internalGetSetIntProperty(
134: SimplePropertyDescriptor property, boolean get, int value) {
135: if (property == MODIFIERS_PROPERTY) {
136: if (get) {
137: return getModifiers();
138: } else {
139: internalSetModifiers(value);
140: return 0;
141: }
142: }
143: // allow default implementation to flag the error
144: return super .internalGetSetIntProperty(property, get, value);
145: }
146:
147: /* (omit javadoc for this method)
148: * Method declared on ASTNode.
149: */
150: final ASTNode internalGetSetChildProperty(
151: ChildPropertyDescriptor property, boolean get, ASTNode child) {
152: if (property == JAVADOC_PROPERTY) {
153: if (get) {
154: return getJavadoc();
155: } else {
156: setJavadoc((Javadoc) child);
157: return null;
158: }
159: }
160: if (property == BODY_PROPERTY) {
161: if (get) {
162: return getBody();
163: } else {
164: setBody((Block) child);
165: return null;
166: }
167: }
168: // allow default implementation to flag the error
169: return super .internalGetSetChildProperty(property, get, child);
170: }
171:
172: /* (omit javadoc for this method)
173: * Method declared on ASTNode.
174: */
175: final List internalGetChildListProperty(
176: ChildListPropertyDescriptor property) {
177: if (property == MODIFIERS2_PROPERTY) {
178: return modifiers();
179: }
180: // allow default implementation to flag the error
181: return super .internalGetChildListProperty(property);
182: }
183:
184: /* (omit javadoc for this method)
185: * Method declared on BodyDeclaration.
186: */
187: final ChildPropertyDescriptor internalJavadocProperty() {
188: return JAVADOC_PROPERTY;
189: }
190:
191: /* (omit javadoc for this method)
192: * Method declared on BodyDeclaration.
193: */
194: final ChildListPropertyDescriptor internalModifiers2Property() {
195: return MODIFIERS2_PROPERTY;
196: }
197:
198: /* (omit javadoc for this method)
199: * Method declared on BodyDeclaration.
200: */
201: final SimplePropertyDescriptor internalModifiersProperty() {
202: return MODIFIERS_PROPERTY;
203: }
204:
205: /* (omit javadoc for this method)
206: * Method declared on ASTNode.
207: */
208: final int getNodeType0() {
209: return INITIALIZER;
210: }
211:
212: /* (omit javadoc for this method)
213: * Method declared on ASTNode.
214: */
215: ASTNode clone0(AST target) {
216: Initializer result = new Initializer(target);
217: result
218: .setSourceRange(this .getStartPosition(), this
219: .getLength());
220: if (this .ast.apiLevel == AST.JLS2_INTERNAL) {
221: result.internalSetModifiers(getModifiers());
222: }
223: if (this .ast.apiLevel >= AST.JLS3) {
224: result.modifiers().addAll(
225: ASTNode.copySubtrees(target, modifiers()));
226: }
227: result.setJavadoc((Javadoc) ASTNode.copySubtree(target,
228: getJavadoc()));
229: result.setBody((Block) getBody().clone(target));
230: return result;
231: }
232:
233: /* (omit javadoc for this method)
234: * Method declared on ASTNode.
235: */
236: final boolean subtreeMatch0(ASTMatcher matcher, Object other) {
237: // dispatch to correct overloaded match method
238: return matcher.match(this , other);
239: }
240:
241: /* (omit javadoc for this method)
242: * Method declared on ASTNode.
243: */
244: void accept0(ASTVisitor visitor) {
245: boolean visitChildren = visitor.visit(this );
246: if (visitChildren) {
247: acceptChild(visitor, getJavadoc());
248: if (this .ast.apiLevel >= AST.JLS3) {
249: acceptChildren(visitor, this .modifiers);
250: }
251: acceptChild(visitor, getBody());
252: }
253: visitor.endVisit(this );
254: }
255:
256: /**
257: * Returns the body of this initializer declaration.
258: *
259: * @return the initializer body
260: */
261: public Block getBody() {
262: if (this .body == null) {
263: // lazy init must be thread-safe for readers
264: synchronized (this ) {
265: if (this .body == null) {
266: preLazyInit();
267: this .body = new Block(this .ast);
268: postLazyInit(this .body, BODY_PROPERTY);
269: }
270: }
271: }
272: return this .body;
273: }
274:
275: /**
276: * Sets the body of this initializer declaration.
277: *
278: * @param body the block node
279: * @exception IllegalArgumentException if:
280: * <ul>
281: * <li>the node belongs to a different AST</li>
282: * <li>the node already has a parent</li>
283: * <li>a cycle in would be created</li>
284: * </ul>
285: */
286: public void setBody(Block body) {
287: if (body == null) {
288: throw new IllegalArgumentException();
289: }
290: ASTNode oldChild = this .body;
291: preReplaceChild(oldChild, body, BODY_PROPERTY);
292: this .body = body;
293: postReplaceChild(oldChild, body, BODY_PROPERTY);
294: }
295:
296: /* (omit javadoc for this method)
297: * Method declared on ASTNode.
298: */
299: int memSize() {
300: return super .memSize() + 1 * 4;
301: }
302:
303: /* (omit javadoc for this method)
304: * Method declared on ASTNode.
305: */
306: int treeSize() {
307: return memSize()
308: + (this .optionalDocComment == null ? 0 : getJavadoc()
309: .treeSize())
310: + (this .modifiers == null ? 0 : this .modifiers
311: .listSize())
312: + (this .body == null ? 0 : getBody().treeSize());
313: }
314: }
|