KickJava   Java API By Example, From Geeks To Geeks.

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


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 /**
19  * AST node for a qualified name. A qualified name is defined recursively
20  * as a simple name preceded by a name, which qualifies it. Expressing it this
21  * way means that the qualifier and the simple name get their own AST nodes.
22  * <pre>
23  * QualifiedName:
24  * Name <b>.</b> SimpleName
25  * </pre>
26  * <p>
27  * See <code>FieldAccess</code> for guidelines on handling other expressions
28  * that resemble qualified names.
29  * </p>
30  *
31  * @see FieldAccess
32  * @since 2.0
33  */

34 public class QualifiedName extends Name {
35     
36     /**
37      * The "qualifier" structural property of this node type.
38      * @since 3.0
39      */

40     public static final ChildPropertyDescriptor QUALIFIER_PROPERTY =
41         new ChildPropertyDescriptor(QualifiedName.class, "qualifier", Name.class, MANDATORY, CYCLE_RISK); //$NON-NLS-1$
42

43     /**
44      * The "name" structural property of this node type.
45      * @since 3.0
46      */

47     public static final ChildPropertyDescriptor NAME_PROPERTY =
48         new ChildPropertyDescriptor(QualifiedName.class, "name", SimpleName.class, MANDATORY, NO_CYCLE_RISK); //$NON-NLS-1$
49

50     /**
51      * A list of property descriptors (element type:
52      * {@link StructuralPropertyDescriptor}),
53      * or null if uninitialized.
54      */

55     private static final List JavaDoc PROPERTY_DESCRIPTORS;
56     
57     static {
58         List JavaDoc propertyList = new ArrayList JavaDoc(3);
59         createPropertyList(QualifiedName.class, propertyList);
60         addProperty(QUALIFIER_PROPERTY, propertyList);
61         addProperty(NAME_PROPERTY, propertyList);
62         PROPERTY_DESCRIPTORS = reapPropertyList(propertyList);
63     }
64
65     /**
66      * Returns a list of structural property descriptors for this node type.
67      * Clients must not modify the result.
68      *
69      * @param apiLevel the API level; one of the
70      * <code>AST.JLS&ast;</code> constants
71      * @return a list of property descriptors (element type:
72      * {@link StructuralPropertyDescriptor})
73      * @since 3.0
74      */

75     public static List JavaDoc propertyDescriptors(int apiLevel) {
76         return PROPERTY_DESCRIPTORS;
77     }
78             
79     /**
80      * The identifier; lazily initialized; defaults to a unspecified, legal
81      * Java identifier.
82      */

83     private Name qualifier = null;
84     
85     /**
86      * The name being qualified; lazily initialized; defaults to a unspecified,
87      * legal Java identifier.
88      */

89     private SimpleName name = null;
90     
91     /**
92      * Creates a new AST node for a qualified name owned by the given AST.
93      * <p>
94      * N.B. This constructor is package-private; all subclasses must be
95      * declared in the same package; clients are unable to declare
96      * additional subclasses.
97      * </p>
98      *
99      * @param ast the AST that is to own this node
100      */

101     QualifiedName(AST ast) {
102         super(ast);
103     }
104     
105     /* (omit javadoc for this method)
106      * Method declared on ASTNode.
107      */

108     final List JavaDoc internalStructuralPropertiesForType(int apiLevel) {
109         return propertyDescriptors(apiLevel);
110     }
111     
112     /* (omit javadoc for this method)
113      * Method declared on ASTNode.
114      */

115     final ASTNode internalGetSetChildProperty(ChildPropertyDescriptor property, boolean get, ASTNode child) {
116         if (property == QUALIFIER_PROPERTY) {
117             if (get) {
118                 return getQualifier();
119             } else {
120                 setQualifier((Name) child);
121                 return null;
122             }
123         }
124         if (property == NAME_PROPERTY) {
125             if (get) {
126                 return getName();
127             } else {
128                 setName((SimpleName) child);
129                 return null;
130             }
131         }
132         // allow default implementation to flag the error
133
return super.internalGetSetChildProperty(property, get, child);
134     }
135     
136     /* (omit javadoc for this method)
137      * Method declared on ASTNode.
138      */

139     final int getNodeType0() {
140         return QUALIFIED_NAME;
141     }
142
143     /* (omit javadoc for this method)
144      * Method declared on ASTNode.
145      */

146     ASTNode clone0(AST target) {
147         QualifiedName result = new QualifiedName(target);
148         result.setSourceRange(this.getStartPosition(), this.getLength());
149         result.setQualifier((Name) getQualifier().clone(target));
150         result.setName((SimpleName) getName().clone(target));
151         return result;
152     }
153
154     /* (omit javadoc for this method)
155      * Method declared on ASTNode.
156      */

157     final boolean subtreeMatch0(ASTMatcher matcher, Object JavaDoc other) {
158         // dispatch to correct overloaded match method
159
return matcher.match(this, other);
160     }
161
162     /* (omit javadoc for this method)
163      * Method declared on ASTNode.
164      */

165     void accept0(ASTVisitor visitor) {
166         boolean visitChildren = visitor.visit(this);
167         if (visitChildren) {
168             // visit children in normal left to right reading order
169
acceptChild(visitor, getQualifier());
170             acceptChild(visitor, getName());
171         }
172         visitor.endVisit(this);
173     }
174     
175     /**
176      * Returns the qualifier part of this qualified name.
177      *
178      * @return the qualifier part of this qualified name
179      */

180     public Name getQualifier() {
181         if (this.qualifier == null) {
182             // lazy init must be thread-safe for readers
183
synchronized (this) {
184                 if (this.qualifier == null) {
185                     preLazyInit();
186                     this.qualifier = new SimpleName(this.ast);
187                     postLazyInit(this.qualifier, QUALIFIER_PROPERTY);
188                 }
189             }
190         }
191         return this.qualifier;
192     }
193     
194     /**
195      * Sets the qualifier of this qualified name to the given name.
196      *
197      * @param qualifier the qualifier of this qualified name
198      * @exception IllegalArgumentException if:
199      * <ul>
200      * <li>the node belongs to a different AST</li>
201      * <li>the node already has a parent</li>
202      * <li>a cycle in would be created</li>
203      * </ul>
204      */

205     public void setQualifier(Name qualifier) {
206         if (qualifier == null) {
207             throw new IllegalArgumentException JavaDoc();
208         }
209         ASTNode oldChild = this.qualifier;
210         preReplaceChild(oldChild, qualifier, QUALIFIER_PROPERTY);
211         this.qualifier = qualifier;
212         postReplaceChild(oldChild, qualifier, QUALIFIER_PROPERTY);
213     }
214     
215     /**
216      * Returns the name part of this qualified name.
217      *
218      * @return the name being qualified
219      */

220     public SimpleName getName() {
221         if (this.name == null) {
222             // lazy init must be thread-safe for readers
223
synchronized (this) {
224                 if (this.name == null) {
225                     preLazyInit();
226                     this.name = new SimpleName(this.ast);
227                     postLazyInit(this.name, NAME_PROPERTY);
228                 }
229             }
230         }
231         return this.name;
232     }
233     
234     /**
235      * Sets the name part of this qualified name to the given simple name.
236      *
237      * @param name the identifier of this qualified name
238      * @exception IllegalArgumentException if:
239      * <ul>
240      * <li>the node belongs to a different AST</li>
241      * <li>the node already has a parent</li>
242      * </ul>
243      */

244     public void setName(SimpleName name) {
245         if (name == null) {
246             throw new IllegalArgumentException JavaDoc();
247         }
248         ASTNode oldChild = this.name;
249         preReplaceChild(oldChild, name, NAME_PROPERTY);
250         this.name = name;
251         postReplaceChild(oldChild, name, NAME_PROPERTY);
252     }
253     
254     /* (omit javadoc for this method)
255      * Method declared on Name.
256      */

257     void appendName(StringBuffer JavaDoc buffer) {
258         getQualifier().appendName(buffer);
259         buffer.append('.');
260         getName().appendName(buffer);
261     }
262
263     /* (omit javadoc for this method)
264      * Method declared on ASTNode.
265      */

266     int memSize() {
267         return BASE_NAME_NODE_SIZE + 3 * 4;
268     }
269     
270     /* (omit javadoc for this method)
271      * Method declared on ASTNode.
272      */

273     int treeSize() {
274         return
275             memSize()
276             + (this.name == null ? 0 : getName().treeSize())
277             + (this.qualifier == null ? 0 : getQualifier().treeSize());
278     }
279 }
280
281
Popular Tags