1 /*******************************************************************************
2 * Copyright (c) 2003, 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
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
12 package net.sourceforge.phpdt.core.dom;
14 import java.util.ArrayList;
15 import java.util.List;
18 * Type parameter node (added in JLS3 API).
21 * TypeVariable [ <b>extends</b> Type { <b>&</b> Type } ]
25 * @noinstantiate This class is not intended to be instantiated by clients.
27 public class TypeParameter extends ASTNode {
30 * The "name" structural property of this node type.
32 public static final ChildPropertyDescriptor NAME_PROPERTY =
33 new ChildPropertyDescriptor(TypeParameter.class, "name", SimpleName.class, MANDATORY, NO_CYCLE_RISK); //$NON-NLS-1$
36 * The "typeBounds" structural property of this node type.
38 public static final ChildListPropertyDescriptor TYPE_BOUNDS_PROPERTY =
39 new ChildListPropertyDescriptor(TypeParameter.class, "typeBounds", Type.class, NO_CYCLE_RISK); //$NON-NLS-1$
42 * A list of property descriptors (element type:
43 * {@link StructuralPropertyDescriptor}),
44 * or null if uninitialized.
46 private static final List PROPERTY_DESCRIPTORS;
49 List propertyList = new ArrayList(3);
50 createPropertyList(TypeParameter.class, propertyList);
51 addProperty(NAME_PROPERTY, propertyList);
52 addProperty(TYPE_BOUNDS_PROPERTY, propertyList);
53 PROPERTY_DESCRIPTORS = reapPropertyList(propertyList);
57 * Returns a list of structural property descriptors for this node type.
58 * Clients must not modify the result.
60 * @param apiLevel the API level; one of the
61 * <code>AST.JLS*</code> constants
63 * @return a list of property descriptors (element type:
64 * {@link StructuralPropertyDescriptor})
66 public static List propertyDescriptors(int apiLevel) {
67 return PROPERTY_DESCRIPTORS;
71 * The type variable node; lazily initialized; defaults to an unspecfied,
74 private SimpleName typeVariableName = null;
77 * The type bounds (element type: <code>Type</code>).
78 * Defaults to an empty list.
80 private ASTNode.NodeList typeBounds =
81 new ASTNode.NodeList(TYPE_BOUNDS_PROPERTY);
84 * Creates a new unparented node for a parameterized type owned by the
85 * given AST. By default, an unspecified, but legal, type variable name,
88 * N.B. This constructor is package-private.
91 * @param ast the AST that is to own this node
93 TypeParameter(AST ast) {
98 /* (omit javadoc for this method)
99 * Method declared on ASTNode.
101 final List internalStructuralPropertiesForType(int apiLevel) {
102 return propertyDescriptors(apiLevel);
105 /* (omit javadoc for this method)
106 * Method declared on ASTNode.
108 final ASTNode internalGetSetChildProperty(ChildPropertyDescriptor property, boolean get, ASTNode child) {
109 if (property == NAME_PROPERTY) {
113 setName((SimpleName) child);
117 // allow default implementation to flag the error
118 return super.internalGetSetChildProperty(property, get, child);
121 /* (omit javadoc for this method)
122 * Method declared on ASTNode.
124 final List internalGetChildListProperty(ChildListPropertyDescriptor property) {
125 if (property == TYPE_BOUNDS_PROPERTY) {
128 // allow default implementation to flag the error
129 return super.internalGetChildListProperty(property);
132 /* (omit javadoc for this method)
133 * Method declared on ASTNode.
135 final int getNodeType0() {
136 return TYPE_PARAMETER;
139 /* (omit javadoc for this method)
140 * Method declared on ASTNode.
142 ASTNode clone0(AST target) {
143 TypeParameter result = new TypeParameter(target);
144 result.setSourceRange(this.getStartPosition(), this.getLength());
145 result.setName((SimpleName) ((ASTNode) getName()).clone(target));
146 result.typeBounds().addAll(
147 ASTNode.copySubtrees(target, typeBounds()));
151 /* (omit javadoc for this method)
152 * Method declared on ASTNode.
154 final boolean subtreeMatch0(ASTMatcher matcher, Object other) {
155 // dispatch to correct overloaded match method
156 return matcher.match(this, other);
159 /* (omit javadoc for this method)
160 * Method declared on ASTNode.
162 void accept0(ASTVisitor visitor) {
163 boolean visitChildren = visitor.visit(this);
165 // visit children in normal left to right reading order
166 acceptChild(visitor, getName());
167 acceptChildren(visitor, this.typeBounds);
169 visitor.endVisit(this);
173 * Returns the name of the type variable declared in this type parameter.
175 * @return the name of the type variable
177 public SimpleName getName() {
178 if (this.typeVariableName == null) {
179 // lazy init must be thread-safe for readers
180 synchronized (this) {
181 if (this.typeVariableName == null) {
183 this.typeVariableName = new SimpleName(this.ast);
184 postLazyInit(this.typeVariableName, NAME_PROPERTY);
188 return this.typeVariableName;
192 * Resolves and returns the binding for this type parameter.
194 * Note that bindings are generally unavailable unless requested when the
195 * AST is being built.
198 * @return the binding, or <code>null</code> if the binding cannot be
201 public final ITypeBinding resolveBinding() {
202 return this.ast.getBindingResolver().resolveTypeParameter(this);
206 * Sets the name of the type variable of this type parameter to the given
209 * @param typeName the new name of this type parameter
210 * @exception IllegalArgumentException if:
212 * <li>the node belongs to a different AST</li>
213 * <li>the node already has a parent</li>
216 public void setName(SimpleName typeName) {
217 if (typeName == null) {
218 throw new IllegalArgumentException();
220 ASTNode oldChild = this.typeVariableName;
221 preReplaceChild(oldChild, typeName, NAME_PROPERTY);
222 this.typeVariableName = typeName;
223 postReplaceChild(oldChild, typeName, NAME_PROPERTY);
227 * Returns the live ordered list of type bounds of this type parameter.
228 * For the type parameter to be plausible, there can be at most one
229 * class in the list, and it must be first, and the remaining ones must be
230 * interfaces; the list should not contain primitive types (but array types
231 * and parameterized types are allowed).
233 * @return the live list of type bounds
234 * (element type: <code>Type</code>)
236 public List typeBounds() {
237 return this.typeBounds;
240 /* (omit javadoc for this method)
241 * Method declared on ASTNode.
244 // treat Code as free
245 return BASE_NODE_SIZE + 2 * 4;
248 /* (omit javadoc for this method)
249 * Method declared on ASTNode.
254 + (this.typeVariableName == null ? 0 : getName().treeSize())
255 + this.typeBounds.listSize();