1 /*******************************************************************************
2 * Copyright (c) 2000, 2008 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
12 package net.sourceforge.phpdt.core.dom;
14 import java.util.ArrayList;
15 import java.util.List;
18 * Import declaration AST node type.
23 * <b>import</b> Name [ <b>.</b> <b>*</b> ] <b>;</b>
25 * For JLS3, static was added:
28 * <b>import</b> [ <b>static</b> ] Name [ <b>.</b> <b>*</b> ] <b>;</b>
31 * @noinstantiate This class is not intended to be instantiated by clients.
33 public class ImportDeclaration extends ASTNode {
36 * The "name" structural property of this node type.
39 public static final ChildPropertyDescriptor NAME_PROPERTY =
40 new ChildPropertyDescriptor(ImportDeclaration.class, "name", Name.class, MANDATORY, NO_CYCLE_RISK); //$NON-NLS-1$
43 * The "onDemand" structural property of this node type.
46 public static final SimplePropertyDescriptor ON_DEMAND_PROPERTY =
47 new SimplePropertyDescriptor(ImportDeclaration.class, "onDemand", boolean.class, MANDATORY); //$NON-NLS-1$
50 * The "static" structural property of this node type (added in JLS3 API).
53 public static final SimplePropertyDescriptor STATIC_PROPERTY =
54 new SimplePropertyDescriptor(ImportDeclaration.class, "static", boolean.class, MANDATORY); //$NON-NLS-1$
57 * A list of property descriptors (element type:
58 * {@link StructuralPropertyDescriptor}),
59 * or null if uninitialized.
62 private static final List PROPERTY_DESCRIPTORS_2_0;
65 * A list of property descriptors (element type:
66 * {@link StructuralPropertyDescriptor}),
67 * or null if uninitialized.
70 private static final List PROPERTY_DESCRIPTORS_3_0;
73 List properyList = new ArrayList(3);
74 createPropertyList(ImportDeclaration.class, properyList);
75 addProperty(NAME_PROPERTY, properyList);
76 addProperty(ON_DEMAND_PROPERTY, properyList);
77 PROPERTY_DESCRIPTORS_2_0 = reapPropertyList(properyList);
79 properyList = new ArrayList(4);
80 createPropertyList(ImportDeclaration.class, properyList);
81 addProperty(STATIC_PROPERTY, properyList);
82 addProperty(NAME_PROPERTY, properyList);
83 addProperty(ON_DEMAND_PROPERTY, properyList);
84 PROPERTY_DESCRIPTORS_3_0 = reapPropertyList(properyList);
88 * Returns a list of structural property descriptors for this node type.
89 * Clients must not modify the result.
91 * @param apiLevel the API level; one of the
92 * <code>AST.JLS*</code> constants
94 * @return a list of property descriptors (element type:
95 * {@link StructuralPropertyDescriptor})
98 public static List propertyDescriptors(int apiLevel) {
99 if (apiLevel == AST.JLS2_INTERNAL) {
100 return PROPERTY_DESCRIPTORS_2_0;
102 return PROPERTY_DESCRIPTORS_3_0;
107 * The import name; lazily initialized; defaults to a unspecified,
108 * legal Java identifier.
110 private Name importName = null;
113 * On demand versus single type import; defaults to single type import.
115 private boolean onDemand = false;
118 * Static versus regular; defaults to regular import.
119 * Added in JLS3; not used in JLS2.
122 private boolean isStatic = false;
125 * Creates a new AST node for an import declaration owned by the
126 * given AST. The import declaration initially is a regular (non-static)
127 * single type import for an unspecified, but legal, Java type name.
129 * N.B. This constructor is package-private; all subclasses must be
130 * declared in the same package; clients are unable to declare
131 * additional subclasses.
134 * @param ast the AST that is to own this node
136 ImportDeclaration(AST ast) {
140 /* (omit javadoc for this method)
141 * Method declared on ASTNode.
143 final List internalStructuralPropertiesForType(int apiLevel) {
144 return propertyDescriptors(apiLevel);
147 /* (omit javadoc for this method)
148 * Method declared on ASTNode.
150 final boolean internalGetSetBooleanProperty(SimplePropertyDescriptor property, boolean get, boolean value) {
151 if (property == ON_DEMAND_PROPERTY) {
159 if (property == STATIC_PROPERTY) {
167 // allow default implementation to flag the error
168 return super.internalGetSetBooleanProperty(property, get, value);
171 /* (omit javadoc for this method)
172 * Method declared on ASTNode.
174 final ASTNode internalGetSetChildProperty(ChildPropertyDescriptor property, boolean get, ASTNode child) {
175 if (property == NAME_PROPERTY) {
179 setName((Name) child);
183 // allow default implementation to flag the error
184 return super.internalGetSetChildProperty(property, get, child);
187 /* (omit javadoc for this method)
188 * Method declared on ASTNode.
190 final int getNodeType0() {
191 return IMPORT_DECLARATION;
194 /* (omit javadoc for this method)
195 * Method declared on ASTNode.
197 ASTNode clone0(AST target) {
198 ImportDeclaration result = new ImportDeclaration(target);
199 result.setSourceRange(this.getStartPosition(), this.getLength());
200 result.setOnDemand(isOnDemand());
201 if (this.ast.apiLevel >= AST.JLS3) {
202 result.setStatic(isStatic());
204 result.setName((Name) getName().clone(target));
208 /* (omit javadoc for this method)
209 * Method declared on ASTNode.
211 final boolean subtreeMatch0(ASTMatcher matcher, Object other) {
212 // dispatch to correct overloaded match method
213 return matcher.match(this, other);
216 /* (omit javadoc for this method)
217 * Method declared on ASTNode.
219 void accept0(ASTVisitor visitor) {
220 boolean visitChildren = visitor.visit(this);
222 acceptChild(visitor, getName());
224 visitor.endVisit(this);
228 * Returns the name imported by this declaration.
230 * For a regular on-demand import, this is the name of a package.
231 * For a static on-demand import, this is the qualified name of
232 * a type. For a regular single-type import, this is the qualified name
233 * of a type. For a static single-type import, this is the qualified name
234 * of a static member of a type.
237 * @return the imported name node
239 public Name getName() {
240 if (this.importName == null) {
241 // lazy init must be thread-safe for readers
242 synchronized (this) {
243 if (this.importName == null) {
245 this.importName =this.ast.newQualifiedName(
246 new SimpleName(this.ast), new SimpleName(this.ast));
247 postLazyInit(this.importName, NAME_PROPERTY);
255 * Sets the name of this import declaration to the given name.
257 * For a regular on-demand import, this is the name of a package.
258 * For a static on-demand import, this is the qualified name of
259 * a type. For a regular single-type import, this is the qualified name
260 * of a type. For a static single-type import, this is the qualified name
261 * of a static member of a type.
264 * @param name the new import name
265 * @exception IllegalArgumentException if:
267 * <li>the node belongs to a different AST</li>
268 * <li>the node already has a parent</li>
271 public void setName(Name name) {
273 throw new IllegalArgumentException();
275 ASTNode oldChild = this.importName;
276 preReplaceChild(oldChild, name, NAME_PROPERTY);
277 this.importName = name;
278 postReplaceChild(oldChild, name, NAME_PROPERTY);
282 * Returns whether this import declaration is an on-demand or a
283 * single-type import.
285 * @return <code>true</code> if this is an on-demand import,
286 * and <code>false</code> if this is a single type import
288 public boolean isOnDemand() {
293 * Sets whether this import declaration is an on-demand or a
294 * single-type import.
296 * @param onDemand <code>true</code> if this is an on-demand import,
297 * and <code>false</code> if this is a single type import
299 public void setOnDemand(boolean onDemand) {
300 preValueChange(ON_DEMAND_PROPERTY);
301 this.onDemand = onDemand;
302 postValueChange(ON_DEMAND_PROPERTY);
306 * Returns whether this import declaration is a static import (added in JLS3 API).
308 * @return <code>true</code> if this is a static import,
309 * and <code>false</code> if this is a regular import
310 * @exception UnsupportedOperationException if this operation is used in
314 public boolean isStatic() {
320 * Sets whether this import declaration is a static import (added in JLS3 API).
322 * @param isStatic <code>true</code> if this is a static import,
323 * and <code>false</code> if this is a regular import
324 * @exception UnsupportedOperationException if this operation is used in
328 public void setStatic(boolean isStatic) {
330 preValueChange(STATIC_PROPERTY);
331 this.isStatic = isStatic;
332 postValueChange(STATIC_PROPERTY);
336 * Resolves and returns the binding for the package, type, field, or
337 * method named in this import declaration.
339 * The name specified in a non-static single-type import can resolve
340 * to a type (only). The name specified in a non-static on-demand
341 * import can itself resolve to either a package or a type.
342 * For static imports (introduced in JLS3), the name specified in a
343 * static on-demand import can itself resolve to a type (only).
344 * The name specified in a static single import can resolve to a
345 * type, field, or method; in cases where the name could be resolved
346 * to more than one element with that name (for example, two
347 * methods both named "max", or a method and a field), this method
348 * returns one of the plausible bindings.
351 * Note that bindings are generally unavailable unless requested when the
352 * AST is being built.
355 * @return a package, type, field, or method binding, or <code>null</code>
356 * if the binding cannot be resolved
358 public IBinding resolveBinding() {
359 return this.ast.getBindingResolver().resolveImport(this);
362 /* (omit javadoc for this method)
363 * Method declared on ASTNode.
366 return BASE_NODE_SIZE + 3 * 4;
369 /* (omit javadoc for this method)
370 * Method declared on ASTNode.
375 + (importName == null ? 0 : getName().treeSize());