KickJava   Java API By Example, From Geeks To Geeks.

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


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 parameter node (added in JLS3 API).
19  * <pre>
20  * TypeParameter:
21  * TypeVariable [ <b>extends</b> Type { <b>&</b> Type } ]
22  * </pre>
23  *
24  * @since 3.1
25  */

26 public class TypeParameter extends ASTNode {
27     
28     /**
29      * The "name" structural property of this node type.
30      */

31     public static final ChildPropertyDescriptor NAME_PROPERTY =
32         new ChildPropertyDescriptor(TypeParameter.class, "name", SimpleName.class, MANDATORY, NO_CYCLE_RISK); //$NON-NLS-1$
33

34     /**
35      * The "typeBounds" structural property of this node type.
36      */

37     public static final ChildListPropertyDescriptor TYPE_BOUNDS_PROPERTY =
38         new ChildListPropertyDescriptor(TypeParameter.class, "typeBounds", Type.class, NO_CYCLE_RISK); //$NON-NLS-1$
39

40     /**
41      * A list of property descriptors (element type:
42      * {@link StructuralPropertyDescriptor}),
43      * or null if uninitialized.
44      */

45     private static final List JavaDoc PROPERTY_DESCRIPTORS;
46     
47     static {
48         List JavaDoc propertyList = new ArrayList JavaDoc(3);
49         createPropertyList(TypeParameter.class, propertyList);
50         addProperty(NAME_PROPERTY, propertyList);
51         addProperty(TYPE_BOUNDS_PROPERTY, propertyList);
52         PROPERTY_DESCRIPTORS = reapPropertyList(propertyList);
53     }
54
55     /**
56      * Returns a list of structural property descriptors for this node type.
57      * Clients must not modify the result.
58      *
59      * @param apiLevel the API level; one of the
60      * <code>AST.JLS*</code> constants
61
62      * @return a list of property descriptors (element type:
63      * {@link StructuralPropertyDescriptor})
64      */

65     public static List JavaDoc propertyDescriptors(int apiLevel) {
66         return PROPERTY_DESCRIPTORS;
67     }
68             
69     /**
70      * The type variable node; lazily initialized; defaults to an unspecfied,
71      * but legal, name.
72      */

73     private SimpleName typeVariableName = null;
74     
75     /**
76      * The type bounds (element type: <code>Type</code>).
77      * Defaults to an empty list.
78      */

79     private ASTNode.NodeList typeBounds =
80         new ASTNode.NodeList(TYPE_BOUNDS_PROPERTY);
81     
82     /**
83      * Creates a new unparented node for a parameterized type owned by the
84      * given AST. By default, an unspecified, but legal, type variable name,
85      * and no type bounds.
86      * <p>
87      * N.B. This constructor is package-private.
88      * </p>
89      *
90      * @param ast the AST that is to own this node
91      */

92     TypeParameter(AST ast) {
93         super(ast);
94         unsupportedIn2();
95     }
96
97     /* (omit javadoc for this method)
98      * Method declared on ASTNode.
99      */

100     final List JavaDoc internalStructuralPropertiesForType(int apiLevel) {
101         return propertyDescriptors(apiLevel);
102     }
103     
104     /* (omit javadoc for this method)
105      * Method declared on ASTNode.
106      */

107     final ASTNode internalGetSetChildProperty(ChildPropertyDescriptor property, boolean get, ASTNode child) {
108         if (property == NAME_PROPERTY) {
109             if (get) {
110                 return getName();
111             } else {
112                 setName((SimpleName) child);
113                 return null;
114             }
115         }
116         // allow default implementation to flag the error
117
return super.internalGetSetChildProperty(property, get, child);
118     }
119     
120     /* (omit javadoc for this method)
121      * Method declared on ASTNode.
122      */

123     final List JavaDoc internalGetChildListProperty(ChildListPropertyDescriptor property) {
124         if (property == TYPE_BOUNDS_PROPERTY) {
125             return typeBounds();
126         }
127         // allow default implementation to flag the error
128
return super.internalGetChildListProperty(property);
129     }
130     
131     /* (omit javadoc for this method)
132      * Method declared on ASTNode.
133      */

134     final int getNodeType0() {
135         return TYPE_PARAMETER;
136     }
137
138     /* (omit javadoc for this method)
139      * Method declared on ASTNode.
140      */

141     ASTNode clone0(AST target) {
142         TypeParameter result = new TypeParameter(target);
143         result.setSourceRange(this.getStartPosition(), this.getLength());
144         result.setName((SimpleName) ((ASTNode) getName()).clone(target));
145         result.typeBounds().addAll(
146             ASTNode.copySubtrees(target, typeBounds()));
147         return result;
148     }
149
150     /* (omit javadoc for this method)
151      * Method declared on ASTNode.
152      */

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

161     void accept0(ASTVisitor visitor) {
162         boolean visitChildren = visitor.visit(this);
163         if (visitChildren) {
164             // visit children in normal left to right reading order
165
acceptChild(visitor, getName());
166             acceptChildren(visitor, this.typeBounds);
167         }
168         visitor.endVisit(this);
169     }
170     
171     /**
172      * Returns the name of the type variable declared in this type parameter.
173      *
174      * @return the name of the type variable
175      */

176     public SimpleName getName() {
177         if (this.typeVariableName == null) {
178             // lazy init must be thread-safe for readers
179
synchronized (this) {
180                 if (this.typeVariableName == null) {
181                     preLazyInit();
182                     this.typeVariableName = new SimpleName(this.ast);
183                     postLazyInit(this.typeVariableName, NAME_PROPERTY);
184                 }
185             }
186         }
187         return this.typeVariableName;
188     }
189     
190     /**
191      * Resolves and returns the binding for this type parameter.
192      * <p>
193      * Note that bindings are generally unavailable unless requested when the
194      * AST is being built.
195      * </p>
196      *
197      * @return the binding, or <code>null</code> if the binding cannot be
198      * resolved
199      */

200     public final ITypeBinding resolveBinding() {
201         return this.ast.getBindingResolver().resolveTypeParameter(this);
202     }
203     
204     /**
205      * Sets the name of the type variable of this type parameter to the given
206      * name.
207      *
208      * @param typeName the new name of this type parameter
209      * @exception IllegalArgumentException if:
210      * <ul>
211      * <li>the node belongs to a different AST</li>
212      * <li>the node already has a parent</li>
213      * </ul>
214      */

215     public void setName(SimpleName typeName) {
216         if (typeName == null) {
217             throw new IllegalArgumentException JavaDoc();
218         }
219         ASTNode oldChild = this.typeVariableName;
220         preReplaceChild(oldChild, typeName, NAME_PROPERTY);
221         this.typeVariableName = typeName;
222         postReplaceChild(oldChild, typeName, NAME_PROPERTY);
223     }
224
225     /**
226      * Returns the live ordered list of type bounds of this type parameter.
227      * For the type parameter to be plausible, there can be at most one
228      * class in the list, and it must be first, and the remaining ones must be
229      * interfaces; the list should not contain primitive types (but array types
230      * and parameterized types are allowed).
231      *
232      * @return the live list of type bounds
233      * (element type: <code>Type</code>)
234      */

235     public List JavaDoc typeBounds() {
236         return this.typeBounds;
237     }
238     
239     /* (omit javadoc for this method)
240      * Method declared on ASTNode.
241      */

242     int memSize() {
243         // treat Code as free
244
return BASE_NODE_SIZE + 2 * 4;
245     }
246     
247     /* (omit javadoc for this method)
248      * Method declared on ASTNode.
249      */

250     int treeSize() {
251         return
252             memSize()
253             + (this.typeVariableName == null ? 0 : getName().treeSize())
254             + this.typeBounds.listSize();
255     }
256 }
257
258
Popular Tags