KickJava   Java API By Example, From Geeks To Geeks.

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


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 parameterized type (added in JLS3 API).
19  * These nodes are used for type references (as opposed to
20  * declarations of parameterized types.)
21  * <pre>
22  * ParameterizedType:
23  * Type <b>&lt;</b> Type { <b>,</b> Type } <b>&gt;</b>
24  * </pre>
25  * The first type may be a simple type or a qualified type;
26  * other kinds of types are meaningless.
27  *
28  * @since 3.1
29  */

30 public class ParameterizedType extends Type {
31     /**
32      * This index represents the position inside a parameterized qualified type.
33      */

34     int index;
35     
36     /**
37      * The "type" structural property of this node type.
38      */

39     public static final ChildPropertyDescriptor TYPE_PROPERTY =
40         new ChildPropertyDescriptor(ParameterizedType.class, "type", Type.class, MANDATORY, CYCLE_RISK); //$NON-NLS-1$
41

42     /**
43      * The "typeArguments" structural property of this node type.
44      */

45     public static final ChildListPropertyDescriptor TYPE_ARGUMENTS_PROPERTY =
46         new ChildListPropertyDescriptor(ParameterizedType.class, "typeArguments", Type.class, CYCLE_RISK); //$NON-NLS-1$
47

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

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

73     public static List JavaDoc propertyDescriptors(int apiLevel) {
74         return PROPERTY_DESCRIPTORS;
75     }
76             
77     /**
78      * The type node; lazily initialized; defaults to an unspecfied, but legal,
79      * type.
80      */

81     private Type type = null;
82     
83     /**
84      * The type arguments (element type: <code>Type</code>).
85      * Defaults to an empty list.
86      */

87     private ASTNode.NodeList typeArguments =
88         new ASTNode.NodeList(TYPE_ARGUMENTS_PROPERTY);
89     
90     /**
91      * Creates a new unparented node for a parameterized type owned by the
92      * given AST. By default, an unspecified, but legal, type, and no type
93      * arguments.
94      * <p>
95      * N.B. This constructor is package-private.
96      * </p>
97      *
98      * @param ast the AST that is to own this node
99      */

100     ParameterizedType(AST ast) {
101         super(ast);
102         unsupportedIn2();
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 == TYPE_PROPERTY) {
117             if (get) {
118                 return getType();
119             } else {
120                 setType((Type) child);
121                 return null;
122             }
123         }
124         // allow default implementation to flag the error
125
return super.internalGetSetChildProperty(property, get, child);
126     }
127     
128     /* (omit javadoc for this method)
129      * Method declared on ASTNode.
130      */

131     final List JavaDoc internalGetChildListProperty(ChildListPropertyDescriptor property) {
132         if (property == TYPE_ARGUMENTS_PROPERTY) {
133             return typeArguments();
134         }
135         // allow default implementation to flag the error
136
return super.internalGetChildListProperty(property);
137     }
138
139     /* (omit javadoc for this method)
140      * Method declared on ASTNode.
141      */

142     final int getNodeType0() {
143         return PARAMETERIZED_TYPE;
144     }
145
146     /* (omit javadoc for this method)
147      * Method declared on ASTNode.
148      */

149     ASTNode clone0(AST target) {
150         ParameterizedType result = new ParameterizedType(target);
151         result.setSourceRange(this.getStartPosition(), this.getLength());
152         result.setType((Type) ((ASTNode) getType()).clone(target));
153         result.typeArguments().addAll(
154             ASTNode.copySubtrees(target, typeArguments()));
155         return result;
156     }
157
158     /* (omit javadoc for this method)
159      * Method declared on ASTNode.
160      */

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

169     void accept0(ASTVisitor visitor) {
170         boolean visitChildren = visitor.visit(this);
171         if (visitChildren) {
172             // visit children in normal left to right reading order
173
acceptChild(visitor, getType());
174             acceptChildren(visitor, this.typeArguments);
175         }
176         visitor.endVisit(this);
177     }
178     
179     /**
180      * Returns the type of this parameterized type.
181      *
182      * @return the type of this parameterized type
183      */

184     public Type getType() {
185         if (this.type == null) {
186             // lazy init must be thread-safe for readers
187
synchronized (this) {
188                 if (this.type == null) {
189                     preLazyInit();
190                     this.type = new SimpleType(this.ast);
191                     postLazyInit(this.type, TYPE_PROPERTY);
192                 }
193             }
194         }
195         return this.type;
196     }
197     
198     /**
199      * Sets the type of this parameterized type.
200      *
201      * @param type the new type of this parameterized type
202      * @exception IllegalArgumentException if:
203      * <ul>
204      * <li>the node belongs to a different AST</li>
205      * <li>the node already has a parent</li>
206      * </ul>
207      */

208     public void setType(Type type) {
209         if (type == null) {
210             throw new IllegalArgumentException JavaDoc();
211         }
212         ASTNode oldChild = this.type;
213         preReplaceChild(oldChild, type, TYPE_PROPERTY);
214         this.type = type;
215         postReplaceChild(oldChild, type, TYPE_PROPERTY);
216     }
217
218     /**
219      * Returns the live ordered list of type arguments of this parameterized
220      * type. For the parameterized type to be plausible, the list should contain
221      * at least one element and not contain primitive types.
222      *
223      * @return the live list of type arguments
224      * (element type: <code>Type</code>)
225      */

226     public List JavaDoc typeArguments() {
227         return this.typeArguments;
228     }
229     
230     /* (omit javadoc for this method)
231      * Method declared on ASTNode.
232      */

233     int memSize() {
234         // treat Code as free
235
return BASE_NODE_SIZE + 3 * 4;
236     }
237     
238     /* (omit javadoc for this method)
239      * Method declared on ASTNode.
240      */

241     int treeSize() {
242         return
243             memSize()
244             + (this.type == null ? 0 : getType().treeSize())
245             + this.typeArguments.listSize();
246     }
247 }
248
249
Popular Tags