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 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.)
23 * Type <b><</b> Type { <b>,</b> Type } <b>></b>
25 * The first type may be a simple type or a qualified type;
26 * other kinds of types are meaningless.
29 * @noinstantiate This class is not intended to be instantiated by clients.
31 public class ParameterizedType extends Type {
33 * This index represents the position inside a parameterized qualified type.
38 * The "type" structural property of this node type.
40 public static final ChildPropertyDescriptor TYPE_PROPERTY =
41 new ChildPropertyDescriptor(ParameterizedType.class, "type", Type.class, MANDATORY, CYCLE_RISK); //$NON-NLS-1$
44 * The "typeArguments" structural property of this node type.
46 public static final ChildListPropertyDescriptor TYPE_ARGUMENTS_PROPERTY =
47 new ChildListPropertyDescriptor(ParameterizedType.class, "typeArguments", Type.class, CYCLE_RISK); //$NON-NLS-1$
50 * A list of property descriptors (element type:
51 * {@link StructuralPropertyDescriptor}),
52 * or null if uninitialized.
54 private static final List PROPERTY_DESCRIPTORS;
57 List propertyList = new ArrayList(3);
58 createPropertyList(ParameterizedType.class, propertyList);
59 addProperty(TYPE_PROPERTY, propertyList);
60 addProperty(TYPE_ARGUMENTS_PROPERTY, propertyList);
61 PROPERTY_DESCRIPTORS = reapPropertyList(propertyList);
65 * Returns a list of structural property descriptors for this node type.
66 * Clients must not modify the result.
68 * @param apiLevel the API level; one of the
69 * <code>AST.JLS*</code> constants
71 * @return a list of property descriptors (element type:
72 * {@link StructuralPropertyDescriptor})
74 public static List propertyDescriptors(int apiLevel) {
75 return PROPERTY_DESCRIPTORS;
79 * The type node; lazily initialized; defaults to an unspecfied, but legal,
82 private Type type = null;
85 * The type arguments (element type: <code>Type</code>).
86 * Defaults to an empty list.
88 private ASTNode.NodeList typeArguments =
89 new ASTNode.NodeList(TYPE_ARGUMENTS_PROPERTY);
92 * Creates a new unparented node for a parameterized type owned by the
93 * given AST. By default, an unspecified, but legal, type, and no type
96 * N.B. This constructor is package-private.
99 * @param ast the AST that is to own this node
101 ParameterizedType(AST ast) {
106 /* (omit javadoc for this method)
107 * Method declared on ASTNode.
109 final List internalStructuralPropertiesForType(int apiLevel) {
110 return propertyDescriptors(apiLevel);
113 /* (omit javadoc for this method)
114 * Method declared on ASTNode.
116 final ASTNode internalGetSetChildProperty(ChildPropertyDescriptor property, boolean get, ASTNode child) {
117 if (property == TYPE_PROPERTY) {
121 setType((Type) child);
125 // allow default implementation to flag the error
126 return super.internalGetSetChildProperty(property, get, child);
129 /* (omit javadoc for this method)
130 * Method declared on ASTNode.
132 final List internalGetChildListProperty(ChildListPropertyDescriptor property) {
133 if (property == TYPE_ARGUMENTS_PROPERTY) {
134 return typeArguments();
136 // allow default implementation to flag the error
137 return super.internalGetChildListProperty(property);
140 /* (omit javadoc for this method)
141 * Method declared on ASTNode.
143 final int getNodeType0() {
144 return PARAMETERIZED_TYPE;
147 /* (omit javadoc for this method)
148 * Method declared on ASTNode.
150 ASTNode clone0(AST target) {
151 ParameterizedType result = new ParameterizedType(target);
152 result.setSourceRange(this.getStartPosition(), this.getLength());
153 result.setType((Type) ((ASTNode) getType()).clone(target));
154 result.typeArguments().addAll(
155 ASTNode.copySubtrees(target, typeArguments()));
159 /* (omit javadoc for this method)
160 * Method declared on ASTNode.
162 final boolean subtreeMatch0(ASTMatcher matcher, Object other) {
163 // dispatch to correct overloaded match method
164 return matcher.match(this, other);
167 /* (omit javadoc for this method)
168 * Method declared on ASTNode.
170 void accept0(ASTVisitor visitor) {
171 boolean visitChildren = visitor.visit(this);
173 // visit children in normal left to right reading order
174 acceptChild(visitor, getType());
175 acceptChildren(visitor, this.typeArguments);
177 visitor.endVisit(this);
181 * Returns the type of this parameterized type.
183 * @return the type of this parameterized type
185 public Type getType() {
186 if (this.type == null) {
187 // lazy init must be thread-safe for readers
188 synchronized (this) {
189 if (this.type == null) {
191 this.type = new SimpleType(this.ast);
192 postLazyInit(this.type, TYPE_PROPERTY);
200 * Sets the type of this parameterized type.
202 * @param type the new type of this parameterized type
203 * @exception IllegalArgumentException if:
205 * <li>the node belongs to a different AST</li>
206 * <li>the node already has a parent</li>
209 public void setType(Type type) {
211 throw new IllegalArgumentException();
213 ASTNode oldChild = this.type;
214 preReplaceChild(oldChild, type, TYPE_PROPERTY);
216 postReplaceChild(oldChild, type, TYPE_PROPERTY);
220 * Returns the live ordered list of type arguments of this parameterized
221 * type. For the parameterized type to be plausible, the list should contain
222 * at least one element and not contain primitive types.
224 * @return the live list of type arguments
225 * (element type: <code>Type</code>)
227 public List typeArguments() {
228 return this.typeArguments;
231 /* (omit javadoc for this method)
232 * Method declared on ASTNode.
235 // treat Code as free
236 return BASE_NODE_SIZE + 3 * 4;
239 /* (omit javadoc for this method)
240 * Method declared on ASTNode.
245 + (this.type == null ? 0 : getType().treeSize())
246 + this.typeArguments.listSize();