KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > core > dom > ImportDeclaration


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 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
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11
12 package org.eclipse.jdt.core.dom;
13
14 import java.util.ArrayList JavaDoc;
15 import java.util.List JavaDoc;
16
17 /**
18  * Import declaration AST node type.
19  *
20  * For JLS2:
21  * <pre>
22  * ImportDeclaration:
23  * <b>import</b> Name [ <b>.</b> <b>*</b> ] <b>;</b>
24  * </pre>
25  * For JLS3, static was added:
26  * <pre>
27  * ImportDeclaration:
28  * <b>import</b> [ <b>static</b> ] Name [ <b>.</b> <b>*</b> ] <b>;</b>
29  * </pre>
30  * @since 2.0
31  */

32 public class ImportDeclaration extends ASTNode {
33     
34     /**
35      * The "name" structural property of this node type.
36      * @since 3.0
37      */

38     public static final ChildPropertyDescriptor NAME_PROPERTY =
39         new ChildPropertyDescriptor(ImportDeclaration.class, "name", Name.class, MANDATORY, NO_CYCLE_RISK); //$NON-NLS-1$
40

41     /**
42      * The "onDemand" structural property of this node type.
43      * @since 3.0
44      */

45     public static final SimplePropertyDescriptor ON_DEMAND_PROPERTY =
46         new SimplePropertyDescriptor(ImportDeclaration.class, "onDemand", boolean.class, MANDATORY); //$NON-NLS-1$
47

48     /**
49      * The "static" structural property of this node type (added in JLS3 API).
50      * @since 3.1
51      */

52     public static final SimplePropertyDescriptor STATIC_PROPERTY =
53         new SimplePropertyDescriptor(ImportDeclaration.class, "static", boolean.class, MANDATORY); //$NON-NLS-1$
54

55     /**
56      * A list of property descriptors (element type:
57      * {@link StructuralPropertyDescriptor}),
58      * or null if uninitialized.
59      * @since 3.0
60      */

61     private static final List JavaDoc PROPERTY_DESCRIPTORS_2_0;
62     
63     /**
64      * A list of property descriptors (element type:
65      * {@link StructuralPropertyDescriptor}),
66      * or null if uninitialized.
67      * @since 3.1
68      */

69     private static final List JavaDoc PROPERTY_DESCRIPTORS_3_0;
70     
71     static {
72         List JavaDoc properyList = new ArrayList JavaDoc(3);
73         createPropertyList(ImportDeclaration.class, properyList);
74         addProperty(NAME_PROPERTY, properyList);
75         addProperty(ON_DEMAND_PROPERTY, properyList);
76         PROPERTY_DESCRIPTORS_2_0 = reapPropertyList(properyList);
77         
78         properyList = new ArrayList JavaDoc(4);
79         createPropertyList(ImportDeclaration.class, properyList);
80         addProperty(STATIC_PROPERTY, properyList);
81         addProperty(NAME_PROPERTY, properyList);
82         addProperty(ON_DEMAND_PROPERTY, properyList);
83         PROPERTY_DESCRIPTORS_3_0 = reapPropertyList(properyList);
84     }
85
86     /**
87      * Returns a list of structural property descriptors for this node type.
88      * Clients must not modify the result.
89      *
90      * @param apiLevel the API level; one of the
91      * <code>AST.JLS&ast;</code> constants
92
93      * @return a list of property descriptors (element type:
94      * {@link StructuralPropertyDescriptor})
95      * @since 3.0
96      */

97     public static List JavaDoc propertyDescriptors(int apiLevel) {
98         if (apiLevel == AST.JLS2_INTERNAL) {
99             return PROPERTY_DESCRIPTORS_2_0;
100         } else {
101             return PROPERTY_DESCRIPTORS_3_0;
102         }
103     }
104             
105     /**
106      * The import name; lazily initialized; defaults to a unspecified,
107      * legal Java identifier.
108      */

109     private Name importName = null;
110
111     /**
112      * On demand versus single type import; defaults to single type import.
113      */

114     private boolean onDemand = false;
115
116     /**
117      * Static versus regular; defaults to regular import.
118      * Added in JLS3; not used in JLS2.
119      * @since 3.1
120      */

121     private boolean isStatic = false;
122
123     /**
124      * Creates a new AST node for an import declaration owned by the
125      * given AST. The import declaration initially is a regular (non-static)
126      * single type import for an unspecified, but legal, Java type name.
127      * <p>
128      * N.B. This constructor is package-private; all subclasses must be
129      * declared in the same package; clients are unable to declare
130      * additional subclasses.
131      * </p>
132      *
133      * @param ast the AST that is to own this node
134      */

135     ImportDeclaration(AST ast) {
136         super(ast);
137     }
138
139     /* (omit javadoc for this method)
140      * Method declared on ASTNode.
141      */

142     final List JavaDoc internalStructuralPropertiesForType(int apiLevel) {
143         return propertyDescriptors(apiLevel);
144     }
145     
146     /* (omit javadoc for this method)
147      * Method declared on ASTNode.
148      */

149     final boolean internalGetSetBooleanProperty(SimplePropertyDescriptor property, boolean get, boolean value) {
150         if (property == ON_DEMAND_PROPERTY) {
151             if (get) {
152                 return isOnDemand();
153             } else {
154                 setOnDemand(value);
155                 return false;
156             }
157         }
158         if (property == STATIC_PROPERTY) {
159             if (get) {
160                 return isStatic();
161             } else {
162                 setStatic(value);
163                 return false;
164             }
165         }
166         // allow default implementation to flag the error
167
return super.internalGetSetBooleanProperty(property, get, value);
168     }
169     
170     /* (omit javadoc for this method)
171      * Method declared on ASTNode.
172      */

173     final ASTNode internalGetSetChildProperty(ChildPropertyDescriptor property, boolean get, ASTNode child) {
174         if (property == NAME_PROPERTY) {
175             if (get) {
176                 return getName();
177             } else {
178                 setName((Name) child);
179                 return null;
180             }
181         }
182         // allow default implementation to flag the error
183
return super.internalGetSetChildProperty(property, get, child);
184     }
185     
186     /* (omit javadoc for this method)
187      * Method declared on ASTNode.
188      */

189     final int getNodeType0() {
190         return IMPORT_DECLARATION;
191     }
192
193     /* (omit javadoc for this method)
194      * Method declared on ASTNode.
195      */

196     ASTNode clone0(AST target) {
197         ImportDeclaration result = new ImportDeclaration(target);
198         result.setSourceRange(this.getStartPosition(), this.getLength());
199         result.setOnDemand(isOnDemand());
200         if (this.ast.apiLevel >= AST.JLS3) {
201             result.setStatic(isStatic());
202         }
203         result.setName((Name) getName().clone(target));
204         return result;
205     }
206
207     /* (omit javadoc for this method)
208      * Method declared on ASTNode.
209      */

210     final boolean subtreeMatch0(ASTMatcher matcher, Object JavaDoc other) {
211         // dispatch to correct overloaded match method
212
return matcher.match(this, other);
213     }
214
215     /* (omit javadoc for this method)
216      * Method declared on ASTNode.
217      */

218     void accept0(ASTVisitor visitor) {
219         boolean visitChildren = visitor.visit(this);
220         if (visitChildren) {
221             acceptChild(visitor, getName());
222         }
223         visitor.endVisit(this);
224     }
225     
226     /**
227      * Returns the name imported by this declaration.
228      * <p>
229      * For a regular on-demand import, this is the name of a package.
230      * For a static on-demand import, this is the qualified name of
231      * a type. For a regular single-type import, this is the qualified name
232      * of a type. For a static single-type import, this is the qualified name
233      * of a static member of a type.
234      * </p>
235      *
236      * @return the imported name node
237      */

238     public Name getName() {
239         if (this.importName == null) {
240             // lazy init must be thread-safe for readers
241
synchronized (this) {
242                 if (this.importName == null) {
243                     preLazyInit();
244                     this.importName =this.ast.newQualifiedName(
245                             new SimpleName(this.ast), new SimpleName(this.ast));
246                     postLazyInit(this.importName, NAME_PROPERTY);
247                 }
248             }
249         }
250         return importName;
251     }
252     
253     /**
254      * Sets the name of this import declaration to the given name.
255      * <p>
256      * For a regular on-demand import, this is the name of a package.
257      * For a static on-demand import, this is the qualified name of
258      * a type. For a regular single-type import, this is the qualified name
259      * of a type. For a static single-type import, this is the qualified name
260      * of a static member of a type.
261      * </p>
262      *
263      * @param name the new import name
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      * </ul>
269      */

270     public void setName(Name name) {
271         if (name == null) {
272             throw new IllegalArgumentException JavaDoc();
273         }
274         ASTNode oldChild = this.importName;
275         preReplaceChild(oldChild, name, NAME_PROPERTY);
276         this.importName = name;
277         postReplaceChild(oldChild, name, NAME_PROPERTY);
278     }
279         
280     /**
281      * Returns whether this import declaration is an on-demand or a
282      * single-type import.
283      *
284      * @return <code>true</code> if this is an on-demand import,
285      * and <code>false</code> if this is a single type import
286      */

287     public boolean isOnDemand() {
288         return onDemand;
289     }
290         
291     /**
292      * Sets whether this import declaration is an on-demand or a
293      * single-type import.
294      *
295      * @param onDemand <code>true</code> if this is an on-demand import,
296      * and <code>false</code> if this is a single type import
297      */

298     public void setOnDemand(boolean onDemand) {
299         preValueChange(ON_DEMAND_PROPERTY);
300         this.onDemand = onDemand;
301         postValueChange(ON_DEMAND_PROPERTY);
302     }
303     
304     /**
305      * Returns whether this import declaration is a static import (added in JLS3 API).
306      *
307      * @return <code>true</code> if this is a static import,
308      * and <code>false</code> if this is a regular import
309      * @exception UnsupportedOperationException if this operation is used in
310      * a JLS2 AST
311      * @since 3.1
312      */

313     public boolean isStatic() {
314         unsupportedIn2();
315         return isStatic;
316     }
317         
318     /**
319      * Sets whether this import declaration is a static import (added in JLS3 API).
320      *
321      * @param isStatic <code>true</code> if this is a static import,
322      * and <code>false</code> if this is a regular import
323      * @exception UnsupportedOperationException if this operation is used in
324      * a JLS2 AST
325      * @since 3.1
326      */

327     public void setStatic(boolean isStatic) {
328         unsupportedIn2();
329         preValueChange(STATIC_PROPERTY);
330         this.isStatic = isStatic;
331         postValueChange(STATIC_PROPERTY);
332     }
333     
334     /**
335      * Resolves and returns the binding for the package, type, field, or
336      * method named in this import declaration.
337      * <p>
338      * The name specified in a non-static single-type import can resolve
339      * to a type (only). The name specified in a non-static on-demand
340      * import can itself resolve to either a package or a type.
341      * For static imports (introduced in JLS3), the name specified in a
342      * static on-demand import can itself resolve to a type (only).
343      * The name specified in a static single import can resolve to a
344      * type, field, or method; in cases where the name could be resolved
345      * to more than one element with that name (for example, two
346      * methods both named "max", or a method and a field), this method
347      * returns one of the plausible bindings.
348      * </p>
349      * <p>
350      * Note that bindings are generally unavailable unless requested when the
351      * AST is being built.
352      * </p>
353      *
354      * @return a package, type, field, or method binding, or <code>null</code>
355      * if the binding cannot be resolved
356      */

357     public IBinding resolveBinding() {
358         return this.ast.getBindingResolver().resolveImport(this);
359     }
360     
361     /* (omit javadoc for this method)
362      * Method declared on ASTNode.
363      */

364     int memSize() {
365         return BASE_NODE_SIZE + 3 * 4;
366     }
367     
368     /* (omit javadoc for this method)
369      * Method declared on ASTNode.
370      */

371     int treeSize() {
372         return
373             memSize()
374             + (importName == null ? 0 : getName().treeSize());
375     }
376 }
377
378
Popular Tags