EclipseJDT Source Viewer

Home|eclipse_jdt/src/org/eclipse/jdt/core/dom/Initializer.java
1/*******************************************************************************
2 * Copyright (c) 2000, 2013 IBM Corporation and others.
3 *
4 * This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License 2.0
6 * which accompanies this distribution, and is available at
7 * https://www.eclipse.org/legal/epl-2.0/
8 *
9 * SPDX-License-Identifier: EPL-2.0
10 *
11 * Contributors:
12 *     IBM Corporation - initial API and implementation
13 *******************************************************************************/
14
15package org.eclipse.jdt.core.dom;
16
17import java.util.ArrayList;
18import java.util.List;
19
20/**
21 * Static or instance initializer AST node type.
22 * <pre>
23 * Initializer:
24 *     [ <b>static</b> ] Block
25 * </pre>
26 *
27 * @since 2.0
28 * @noinstantiate This class is not intended to be instantiated by clients.
29 */
30@SuppressWarnings({"rawtypes"})
31public class Initializer extends BodyDeclaration {
32
33    /**
34     * The "javadoc" structural property of this node type (child type: {@link Javadoc}).
35     * @since 3.0
36     */
37    public static final ChildPropertyDescriptor JAVADOC_PROPERTY =
38        internalJavadocPropertyFactory(Initializer.class);
39
40    /**
41     * The "modifiers" structural property of this node type (type: {@link Integer}) (JLS2 API only).
42     * @since 3.0
43     * @deprecated In the JLS3 API, this property is replaced by {@link #MODIFIERS2_PROPERTY}.
44     */
45    public static final SimplePropertyDescriptor MODIFIERS_PROPERTY =
46        internalModifiersPropertyFactory(Initializer.class);
47
48    /**
49     * The "modifiers" structural property of this node type (element type: {@link IExtendedModifier}) (added in JLS3 API).
50     * @since 3.1
51     */
52    public static final ChildListPropertyDescriptor MODIFIERS2_PROPERTY =
53        internalModifiers2PropertyFactory(Initializer.class);
54
55    /**
56     * The "body" structural property of this node type (child type: {@link Block}).
57     * @since 3.0
58     */
59    public static final ChildPropertyDescriptor BODY_PROPERTY =
60        new ChildPropertyDescriptor(Initializer.class"body"Block.classMANDATORYCYCLE_RISK); //$NON-NLS-1$
61
62    /**
63     * A list of property descriptors (element type:
64     * {@link StructuralPropertyDescriptor}),
65     * or null if uninitialized.
66     * @since 3.0
67     */
68    private static final List PROPERTY_DESCRIPTORS_2_0;
69
70    /**
71     * A list of property descriptors (element type:
72     * {@link StructuralPropertyDescriptor}),
73     * or null if uninitialized.
74     * @since 3.1
75     */
76    private static final List PROPERTY_DESCRIPTORS_3_0;
77
78    static {
79        List properyList = new ArrayList(4);
80        createPropertyList(Initializer.classproperyList);
81        addProperty(JAVADOC_PROPERTYproperyList);
82        addProperty(MODIFIERS_PROPERTYproperyList);
83        addProperty(BODY_PROPERTYproperyList);
84        PROPERTY_DESCRIPTORS_2_0 = reapPropertyList(properyList);
85
86        properyList = new ArrayList(4);
87        createPropertyList(Initializer.classproperyList);
88        addProperty(JAVADOC_PROPERTYproperyList);
89        addProperty(MODIFIERS2_PROPERTYproperyList);
90        addProperty(BODY_PROPERTYproperyList);
91        PROPERTY_DESCRIPTORS_3_0 = reapPropertyList(properyList);
92    }
93
94    /**
95     * Returns a list of structural property descriptors for this node type.
96     * Clients must not modify the result.
97     *
98     * @param apiLevel the API level; one of the
99     * <code>AST.JLS*</code> constants
100
101     * @return a list of property descriptors (element type:
102     * {@link StructuralPropertyDescriptor})
103     * @since 3.0
104     */
105    public static List propertyDescriptors(int apiLevel) {
106        if (apiLevel == AST.JLS2_INTERNAL) {
107            return PROPERTY_DESCRIPTORS_2_0;
108        } else {
109            return PROPERTY_DESCRIPTORS_3_0;
110        }
111    }
112
113    /**
114     * The initializer body; lazily initialized; defaults to an empty block.
115     */
116    private Block body = null;
117
118    /**
119     * Creates a new AST node for an initializer declaration owned by the given
120     * AST. By default, the initializer has no modifiers and an empty block.
121     * The javadoc comment is not used for initializers.
122     * <p>
123     * N.B. This constructor is package-private.
124     * </p>
125     *
126     * @param ast the AST that is to own this node
127     */
128    Initializer(AST ast) {
129        super(ast);
130    }
131
132    /* (omit javadoc for this method)
133     * Method declared on ASTNode.
134     * @since 3.0
135     */
136    @Override
137    final List internalStructuralPropertiesForType(int apiLevel) {
138        return propertyDescriptors(apiLevel);
139    }
140
141    @Override
142    final int internalGetSetIntProperty(SimplePropertyDescriptor propertyboolean getint value) {
143        if (property == MODIFIERS_PROPERTY) {
144            if (get) {
145                return getModifiers();
146            } else {
147                internalSetModifiers(value);
148                return 0;
149            }
150        }
151        // allow default implementation to flag the error
152        return super.internalGetSetIntProperty(propertygetvalue);
153    }
154
155    @Override
156    final ASTNode internalGetSetChildProperty(ChildPropertyDescriptor propertyboolean getASTNode child) {
157        if (property == JAVADOC_PROPERTY) {
158            if (get) {
159                return getJavadoc();
160            } else {
161                setJavadoc((Javadocchild);
162                return null;
163            }
164        }
165        if (property == BODY_PROPERTY) {
166            if (get) {
167                return getBody();
168            } else {
169                setBody((Blockchild);
170                return null;
171            }
172        }
173        // allow default implementation to flag the error
174        return super.internalGetSetChildProperty(propertygetchild);
175    }
176
177    @Override
178    final List internalGetChildListProperty(ChildListPropertyDescriptor property) {
179        if (property == MODIFIERS2_PROPERTY) {
180            return modifiers();
181        }
182        // allow default implementation to flag the error
183        return super.internalGetChildListProperty(property);
184    }
185
186    @Override
187    final ChildPropertyDescriptor internalJavadocProperty() {
188        return JAVADOC_PROPERTY;
189    }
190
191    @Override
192    final ChildListPropertyDescriptor internalModifiers2Property() {
193        return MODIFIERS2_PROPERTY;
194    }
195
196    @Override
197    final SimplePropertyDescriptor internalModifiersProperty() {
198        return MODIFIERS_PROPERTY;
199    }
200
201    @Override
202    final int getNodeType0() {
203        return INITIALIZER;
204    }
205
206    @Override
207    ASTNode clone0(AST target) {
208        Initializer result = new Initializer(target);
209        result.setSourceRange(getStartPosition(), getLength());
210        if (this.ast.apiLevel == AST.JLS2_INTERNAL) {
211            result.internalSetModifiers(getModifiers());
212        }
213        if (this.ast.apiLevel >= AST.JLS3_INTERNAL) {
214            result.modifiers().addAll(ASTNode.copySubtrees(targetmodifiers()));
215        }
216        result.setJavadoc(
217            (JavadocASTNode.copySubtree(targetgetJavadoc()));
218        result.setBody((BlockgetBody().clone(target));
219        return result;
220    }
221
222    @Override
223    final boolean subtreeMatch0(ASTMatcher matcherObject other) {
224        // dispatch to correct overloaded match method
225        return matcher.match(this, other);
226    }
227
228    @Override
229    void accept0(ASTVisitor visitor) {
230        boolean visitChildren = visitor.visit(this);
231        if (visitChildren) {
232            acceptChild(visitorgetJavadoc());
233            if (this.ast.apiLevel >= AST.JLS3_INTERNAL) {
234                acceptChildren(visitor, this.modifiers);
235            }
236            acceptChild(visitorgetBody());
237        }
238        visitor.endVisit(this);
239    }
240
241    /**
242     * Returns the body of this initializer declaration.
243     *
244     * @return the initializer body
245     */
246    public Block getBody() {
247        if (this.body == null) {
248            // lazy init must be thread-safe for readers
249            synchronized (this) {
250                if (this.body == null) {
251                    preLazyInit();
252                    this.body= new Block(this.ast);
253                    postLazyInit(this.bodyBODY_PROPERTY);
254                }
255            }
256        }
257        return this.body;
258    }
259
260    /**
261     * Sets the body of this initializer declaration.
262     *
263     * @param body the block node
264     * @exception IllegalArgumentException if:
265     * <ul>
266     * <li>the node belongs to a different AST</li>
267     * <li>the node already has a parent</li>
268     * <li>a cycle in would be created</li>
269     * </ul>
270     */
271    public void setBody(Block body) {
272        if (body == null) {
273            throw new IllegalArgumentException();
274        }
275        ASTNode oldChild = this.body;
276        preReplaceChild(oldChildbodyBODY_PROPERTY);
277        this.body = body;
278        postReplaceChild(oldChildbodyBODY_PROPERTY);
279    }
280
281    @Override
282    int memSize() {
283        return super.memSize() + 1 * 4;
284    }
285
286    @Override
287    int treeSize() {
288        return
289            memSize()
290            + (this.optionalDocComment == null ? 0 : getJavadoc().treeSize())
291            + (this.modifiers == null ? 0 : this.modifiers.listSize())
292            + (this.body == null ? 0 : getBody().treeSize());
293    }
294}
295
296
MembersX
Initializer:MODIFIERS_PROPERTY
Initializer:MODIFIERS2_PROPERTY
Initializer:internalJavadocProperty
Initializer:internalGetSetChildProperty
Initializer:Initializer
Initializer:treeSize
Initializer:body
Initializer:propertyDescriptors
Initializer:accept0
Initializer:internalModifiersProperty
Initializer:getNodeType0
Initializer:setBody
Initializer:JAVADOC_PROPERTY
Initializer:accept0:Block:visitChildren
Initializer:Block:properyList
Initializer:subtreeMatch0
Initializer:PROPERTY_DESCRIPTORS_2_0
Initializer:PROPERTY_DESCRIPTORS_3_0
Initializer:BODY_PROPERTY
Initializer:internalGetSetIntProperty
Initializer:internalModifiers2Property
Initializer:internalGetChildListProperty
Initializer:memSize
Initializer:clone0
Initializer:getBody
Initializer:internalStructuralPropertiesForType
Initializer:clone0:Block:result
Initializer:setBody:Block:oldChild
Members
X