removed unused private methods.
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / core / dom / ImportDeclaration.java
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
7  *
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  *******************************************************************************/
11
12 package net.sourceforge.phpdt.core.dom;
13
14 import java.util.ArrayList;
15 import java.util.List;
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  * @noinstantiate This class is not intended to be instantiated by clients.
32  */
33 public class ImportDeclaration extends ASTNode {
34         
35         /**
36          * The "name" structural property of this node type.
37          * @since 3.0
38          */
39         public static final ChildPropertyDescriptor NAME_PROPERTY = 
40                 new ChildPropertyDescriptor(ImportDeclaration.class, "name", Name.class, MANDATORY, NO_CYCLE_RISK); //$NON-NLS-1$
41
42         /**
43          * The "onDemand" structural property of this node type.
44          * @since 3.0
45          */
46         public static final SimplePropertyDescriptor ON_DEMAND_PROPERTY = 
47                 new SimplePropertyDescriptor(ImportDeclaration.class, "onDemand", boolean.class, MANDATORY); //$NON-NLS-1$
48         
49         /**
50          * The "static" structural property of this node type (added in JLS3 API).
51          * @since 3.1
52          */
53         public static final SimplePropertyDescriptor STATIC_PROPERTY = 
54                 new SimplePropertyDescriptor(ImportDeclaration.class, "static", boolean.class, MANDATORY); //$NON-NLS-1$
55         
56         /**
57          * A list of property descriptors (element type: 
58          * {@link StructuralPropertyDescriptor}),
59          * or null if uninitialized.
60          * @since 3.0
61          */
62         private static final List PROPERTY_DESCRIPTORS_2_0;
63         
64         /**
65          * A list of property descriptors (element type: 
66          * {@link StructuralPropertyDescriptor}),
67          * or null if uninitialized.
68          * @since 3.1
69          */
70         private static final List PROPERTY_DESCRIPTORS_3_0;
71         
72         static {
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);
78                 
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);
85         }
86
87         /**
88          * Returns a list of structural property descriptors for this node type.
89          * Clients must not modify the result.
90          * 
91          * @param apiLevel the API level; one of the
92          * <code>AST.JLS*</code> constants
93
94          * @return a list of property descriptors (element type: 
95          * {@link StructuralPropertyDescriptor})
96          * @since 3.0
97          */
98         public static List propertyDescriptors(int apiLevel) {
99                 if (apiLevel == AST.JLS2_INTERNAL) {
100                         return PROPERTY_DESCRIPTORS_2_0;
101                 } else {
102                         return PROPERTY_DESCRIPTORS_3_0;
103                 }
104         }
105                         
106         /**
107          * The import name; lazily initialized; defaults to a unspecified,
108          * legal Java identifier.
109          */
110         private Name importName = null;
111
112         /**
113          * On demand versus single type import; defaults to single type import.
114          */
115         private boolean onDemand = false;
116
117         /**
118          * Static versus regular; defaults to regular import.
119          * Added in JLS3; not used in JLS2.
120          * @since 3.1
121          */
122         private boolean isStatic = false;
123
124         /**
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.
128          * <p>
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.
132          * </p>
133          * 
134          * @param ast the AST that is to own this node
135          */
136         ImportDeclaration(AST ast) {
137                 super(ast);
138         }
139
140         /* (omit javadoc for this method)
141          * Method declared on ASTNode.
142          */
143         final List internalStructuralPropertiesForType(int apiLevel) {
144                 return propertyDescriptors(apiLevel);
145         }
146         
147         /* (omit javadoc for this method)
148          * Method declared on ASTNode.
149          */
150         final boolean internalGetSetBooleanProperty(SimplePropertyDescriptor property, boolean get, boolean value) {
151                 if (property == ON_DEMAND_PROPERTY) {
152                         if (get) {
153                                 return isOnDemand();
154                         } else {
155                                 setOnDemand(value);
156                                 return false;
157                         }
158                 }
159                 if (property == STATIC_PROPERTY) {
160                         if (get) {
161                                 return isStatic();
162                         } else {
163                                 setStatic(value);
164                                 return false;
165                         }
166                 }
167                 // allow default implementation to flag the error
168                 return super.internalGetSetBooleanProperty(property, get, value);
169         }
170         
171         /* (omit javadoc for this method)
172          * Method declared on ASTNode.
173          */
174         final ASTNode internalGetSetChildProperty(ChildPropertyDescriptor property, boolean get, ASTNode child) {
175                 if (property == NAME_PROPERTY) {
176                         if (get) {
177                                 return getName();
178                         } else {
179                                 setName((Name) child);
180                                 return null;
181                         }
182                 }
183                 // allow default implementation to flag the error
184                 return super.internalGetSetChildProperty(property, get, child);
185         }
186         
187         /* (omit javadoc for this method)
188          * Method declared on ASTNode.
189          */
190         final int getNodeType0() {
191                 return IMPORT_DECLARATION;
192         }
193
194         /* (omit javadoc for this method)
195          * Method declared on ASTNode.
196          */
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());
203                 }
204                 result.setName((Name) getName().clone(target));
205                 return result;
206         }
207
208         /* (omit javadoc for this method)
209          * Method declared on ASTNode.
210          */
211         final boolean subtreeMatch0(ASTMatcher matcher, Object other) {
212                 // dispatch to correct overloaded match method
213                 return matcher.match(this, other);
214         }
215
216         /* (omit javadoc for this method)
217          * Method declared on ASTNode.
218          */
219         void accept0(ASTVisitor visitor) {
220                 boolean visitChildren = visitor.visit(this);
221                 if (visitChildren) {
222                         acceptChild(visitor, getName());
223                 }
224                 visitor.endVisit(this);
225         }
226         
227         /**
228          * Returns the name imported by this declaration.
229          * <p>
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.
235          * </p>
236          * 
237          * @return the imported name node
238          */ 
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) {
244                                         preLazyInit();
245                                         this.importName =this.ast.newQualifiedName(
246                                                         new SimpleName(this.ast), new SimpleName(this.ast));
247                                         postLazyInit(this.importName, NAME_PROPERTY);
248                                 }
249                         }
250                 }
251                 return importName;
252         }
253         
254         /**
255          * Sets the name of this import declaration to the given name.
256          * <p>
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.
262          * </p>
263          * 
264          * @param name the new import name
265          * @exception IllegalArgumentException if:
266          * <ul>
267          * <li>the node belongs to a different AST</li>
268          * <li>the node already has a parent</li>
269          * </ul>
270          */ 
271         public void setName(Name name) {
272                 if (name == null) {
273                         throw new IllegalArgumentException();
274                 }
275                 ASTNode oldChild = this.importName;
276                 preReplaceChild(oldChild, name, NAME_PROPERTY);
277                 this.importName = name;
278                 postReplaceChild(oldChild, name, NAME_PROPERTY);
279         }
280                 
281         /**
282          * Returns whether this import declaration is an on-demand or a 
283          * single-type import.
284          * 
285          * @return <code>true</code> if this is an on-demand import,
286          *    and <code>false</code> if this is a single type import
287          */ 
288         public boolean isOnDemand() {
289                 return onDemand;
290         }
291                 
292         /**
293          * Sets whether this import declaration is an on-demand or a 
294          * single-type import.
295          * 
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
298          */ 
299         public void setOnDemand(boolean onDemand) {
300                 preValueChange(ON_DEMAND_PROPERTY);
301                 this.onDemand = onDemand;
302                 postValueChange(ON_DEMAND_PROPERTY);
303         }
304         
305         /**
306          * Returns whether this import declaration is a static import (added in JLS3 API).
307          * 
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
311          * a JLS2 AST
312          * @since 3.1
313          */ 
314         public boolean isStatic() {
315                 unsupportedIn2();
316                 return isStatic;
317         }
318                 
319         /**
320          * Sets whether this import declaration is a static import (added in JLS3 API).
321          * 
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
325          * a JLS2 AST
326          * @since 3.1
327          */ 
328         public void setStatic(boolean isStatic) {
329                 unsupportedIn2();
330                 preValueChange(STATIC_PROPERTY);
331                 this.isStatic = isStatic;
332                 postValueChange(STATIC_PROPERTY);
333         }
334         
335         /**
336          * Resolves and returns the binding for the package, type, field, or
337          * method named in this import declaration.
338          * <p>
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.
349          * </p>
350          * <p>
351          * Note that bindings are generally unavailable unless requested when the
352          * AST is being built.
353          * </p>
354          * 
355          * @return a package, type, field, or method binding, or <code>null</code>
356          * if the binding cannot be resolved
357          */     
358         public IBinding resolveBinding() {
359                 return this.ast.getBindingResolver().resolveImport(this);
360         }
361         
362         /* (omit javadoc for this method)
363          * Method declared on ASTNode.
364          */
365         int memSize() {
366                 return BASE_NODE_SIZE + 3 * 4;
367         }
368         
369         /* (omit javadoc for this method)
370          * Method declared on ASTNode.
371          */
372         int treeSize() {
373                 return
374                         memSize()
375                         + (importName == null ? 0 : getName().treeSize());
376         }
377 }
378