KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2003, 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  * Type node for a qualified type (added in JLS3 API).
19  * <pre>
20  * QualifiedType:
21  * Type <b>.</b> SimpleName
22  * </pre>
23  * <p>
24  * Not all node arragements will represent legal Java constructs. In particular,
25  * it is nonsense if the type is an array type or primitive type. The normal use
26  * is when the type is a simple or parameterized type.
27  * </p>
28  * <p>
29  * A type like "A.B" can be represented either of two ways:
30  * <ol>
31  * <li>
32  * <code>QualifiedType(SimpleType(SimpleName("A")),SimpleName("B"))</code>
33  * </li>
34  * <li>
35  * <code>SimpleType(QualifiedName(SimpleName("A"),SimpleName("B")))</code>
36  * </li>
37  * </ol>
38  * The first form is preferred when "A" is known to be a type. However, a
39  * parser cannot always determine this. Clients should be prepared to handle
40  * either rather than make assumptions. (Note also that the first form
41  * became possible as of JLS3; only the second form existed in JLS2 API.)
42  * </p>
43  *
44  * @since 3.1
45  */

46 public class QualifiedType extends Type {
47     /**
48      * This index represents the position inside a parameterized qualified type.
49      */

50     int index;
51     
52     /**
53      * The "qualifier" structural property of this node type.
54      */

55     public static final ChildPropertyDescriptor QUALIFIER_PROPERTY =
56         new ChildPropertyDescriptor(QualifiedType.class, "qualifier", Type.class, MANDATORY, CYCLE_RISK); //$NON-NLS-1$
57

58     /**
59      * The "name" structural property of this node type.
60      */

61     public static final ChildPropertyDescriptor NAME_PROPERTY =
62         new ChildPropertyDescriptor(QualifiedType.class, "name", SimpleName.class, MANDATORY, NO_CYCLE_RISK); //$NON-NLS-1$
63

64     /**
65      * A list of property descriptors (element type:
66      * {@link StructuralPropertyDescriptor}),
67      * or null if uninitialized.
68      */

69     private static final List JavaDoc PROPERTY_DESCRIPTORS;
70     
71     static {
72         List JavaDoc propertyList = new ArrayList JavaDoc(3);
73         createPropertyList(QualifiedType.class, propertyList);
74         addProperty(QUALIFIER_PROPERTY, propertyList);
75         addProperty(NAME_PROPERTY, propertyList);
76         PROPERTY_DESCRIPTORS = reapPropertyList(propertyList);
77     }
78
79     /**
80      * Returns a list of structural property descriptors for this node type.
81      * Clients must not modify the result.
82      *
83      * @param apiLevel the API level; one of the
84      * <code>AST.JLS&ast;</code> constants
85      * @return a list of property descriptors (element type:
86      * {@link StructuralPropertyDescriptor})
87      */

88     public static List JavaDoc propertyDescriptors(int apiLevel) {
89         return PROPERTY_DESCRIPTORS;
90     }
91             
92     /**
93      * The type node; lazily initialized; defaults to a type with
94      * an unspecfied, but legal, simple name.
95      */

96     private Type qualifier = null;
97     
98     /**
99      * The name being qualified; lazily initialized; defaults to a unspecified,
100      * legal Java identifier.
101      */

102     private SimpleName name = null;
103
104     /**
105      * Creates a new unparented node for a qualified type owned by the
106      * given AST. By default, an unspecified, but legal, qualifier and name.
107      * <p>
108      * N.B. This constructor is package-private.
109      * </p>
110      *
111      * @param ast the AST that is to own this node
112      */

113     QualifiedType(AST ast) {
114         super(ast);
115         unsupportedIn2();
116     }
117
118     /* (omit javadoc for this method)
119      * Method declared on ASTNode.
120      */

121     final List JavaDoc internalStructuralPropertiesForType(int apiLevel) {
122         return propertyDescriptors(apiLevel);
123     }
124     
125     /* (omit javadoc for this method)
126      * Method declared on ASTNode.
127      */

128     final ASTNode internalGetSetChildProperty(ChildPropertyDescriptor property, boolean get, ASTNode child) {
129         if (property == QUALIFIER_PROPERTY) {
130             if (get) {
131                 return getQualifier();
132             } else {
133                 setQualifier((Type) child);
134                 return null;
135             }
136         }
137         if (property == NAME_PROPERTY) {
138             if (get) {
139                 return getName();
140             } else {
141                 setName((SimpleName) child);
142                 return null;
143             }
144         }
145         // allow default implementation to flag the error
146
return super.internalGetSetChildProperty(property, get, child);
147     }
148     
149     /* (omit javadoc for this method)
150      * Method declared on ASTNode.
151      */

152     final int getNodeType0() {
153         return QUALIFIED_TYPE;
154     }
155
156     /* (omit javadoc for this method)
157      * Method declared on ASTNode.
158      */

159     ASTNode clone0(AST target) {
160         QualifiedType result = new QualifiedType(target);
161         result.setSourceRange(this.getStartPosition(), this.getLength());
162         result.setQualifier((Type) ((ASTNode) getQualifier()).clone(target));
163         result.setName((SimpleName) ((ASTNode) getName()).clone(target));
164         return result;
165     }
166
167     /* (omit javadoc for this method)
168      * Method declared on ASTNode.
169      */

170     final boolean subtreeMatch0(ASTMatcher matcher, Object JavaDoc other) {
171         // dispatch to correct overloaded match method
172
return matcher.match(this, other);
173     }
174
175     /* (omit javadoc for this method)
176      * Method declared on ASTNode.
177      */

178     void accept0(ASTVisitor visitor) {
179         boolean visitChildren = visitor.visit(this);
180         if (visitChildren) {
181             // visit children in normal left to right reading order
182
acceptChild(visitor, getQualifier());
183             acceptChild(visitor, getName());
184         }
185         visitor.endVisit(this);
186     }
187     
188     /**
189      * Returns the qualifier of this qualified type.
190      *
191      * @return the qualifier of this qualified type
192      */

193     public Type getQualifier() {
194         if (this.qualifier == null) {
195             // lazy init must be thread-safe for readers
196
synchronized (this) {
197                 if (this.qualifier == null) {
198                     preLazyInit();
199                     this.qualifier = new SimpleType(this.ast);
200                     postLazyInit(this.qualifier, QUALIFIER_PROPERTY);
201                 }
202             }
203         }
204         return this.qualifier;
205     }
206     
207     /**
208      * Sets the qualifier of this qualified type to the given type.
209      *
210      * @param type the new qualifier of this qualified type
211      * @exception IllegalArgumentException if:
212      * <ul>
213      * <li>the node belongs to a different AST</li>
214      * <li>the node already has a parent</li>
215      * </ul>
216      */

217     public void setQualifier(Type type) {
218         if (type == null) {
219             throw new IllegalArgumentException JavaDoc();
220         }
221         ASTNode oldChild = this.qualifier;
222         preReplaceChild(oldChild, type, QUALIFIER_PROPERTY);
223         this.qualifier = type;
224         postReplaceChild(oldChild, type, QUALIFIER_PROPERTY);
225     }
226
227     /**
228      * Returns the name part of this qualified type.
229      *
230      * @return the name being qualified
231      */

232     public SimpleName getName() {
233         if (this.name == null) {
234             // lazy init must be thread-safe for readers
235
synchronized (this) {
236                 if (this.name == null) {
237                     preLazyInit();
238                     this.name = new SimpleName(this.ast);
239                     postLazyInit(this.name, NAME_PROPERTY);
240                 }
241             }
242         }
243         return this.name;
244     }
245     
246     /**
247      * Sets the name part of this qualified type to the given simple name.
248      *
249      * @param name the identifier of this qualified name
250      * @exception IllegalArgumentException if:
251      * <ul>
252      * <li>the node belongs to a different AST</li>
253      * <li>the node already has a parent</li>
254      * </ul>
255      */

256     public void setName(SimpleName name) {
257         if (name == null) {
258             throw new IllegalArgumentException JavaDoc();
259         }
260         ASTNode oldChild = this.name;
261         preReplaceChild(oldChild, name, NAME_PROPERTY);
262         this.name = name;
263         postReplaceChild(oldChild, name, NAME_PROPERTY);
264     }
265     
266     /* (omit javadoc for this method)
267      * Method declared on ASTNode.
268      */

269     int memSize() {
270         // treat Code as free
271
return BASE_NODE_SIZE + 3 * 4;
272     }
273     
274     /* (omit javadoc for this method)
275      * Method declared on ASTNode.
276      */

277     int treeSize() {
278         return
279             memSize()
280             + (this.qualifier == null ? 0 : getQualifier().treeSize())
281             + (this.name == null ? 0 : getName().treeSize());
282     }
283 }
284
285
Popular Tags