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 | |
15 | package org.eclipse.jdt.core.dom; |
16 | |
17 | import java.util.ArrayList; |
18 | import 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"}) |
31 | public 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.class, MANDATORY, CYCLE_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.class, properyList); |
81 | addProperty(JAVADOC_PROPERTY, properyList); |
82 | addProperty(MODIFIERS_PROPERTY, properyList); |
83 | addProperty(BODY_PROPERTY, properyList); |
84 | PROPERTY_DESCRIPTORS_2_0 = reapPropertyList(properyList); |
85 | |
86 | properyList = new ArrayList(4); |
87 | createPropertyList(Initializer.class, properyList); |
88 | addProperty(JAVADOC_PROPERTY, properyList); |
89 | addProperty(MODIFIERS2_PROPERTY, properyList); |
90 | addProperty(BODY_PROPERTY, properyList); |
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 property, boolean get, int 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(property, get, value); |
153 | } |
154 | |
155 | @Override |
156 | final ASTNode internalGetSetChildProperty(ChildPropertyDescriptor property, boolean get, ASTNode child) { |
157 | if (property == JAVADOC_PROPERTY) { |
158 | if (get) { |
159 | return getJavadoc(); |
160 | } else { |
161 | setJavadoc((Javadoc) child); |
162 | return null; |
163 | } |
164 | } |
165 | if (property == BODY_PROPERTY) { |
166 | if (get) { |
167 | return getBody(); |
168 | } else { |
169 | setBody((Block) child); |
170 | return null; |
171 | } |
172 | } |
173 | // allow default implementation to flag the error |
174 | return super.internalGetSetChildProperty(property, get, child); |
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(target, modifiers())); |
215 | } |
216 | result.setJavadoc( |
217 | (Javadoc) ASTNode.copySubtree(target, getJavadoc())); |
218 | result.setBody((Block) getBody().clone(target)); |
219 | return result; |
220 | } |
221 | |
222 | @Override |
223 | final boolean subtreeMatch0(ASTMatcher matcher, Object 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(visitor, getJavadoc()); |
233 | if (this.ast.apiLevel >= AST.JLS3_INTERNAL) { |
234 | acceptChildren(visitor, this.modifiers); |
235 | } |
236 | acceptChild(visitor, getBody()); |
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.body, BODY_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(oldChild, body, BODY_PROPERTY); |
277 | this.body = body; |
278 | postReplaceChild(oldChild, body, BODY_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 |
Members