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 | * Import declaration AST node type. |
22 | * |
23 | * <pre> |
24 | * ImportDeclaration: |
25 | * <b>import</b> [ <b>static</b> ] Name [ <b>.</b> <b>*</b> ] <b>;</b> |
26 | * </pre> |
27 | * @since 2.0 |
28 | * @noinstantiate This class is not intended to be instantiated by clients. |
29 | */ |
30 | @SuppressWarnings("rawtypes") |
31 | public class ImportDeclaration extends ASTNode { |
32 | |
33 | /** |
34 | * The "name" structural property of this node type (child type: {@link Name}). |
35 | * @since 3.0 |
36 | */ |
37 | public static final ChildPropertyDescriptor NAME_PROPERTY = |
38 | new ChildPropertyDescriptor(ImportDeclaration.class, "name", Name.class, MANDATORY, NO_CYCLE_RISK); //$NON-NLS-1$ |
39 | |
40 | /** |
41 | * The "onDemand" structural property of this node type (type: {@link Boolean}). |
42 | * @since 3.0 |
43 | */ |
44 | public static final SimplePropertyDescriptor ON_DEMAND_PROPERTY = |
45 | new SimplePropertyDescriptor(ImportDeclaration.class, "onDemand", boolean.class, MANDATORY); //$NON-NLS-1$ |
46 | |
47 | /** |
48 | * The "static" structural property of this node type (type: {@link Boolean}) (added in JLS3 API). |
49 | * @since 3.1 |
50 | */ |
51 | public static final SimplePropertyDescriptor STATIC_PROPERTY = |
52 | new SimplePropertyDescriptor(ImportDeclaration.class, "static", boolean.class, MANDATORY); //$NON-NLS-1$ |
53 | |
54 | /** |
55 | * A list of property descriptors (element type: |
56 | * {@link StructuralPropertyDescriptor}), |
57 | * or null if uninitialized. |
58 | * @since 3.0 |
59 | */ |
60 | private static final List PROPERTY_DESCRIPTORS_2_0; |
61 | |
62 | /** |
63 | * A list of property descriptors (element type: |
64 | * {@link StructuralPropertyDescriptor}), |
65 | * or null if uninitialized. |
66 | * @since 3.1 |
67 | */ |
68 | private static final List PROPERTY_DESCRIPTORS_3_0; |
69 | |
70 | static { |
71 | List properyList = new ArrayList(3); |
72 | createPropertyList(ImportDeclaration.class, properyList); |
73 | addProperty(NAME_PROPERTY, properyList); |
74 | addProperty(ON_DEMAND_PROPERTY, properyList); |
75 | PROPERTY_DESCRIPTORS_2_0 = reapPropertyList(properyList); |
76 | |
77 | properyList = new ArrayList(4); |
78 | createPropertyList(ImportDeclaration.class, properyList); |
79 | addProperty(STATIC_PROPERTY, properyList); |
80 | addProperty(NAME_PROPERTY, properyList); |
81 | addProperty(ON_DEMAND_PROPERTY, properyList); |
82 | PROPERTY_DESCRIPTORS_3_0 = reapPropertyList(properyList); |
83 | } |
84 | |
85 | /** |
86 | * Returns a list of structural property descriptors for this node type. |
87 | * Clients must not modify the result. |
88 | * |
89 | * @param apiLevel the API level; one of the |
90 | * <code>AST.JLS*</code> constants |
91 | |
92 | * @return a list of property descriptors (element type: |
93 | * {@link StructuralPropertyDescriptor}) |
94 | * @since 3.0 |
95 | */ |
96 | public static List propertyDescriptors(int apiLevel) { |
97 | if (apiLevel == AST.JLS2_INTERNAL) { |
98 | return PROPERTY_DESCRIPTORS_2_0; |
99 | } else { |
100 | return PROPERTY_DESCRIPTORS_3_0; |
101 | } |
102 | } |
103 | |
104 | /** |
105 | * The import name; lazily initialized; defaults to a unspecified, |
106 | * legal Java identifier. |
107 | */ |
108 | private Name importName = null; |
109 | |
110 | /** |
111 | * On demand versus single type import; defaults to single type import. |
112 | */ |
113 | private boolean onDemand = false; |
114 | |
115 | /** |
116 | * Static versus regular; defaults to regular import. |
117 | * Added in JLS3; not used in JLS2. |
118 | * @since 3.1 |
119 | */ |
120 | private boolean isStatic = false; |
121 | |
122 | /** |
123 | * Creates a new AST node for an import declaration owned by the |
124 | * given AST. The import declaration initially is a regular (non-static) |
125 | * single type import for an unspecified, but legal, Java type name. |
126 | * <p> |
127 | * N.B. This constructor is package-private; all subclasses must be |
128 | * declared in the same package; clients are unable to declare |
129 | * additional subclasses. |
130 | * </p> |
131 | * |
132 | * @param ast the AST that is to own this node |
133 | */ |
134 | ImportDeclaration(AST ast) { |
135 | super(ast); |
136 | } |
137 | |
138 | @Override |
139 | final List internalStructuralPropertiesForType(int apiLevel) { |
140 | return propertyDescriptors(apiLevel); |
141 | } |
142 | |
143 | @Override |
144 | final boolean internalGetSetBooleanProperty(SimplePropertyDescriptor property, boolean get, boolean value) { |
145 | if (property == ON_DEMAND_PROPERTY) { |
146 | if (get) { |
147 | return isOnDemand(); |
148 | } else { |
149 | setOnDemand(value); |
150 | return false; |
151 | } |
152 | } |
153 | if (property == STATIC_PROPERTY) { |
154 | if (get) { |
155 | return isStatic(); |
156 | } else { |
157 | setStatic(value); |
158 | return false; |
159 | } |
160 | } |
161 | // allow default implementation to flag the error |
162 | return super.internalGetSetBooleanProperty(property, get, value); |
163 | } |
164 | |
165 | @Override |
166 | final ASTNode internalGetSetChildProperty(ChildPropertyDescriptor property, boolean get, ASTNode child) { |
167 | if (property == NAME_PROPERTY) { |
168 | if (get) { |
169 | return getName(); |
170 | } else { |
171 | setName((Name) child); |
172 | return null; |
173 | } |
174 | } |
175 | // allow default implementation to flag the error |
176 | return super.internalGetSetChildProperty(property, get, child); |
177 | } |
178 | |
179 | @Override |
180 | final int getNodeType0() { |
181 | return IMPORT_DECLARATION; |
182 | } |
183 | |
184 | @Override |
185 | ASTNode clone0(AST target) { |
186 | ImportDeclaration result = new ImportDeclaration(target); |
187 | result.setSourceRange(getStartPosition(), getLength()); |
188 | result.setOnDemand(isOnDemand()); |
189 | if (this.ast.apiLevel >= AST.JLS3_INTERNAL) { |
190 | result.setStatic(isStatic()); |
191 | } |
192 | result.setName((Name) getName().clone(target)); |
193 | return result; |
194 | } |
195 | |
196 | @Override |
197 | final boolean subtreeMatch0(ASTMatcher matcher, Object other) { |
198 | // dispatch to correct overloaded match method |
199 | return matcher.match(this, other); |
200 | } |
201 | |
202 | @Override |
203 | void accept0(ASTVisitor visitor) { |
204 | boolean visitChildren = visitor.visit(this); |
205 | if (visitChildren) { |
206 | acceptChild(visitor, getName()); |
207 | } |
208 | visitor.endVisit(this); |
209 | } |
210 | |
211 | /** |
212 | * Returns the name imported by this declaration. |
213 | * <p> |
214 | * For a regular on-demand import, this is the name of a package. |
215 | * For a static on-demand import, this is the qualified name of |
216 | * a type. For a regular single-type import, this is the qualified name |
217 | * of a type. For a static single-type import, this is the qualified name |
218 | * of a static member of a type. |
219 | * </p> |
220 | * |
221 | * @return the imported name node |
222 | */ |
223 | public Name getName() { |
224 | if (this.importName == null) { |
225 | // lazy init must be thread-safe for readers |
226 | synchronized (this) { |
227 | if (this.importName == null) { |
228 | preLazyInit(); |
229 | this.importName =this.ast.newQualifiedName( |
230 | new SimpleName(this.ast), new SimpleName(this.ast)); |
231 | postLazyInit(this.importName, NAME_PROPERTY); |
232 | } |
233 | } |
234 | } |
235 | return this.importName; |
236 | } |
237 | |
238 | /** |
239 | * Sets the name of this import declaration to the given name. |
240 | * <p> |
241 | * For a regular on-demand import, this is the name of a package. |
242 | * For a static on-demand import, this is the qualified name of |
243 | * a type. For a regular single-type import, this is the qualified name |
244 | * of a type. For a static single-type import, this is the qualified name |
245 | * of a static member of a type. |
246 | * </p> |
247 | * |
248 | * @param name the new import name |
249 | * @exception IllegalArgumentException if: |
250 | * <ul> |
251 | * <li>the node belongs to a different AST</li> |
252 | * <li>the node already has a parent</li> |
253 | * </ul> |
254 | */ |
255 | public void setName(Name name) { |
256 | if (name == null) { |
257 | throw new IllegalArgumentException(); |
258 | } |
259 | ASTNode oldChild = this.importName; |
260 | preReplaceChild(oldChild, name, NAME_PROPERTY); |
261 | this.importName = name; |
262 | postReplaceChild(oldChild, name, NAME_PROPERTY); |
263 | } |
264 | |
265 | /** |
266 | * Returns whether this import declaration is an on-demand or a |
267 | * single-type import. |
268 | * |
269 | * @return <code>true</code> if this is an on-demand import, |
270 | * and <code>false</code> if this is a single type import |
271 | */ |
272 | public boolean isOnDemand() { |
273 | return this.onDemand; |
274 | } |
275 | |
276 | /** |
277 | * Sets whether this import declaration is an on-demand or a |
278 | * single-type import. |
279 | * |
280 | * @param onDemand <code>true</code> if this is an on-demand import, |
281 | * and <code>false</code> if this is a single type import |
282 | */ |
283 | public void setOnDemand(boolean onDemand) { |
284 | preValueChange(ON_DEMAND_PROPERTY); |
285 | this.onDemand = onDemand; |
286 | postValueChange(ON_DEMAND_PROPERTY); |
287 | } |
288 | |
289 | /** |
290 | * Returns whether this import declaration is a static import (added in JLS3 API). |
291 | * |
292 | * @return <code>true</code> if this is a static import, |
293 | * and <code>false</code> if this is a regular import |
294 | * @exception UnsupportedOperationException if this operation is used in |
295 | * a JLS2 AST |
296 | * @since 3.1 |
297 | */ |
298 | public boolean isStatic() { |
299 | unsupportedIn2(); |
300 | return this.isStatic; |
301 | } |
302 | |
303 | /** |
304 | * Sets whether this import declaration is a static import (added in JLS3 API). |
305 | * |
306 | * @param isStatic <code>true</code> if this is a static import, |
307 | * and <code>false</code> if this is a regular import |
308 | * @exception UnsupportedOperationException if this operation is used in |
309 | * a JLS2 AST |
310 | * @since 3.1 |
311 | */ |
312 | public void setStatic(boolean isStatic) { |
313 | unsupportedIn2(); |
314 | preValueChange(STATIC_PROPERTY); |
315 | this.isStatic = isStatic; |
316 | postValueChange(STATIC_PROPERTY); |
317 | } |
318 | |
319 | /** |
320 | * Resolves and returns the binding for the package, type, field, or |
321 | * method named in this import declaration. |
322 | * <p> |
323 | * The name specified in a non-static single-type import can resolve |
324 | * to a type (only). The name specified in a non-static on-demand |
325 | * import can itself resolve to either a package or a type. |
326 | * For static imports (introduced in JLS3), the name specified in a |
327 | * static on-demand import can itself resolve to a type (only). |
328 | * The name specified in a static single import can resolve to a |
329 | * type, field, or method; in cases where the name could be resolved |
330 | * to more than one element with that name (for example, two |
331 | * methods both named "max", or a method and a field), this method |
332 | * returns one of the plausible bindings. |
333 | * </p> |
334 | * <p> |
335 | * Note that bindings are generally unavailable unless requested when the |
336 | * AST is being built. |
337 | * </p> |
338 | * |
339 | * @return a package, type, field, or method binding, or <code>null</code> |
340 | * if the binding cannot be resolved |
341 | */ |
342 | public IBinding resolveBinding() { |
343 | return this.ast.getBindingResolver().resolveImport(this); |
344 | } |
345 | |
346 | @Override |
347 | int memSize() { |
348 | return BASE_NODE_SIZE + 3 * 4; |
349 | } |
350 | |
351 | @Override |
352 | int treeSize() { |
353 | return |
354 | memSize() |
355 | + (this.importName == null ? 0 : getName().treeSize()); |
356 | } |
357 | } |
358 | |
359 |
Members