1 | /******************************************************************************* |
---|---|
2 | * Copyright (c) 2016, 2017 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.Iterator; |
17 | import java.util.List; |
18 | |
19 | /** |
20 | * Requires directive AST node type (added in JLS9 API). |
21 | * <pre> |
22 | * RequiresDirective: |
23 | * <b>requires</b> { ModuleModifier } Name <b>;</b> |
24 | * </pre> |
25 | * |
26 | * @since 3.14 |
27 | * @noinstantiate This class is not intended to be instantiated by clients. |
28 | * @noextend This class is not intended to be subclassed by clients. |
29 | */ |
30 | @SuppressWarnings({"rawtypes", "unchecked"}) |
31 | public class RequiresDirective extends ModuleDirective { |
32 | |
33 | /** |
34 | * The "modifiers" structural property of this node type (element type: {@link ModuleModifier}). |
35 | */ |
36 | public static final ChildListPropertyDescriptor MODIFIERS_PROPERTY = |
37 | new ChildListPropertyDescriptor(RequiresDirective.class, "modifiers", ModuleModifier.class, NO_CYCLE_RISK); //$NON-NLS-1$ |
38 | |
39 | /** |
40 | * The module structural property of this node type (child type: {@link Name}). |
41 | */ |
42 | public static final ChildPropertyDescriptor NAME_PROPERTY = |
43 | new ChildPropertyDescriptor(RequiresDirective.class, "name", Name.class, OPTIONAL, NO_CYCLE_RISK); //$NON-NLS-1$ |
44 | |
45 | /** |
46 | * A list of property descriptors (element type: |
47 | * {@link StructuralPropertyDescriptor}), |
48 | * or null if uninitialized. |
49 | */ |
50 | private static final List PROPERTY_DESCRIPTORS_9_0; |
51 | |
52 | static { |
53 | List propertyList = new ArrayList(3); |
54 | createPropertyList(RequiresDirective.class, propertyList); |
55 | addProperty(MODIFIERS_PROPERTY, propertyList); |
56 | addProperty(NAME_PROPERTY, propertyList); |
57 | PROPERTY_DESCRIPTORS_9_0 = reapPropertyList(propertyList); |
58 | } |
59 | |
60 | /** |
61 | * Returns a list of structural property descriptors for this node type. |
62 | * Clients must not modify the result. |
63 | * |
64 | * @param apiLevel the API level; one of the |
65 | * <code>AST.JLS*</code> constants |
66 | |
67 | * @return a list of property descriptors (element type: |
68 | * {@link StructuralPropertyDescriptor}) |
69 | */ |
70 | public static List propertyDescriptors(int apiLevel) { |
71 | return PROPERTY_DESCRIPTORS_9_0; |
72 | } |
73 | |
74 | /** |
75 | * The extended modifiers (element type: {@link ModuleModifier}). |
76 | * defaults to an empty list |
77 | */ |
78 | private ASTNode.NodeList modifiers = new ASTNode.NodeList(MODIFIERS_PROPERTY); |
79 | |
80 | /** |
81 | * The referenced module name; lazily initialized; defaults to a unspecified, |
82 | * legal Java identifier. |
83 | */ |
84 | private Name name = null; |
85 | |
86 | /** |
87 | * Creates a new AST node for an requires directive owned by the |
88 | * given AST. The requires directive initially is a regular (no modifiers) |
89 | * requires for an unspecified, but legal, Java module name. |
90 | * <p> |
91 | * N.B. This constructor is package-private; all subclasses must be |
92 | * declared in the same package; clients are unable to declare |
93 | * additional subclasses. |
94 | * </p> |
95 | * |
96 | * @param ast the AST that is to own this node |
97 | */ |
98 | RequiresDirective(AST ast) { |
99 | super(ast); |
100 | } |
101 | |
102 | @Override |
103 | final List internalStructuralPropertiesForType(int apiLevel) { |
104 | return propertyDescriptors(apiLevel); |
105 | } |
106 | |
107 | @Override |
108 | final ASTNode internalGetSetChildProperty(ChildPropertyDescriptor property, boolean get, ASTNode child) { |
109 | if (property == NAME_PROPERTY) { |
110 | if (get) { |
111 | return getName(); |
112 | } else { |
113 | setName((Name) child); |
114 | return null; |
115 | } |
116 | } |
117 | |
118 | // allow default implementation to flag the error |
119 | return super.internalGetSetChildProperty(property, get, child); |
120 | } |
121 | |
122 | @Override |
123 | final List internalGetChildListProperty(ChildListPropertyDescriptor property) { |
124 | if (property == MODIFIERS_PROPERTY) { |
125 | return modifiers(); |
126 | } |
127 | |
128 | // allow default implementation to flag the error |
129 | return super.internalGetChildListProperty(property); |
130 | } |
131 | |
132 | @Override |
133 | final int getNodeType0() { |
134 | return REQUIRES_DIRECTIVE; |
135 | } |
136 | |
137 | @Override |
138 | ASTNode clone0(AST target) { |
139 | RequiresDirective result = new RequiresDirective(target); |
140 | result.modifiers().addAll(ASTNode.copySubtrees(target, modifiers())); |
141 | result.setName((Name) getName().clone(target)); |
142 | return result; |
143 | } |
144 | |
145 | @Override |
146 | final boolean subtreeMatch0(ASTMatcher matcher, Object other) { |
147 | // dispatch to correct overloaded match method |
148 | return matcher.match(this, other); |
149 | } |
150 | |
151 | @Override |
152 | void accept0(ASTVisitor visitor) { |
153 | boolean visitChildren = visitor.visit(this); |
154 | if (visitChildren) { |
155 | acceptChildren(visitor, this.modifiers); |
156 | acceptChild(visitor, getName()); |
157 | } |
158 | visitor.endVisit(this); |
159 | } |
160 | |
161 | /** |
162 | * Returns the live ordered list of modifiers |
163 | * of this declaration. |
164 | * <p> |
165 | * Note that the not all modifiers are legal. |
166 | * </p> |
167 | * |
168 | * @return the live list of module modifiers |
169 | * (element type: {@link ModuleModifier}) |
170 | */ |
171 | public List modifiers() { |
172 | return this.modifiers; |
173 | } |
174 | |
175 | /** |
176 | * Returns the module modifiers explicitly specified on this declaration. |
177 | * <p> |
178 | * This method is a convenience method that |
179 | * computes these flags from <code>modifiers()</code>. |
180 | * </p> |
181 | * |
182 | * @return the bit-wise or of <code>ModuleModifier</code> constants |
183 | * @see ModuleModifier |
184 | */ |
185 | public int getModifiers() { |
186 | // do not cache - performance could be improved by caching computed flags |
187 | // but this would require tracking changes to this.modifiers |
188 | int computedModifierFlags = ModuleModifier.NONE; |
189 | for (Iterator it = modifiers().iterator(); it.hasNext(); ) { |
190 | Object x = it.next(); |
191 | if (x instanceof ModuleModifier) { |
192 | computedModifierFlags |= ((ModuleModifier) x).getKeyword().toFlagValue(); |
193 | } |
194 | } |
195 | return computedModifierFlags; |
196 | } |
197 | |
198 | /** |
199 | * Returns the module name referenced by this declaration. |
200 | * |
201 | * @return the module referenced |
202 | */ |
203 | public Name getName() { |
204 | if (this.name == null) { |
205 | // lazy init must be thread-safe for readers |
206 | synchronized (this) { |
207 | if (this.name == null) { |
208 | preLazyInit(); |
209 | this.name =this.ast.newQualifiedName( |
210 | new SimpleName(this.ast), new SimpleName(this.ast)); |
211 | postLazyInit(this.name, NAME_PROPERTY); |
212 | } |
213 | } |
214 | } |
215 | return this.name; |
216 | } |
217 | |
218 | /** |
219 | * Sets the module name in requires directive to the given name. |
220 | * |
221 | * @param name the new module name |
222 | * @exception IllegalArgumentException if: |
223 | * <ul> |
224 | * <li>the node belongs to a different AST</li> |
225 | * <li>the node already has a parent</li> |
226 | * </ul> |
227 | */ |
228 | public void setName(Name name) { |
229 | if (name == null) { |
230 | throw new IllegalArgumentException(); |
231 | } |
232 | ASTNode oldChild = this.name; |
233 | preReplaceChild(oldChild, name, NAME_PROPERTY); |
234 | this.name = name; |
235 | postReplaceChild(oldChild, name, NAME_PROPERTY); |
236 | } |
237 | |
238 | @Override |
239 | int memSize() { |
240 | return BASE_NODE_SIZE + 2 * 4; |
241 | } |
242 | |
243 | @Override |
244 | int treeSize() { |
245 | return |
246 | memSize() |
247 | + (this.modifiers == null ? 0 : this.modifiers.listSize()) |
248 | + (this.name == null ? 0 : getName().treeSize()); |
249 | } |
250 | |
251 | } |
252 |
Members