1 | /******************************************************************************* |
---|---|
2 | * Copyright (c) 2016, 2019 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 | * IBM Corporation - initial API and implementation |
12 | *******************************************************************************/ |
13 | package org.eclipse.jdt.core.dom; |
14 | |
15 | import java.util.ArrayList; |
16 | import java.util.List; |
17 | |
18 | /** |
19 | * Module declaration AST node type representing the module descriptor file (added in JLS9 API). |
20 | * |
21 | * <pre> |
22 | * ModuleDeclaration: |
23 | * [ Javadoc ] { Annotation } [ <b>open</b> ] <b>module</b> Name <b>{</b> |
24 | * { RequiresDirective | ExportsDirective | OpensDirective | UsesDirective | ProvidesDirective } |
25 | * <b>}</b> |
26 | * </pre> |
27 | * |
28 | * @since 3.14 |
29 | * |
30 | * @noextend This class is not intended to be subclassed by clients. |
31 | * @noinstantiate This class is not intended to be instantiated by clients. |
32 | */ |
33 | @SuppressWarnings("rawtypes") |
34 | public class ModuleDeclaration extends ASTNode { |
35 | |
36 | /** |
37 | * The "javadoc" structural property of this node type (child type: {@link Javadoc}). |
38 | */ |
39 | public static final ChildPropertyDescriptor JAVADOC_PROPERTY = |
40 | new ChildPropertyDescriptor(ModuleDeclaration.class, "javadoc", Javadoc.class, OPTIONAL, NO_CYCLE_RISK); //$NON-NLS-1$ |
41 | |
42 | /** |
43 | * The "annotations" structural property of this node type (element type: {@link Annotation}). |
44 | */ |
45 | public static final ChildListPropertyDescriptor ANNOTATIONS_PROPERTY = |
46 | new ChildListPropertyDescriptor(ModuleDeclaration.class, "annotations", Annotation.class, NO_CYCLE_RISK); //$NON-NLS-1$ |
47 | |
48 | /** |
49 | * The "open" structural property of this node type (type: {@link Boolean}). |
50 | */ |
51 | public static final SimplePropertyDescriptor OPEN_PROPERTY = |
52 | new SimplePropertyDescriptor(ModuleDeclaration.class, "open", boolean.class, MANDATORY); //$NON-NLS-1$ |
53 | |
54 | /** |
55 | * The "name" structural property of this node type (child type: {@link Name}). |
56 | */ |
57 | public static final ChildPropertyDescriptor NAME_PROPERTY = |
58 | new ChildPropertyDescriptor(ModuleDeclaration.class, "name", Name.class, MANDATORY, NO_CYCLE_RISK); //$NON-NLS-1$ |
59 | |
60 | /** |
61 | * The "moduleDirectives" structural property of this node type (element type: {@link ModuleDirective}). |
62 | */ |
63 | public static final ChildListPropertyDescriptor MODULE_DIRECTIVES_PROPERTY = |
64 | new ChildListPropertyDescriptor(ModuleDeclaration.class, "moduleDirectives", ModuleDirective.class, NO_CYCLE_RISK); //$NON-NLS-1$ |
65 | |
66 | /** |
67 | * A list of property descriptors (element type: |
68 | * {@link StructuralPropertyDescriptor}), |
69 | * or null if uninitialized. |
70 | */ |
71 | private static final List PROPERTY_DESCRIPTORS_9_0; |
72 | |
73 | static { |
74 | List properyList = new ArrayList(6); |
75 | createPropertyList(ModuleDeclaration.class, properyList); |
76 | addProperty(JAVADOC_PROPERTY, properyList); |
77 | addProperty(ANNOTATIONS_PROPERTY, properyList); |
78 | addProperty(OPEN_PROPERTY, properyList); |
79 | addProperty(NAME_PROPERTY, properyList); |
80 | addProperty(MODULE_DIRECTIVES_PROPERTY, properyList); |
81 | PROPERTY_DESCRIPTORS_9_0 = reapPropertyList(properyList); |
82 | } |
83 | |
84 | /** |
85 | * Returns a list of structural property descriptors for this node type. |
86 | * Clients must not modify the result. |
87 | * |
88 | * @param apiLevel the API level; one of the |
89 | * <code>AST.JLS*</code> constants |
90 | |
91 | * @return a list of property descriptors (element type: |
92 | * {@link StructuralPropertyDescriptor}) |
93 | */ |
94 | public static List propertyDescriptors(int apiLevel) { |
95 | return PROPERTY_DESCRIPTORS_9_0; |
96 | } |
97 | |
98 | /** |
99 | * The doc comment, or <code>null</code> if none. |
100 | * Defaults to none. |
101 | */ |
102 | private Javadoc optionalDocComment = null; |
103 | |
104 | /** |
105 | * The annotations (element type: {@link Annotation}). |
106 | * Defaults to an empty list. |
107 | * |
108 | */ |
109 | private ASTNode.NodeList annotations = new ASTNode.NodeList(ANNOTATIONS_PROPERTY); |
110 | |
111 | /** |
112 | * open versus normal; defaults to normal module. |
113 | */ |
114 | private boolean isOpen = false; |
115 | |
116 | /** |
117 | * The referenced module name; lazily initialized; defaults to a unspecified, |
118 | * legal Java identifier. |
119 | */ |
120 | private Name name = null; |
121 | |
122 | /** |
123 | * The list of statements (element type: {@link ModuleDirective}). |
124 | * Defaults to an empty list. |
125 | */ |
126 | private ASTNode.NodeList moduleStatements = new ASTNode.NodeList(MODULE_DIRECTIVES_PROPERTY); |
127 | |
128 | ModuleDeclaration(AST ast) { |
129 | super(ast); |
130 | unsupportedBelow9(); |
131 | } |
132 | |
133 | @Override |
134 | final List internalStructuralPropertiesForType(int apiLevel) { |
135 | return propertyDescriptors(apiLevel); |
136 | } |
137 | |
138 | @Override |
139 | final boolean internalGetSetBooleanProperty(SimplePropertyDescriptor property, boolean get, boolean value) { |
140 | if (property == OPEN_PROPERTY) { |
141 | if (get) { |
142 | return isOpen(); |
143 | } else { |
144 | setOpen(value); |
145 | return false; |
146 | } |
147 | } |
148 | // allow default implementation to flag the error |
149 | return super.internalGetSetBooleanProperty(property, get, value); |
150 | } |
151 | |
152 | @Override |
153 | final ASTNode internalGetSetChildProperty(ChildPropertyDescriptor property, boolean get, ASTNode child) { |
154 | if (property == JAVADOC_PROPERTY) { |
155 | if (get) { |
156 | return getJavadoc(); |
157 | } else { |
158 | setJavadoc((Javadoc) child); |
159 | return null; |
160 | } |
161 | } |
162 | if (property == NAME_PROPERTY) { |
163 | if (get) { |
164 | return getName(); |
165 | } else { |
166 | setName((Name) child); |
167 | return null; |
168 | } |
169 | } |
170 | // allow default implementation to flag the error |
171 | return super.internalGetSetChildProperty(property, get, child); |
172 | } |
173 | |
174 | @Override |
175 | final List internalGetChildListProperty(ChildListPropertyDescriptor property) { |
176 | if (property == ANNOTATIONS_PROPERTY) { |
177 | return annotations(); |
178 | } |
179 | if (property == MODULE_DIRECTIVES_PROPERTY) { |
180 | return moduleStatements(); |
181 | } |
182 | // allow default implementation to flag the error |
183 | return super.internalGetChildListProperty(property); |
184 | } |
185 | |
186 | @Override |
187 | int getNodeType0() { |
188 | return MODULE_DECLARATION; |
189 | } |
190 | |
191 | @SuppressWarnings("unchecked") |
192 | @Override |
193 | ASTNode clone0(AST target) { |
194 | ModuleDeclaration result = new ModuleDeclaration(target); |
195 | result.setSourceRange(getStartPosition(), getLength()); |
196 | result.setJavadoc((Javadoc) ASTNode.copySubtree(target, getJavadoc())); |
197 | result.setOpen(isOpen()); |
198 | result.annotations().addAll(ASTNode.copySubtrees(target, annotations())); |
199 | result.setName((SimpleName) getName().clone(target)); |
200 | result.moduleStatements().addAll(ASTNode.copySubtrees(target, moduleStatements())); |
201 | return result; |
202 | } |
203 | |
204 | @Override |
205 | boolean subtreeMatch0(ASTMatcher matcher, Object other) { |
206 | // dispatch to correct overloaded match method |
207 | return matcher.match(this, other); |
208 | } |
209 | |
210 | @Override |
211 | void accept0(ASTVisitor visitor) { |
212 | boolean visitChildren = visitor.visit(this); |
213 | if (visitChildren) { |
214 | // visit children in normal left to right reading order |
215 | acceptChild(visitor, getJavadoc()); |
216 | acceptChildren(visitor, this.annotations); |
217 | acceptChild(visitor, getName()); |
218 | acceptChildren(visitor, this.moduleStatements); |
219 | } |
220 | visitor.endVisit(this); |
221 | |
222 | } |
223 | /** |
224 | * Returns the doc comment node. |
225 | * |
226 | * @return the doc comment node, or <code>null</code> if none |
227 | */ |
228 | public Javadoc getJavadoc() { |
229 | return this.optionalDocComment; |
230 | } |
231 | |
232 | /** |
233 | * Sets or clears the doc comment node. |
234 | * |
235 | * @param docComment the doc comment node, or <code>null</code> if none |
236 | * @exception IllegalArgumentException if the doc comment string is invalid |
237 | */ |
238 | public void setJavadoc(Javadoc docComment) { |
239 | ChildPropertyDescriptor p = JAVADOC_PROPERTY; |
240 | ASTNode oldChild = this.optionalDocComment; |
241 | preReplaceChild(oldChild, docComment, p); |
242 | this.optionalDocComment = docComment; |
243 | postReplaceChild(oldChild, docComment, p); |
244 | } |
245 | |
246 | /** |
247 | * Returns the live ordered list of annotations |
248 | * of this declaration. |
249 | * |
250 | * @return the live list of annotations |
251 | * (element type: {@link Annotation}) |
252 | */ |
253 | public List annotations() { |
254 | return this.annotations; |
255 | } |
256 | |
257 | /** |
258 | * Returns whether this module declaration is open or not. |
259 | * |
260 | * @return <code>true</code> if this is open, else |
261 | * <code>false</code> |
262 | */ |
263 | public boolean isOpen() { |
264 | return this.isOpen; |
265 | } |
266 | |
267 | /** |
268 | * Sets whether this module declaration is open or not. |
269 | * |
270 | * @param isOpen <code>true</code> if this is an open module, |
271 | * and <code>false</code> if not |
272 | */ |
273 | public void setOpen(boolean isOpen) { |
274 | preValueChange(OPEN_PROPERTY); |
275 | this.isOpen = isOpen; |
276 | postValueChange(OPEN_PROPERTY); |
277 | } |
278 | |
279 | /** |
280 | * Returns the name of this module declaration. |
281 | * |
282 | * @return the module name |
283 | */ |
284 | public Name getName() { |
285 | if (this.name == null) { |
286 | // lazy init must be thread-safe for readers |
287 | synchronized (this) { |
288 | if (this.name == null) { |
289 | preLazyInit(); |
290 | this.name =this.ast.newQualifiedName( |
291 | new SimpleName(this.ast), new SimpleName(this.ast)); |
292 | postLazyInit(this.name, NAME_PROPERTY); |
293 | } |
294 | } |
295 | } |
296 | return this.name; |
297 | } |
298 | |
299 | /** |
300 | * Sets the module name in to the given name. |
301 | * |
302 | * @param name the new module name |
303 | * @exception IllegalArgumentException if: |
304 | * <ul> |
305 | * <li>the node belongs to a different AST</li> |
306 | * <li>the node already has a parent</li> |
307 | * </ul> |
308 | */ |
309 | public void setName(Name name) { |
310 | if (name == null) { |
311 | throw new IllegalArgumentException(); |
312 | } |
313 | ASTNode oldChild = this.name; |
314 | preReplaceChild(oldChild, name, NAME_PROPERTY); |
315 | this.name = name; |
316 | postReplaceChild(oldChild, name, NAME_PROPERTY); |
317 | } |
318 | |
319 | /** |
320 | * Returns the live list of statements in this module. Adding and |
321 | * removing nodes from this list affects this node dynamically. |
322 | * All nodes in this list must be <code>ModuleDirective</code>s; |
323 | * attempts to add any other type of node will trigger an |
324 | * exception. |
325 | * |
326 | * @return the live list of statements in this module declaration |
327 | * (element type: {@link ModuleDirective}) |
328 | */ |
329 | public List moduleStatements() { |
330 | return this.moduleStatements; |
331 | } |
332 | |
333 | /** |
334 | * Resolves and returns the binding for the module. |
335 | * <p> |
336 | * Note that bindings are generally unavailable unless requested when the |
337 | * AST is being built. |
338 | * </p> |
339 | * |
340 | * @return the binding, or <code>null</code> if the binding cannot be |
341 | * resolved |
342 | */ |
343 | public IModuleBinding resolveBinding() { |
344 | return this.ast.getBindingResolver().resolveModule(this); |
345 | } |
346 | |
347 | @Override |
348 | int memSize() { |
349 | return BASE_NODE_SIZE + 5 * 4; |
350 | } |
351 | |
352 | @Override |
353 | int treeSize() { |
354 | return memSize() |
355 | + (this.optionalDocComment == null ? 0 : getJavadoc().treeSize()) |
356 | + this.annotations.listSize() |
357 | + (this.name == null ? 0 : getName().treeSize()) |
358 | + this.moduleStatements.listSize(); |
359 | } |
360 | } |
361 |
Members