1 /*******************************************************************************
2 * Copyright (c) 2000, 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 an array type.
20 * Array types are expressed in a recursive manner, one dimension at a time.
24 * Type <b>[</b> <b>]</b>
28 * @noinstantiate This class is not intended to be instantiated by clients.
30 public class ArrayType extends Type {
33 * The "componentType" structural property of this node type.
36 public static final ChildPropertyDescriptor COMPONENT_TYPE_PROPERTY =
37 new ChildPropertyDescriptor(ArrayType.class, "componentType", Type.class, MANDATORY, CYCLE_RISK); //$NON-NLS-1$
40 * A list of property descriptors (element type:
41 * {@link StructuralPropertyDescriptor}),
42 * or null if uninitialized.
44 private static final List PROPERTY_DESCRIPTORS;
47 List properyList = new ArrayList(2);
48 createPropertyList(ArrayType.class, properyList);
49 addProperty(COMPONENT_TYPE_PROPERTY, properyList);
50 PROPERTY_DESCRIPTORS = reapPropertyList(properyList);
54 * Returns a list of structural property descriptors for this node type.
55 * Clients must not modify the result.
57 * @param apiLevel the API level; one of the
58 * <code>AST.JLS*</code> constants
60 * @return a list of property descriptors (element type:
61 * {@link StructuralPropertyDescriptor})
64 public static List propertyDescriptors(int apiLevel) {
65 return PROPERTY_DESCRIPTORS;
69 * The component type; lazily initialized; defaults to a simple type with
70 * an unspecfied, but legal, name.
72 private Type componentType = null;
75 * Creates a new unparented node for an array type owned by the given AST.
76 * By default, a 1-dimensional array of an unspecified simple type.
78 * N.B. This constructor is package-private.
81 * @param ast the AST that is to own this node
87 /* (omit javadoc for this method)
88 * Method declared on ASTNode.
90 final List internalStructuralPropertiesForType(int apiLevel) {
91 return propertyDescriptors(apiLevel);
94 /* (omit javadoc for this method)
95 * Method declared on ASTNode.
97 final ASTNode internalGetSetChildProperty(ChildPropertyDescriptor property, boolean get, ASTNode child) {
98 if (property == COMPONENT_TYPE_PROPERTY) {
100 return getComponentType();
102 setComponentType((Type) child);
106 // allow default implementation to flag the error
107 return super.internalGetSetChildProperty(property, get, child);
110 /* (omit javadoc for this method)
111 * Method declared on ASTNode.
113 final int getNodeType0() {
117 /* (omit javadoc for this method)
118 * Method declared on ASTNode.
120 ASTNode clone0(AST target) {
121 ArrayType result = new ArrayType(target);
122 result.setSourceRange(this.getStartPosition(), this.getLength());
123 result.setComponentType((Type) getComponentType().clone(target));
127 /* (omit javadoc for this method)
128 * Method declared on ASTNode.
130 final boolean subtreeMatch0(ASTMatcher matcher, Object other) {
131 // dispatch to correct overloaded match method
132 return matcher.match(this, other);
135 /* (omit javadoc for this method)
136 * Method declared on ASTNode.
138 void accept0(ASTVisitor visitor) {
139 boolean visitChildren = visitor.visit(this);
141 acceptChild(visitor, getComponentType());
143 visitor.endVisit(this);
147 * Returns the component type of this array type. The component type
148 * may be another array type.
150 * @return the component type node
152 public Type getComponentType() {
153 if (this.componentType == null) {
154 // lazy init must be thread-safe for readers
155 synchronized (this) {
156 if (this.componentType == null) {
158 this.componentType = new SimpleType(this.ast);
159 postLazyInit(this.componentType, COMPONENT_TYPE_PROPERTY);
163 return this.componentType;
167 * Sets the component type of this array type. The component type
168 * may be another array type.
170 * @param componentType the component type
171 * @exception IllegalArgumentException if:
173 * <li>the node belongs to a different AST</li>
174 * <li>the node already has a parent</li>
175 * <li>a cycle in would be created</li>
178 public void setComponentType(Type componentType) {
179 if (componentType == null) {
180 throw new IllegalArgumentException();
182 ASTNode oldChild = this.componentType;
183 preReplaceChild(oldChild, componentType, COMPONENT_TYPE_PROPERTY);
184 this.componentType = componentType;
185 postReplaceChild(oldChild, componentType, COMPONENT_TYPE_PROPERTY);
189 * Returns the element type of this array type. The element type is
190 * never an array type.
192 * This is a convenience method that descends a chain of nested array types
193 * until it reaches a non-array type.
196 * @return the component type node
198 public Type getElementType() {
199 Type t = getComponentType();
200 while (t.isArrayType()) {
201 t = ((ArrayType) t).getComponentType();
207 * Returns the number of dimensions in this array type.
209 * This is a convenience method that descends a chain of nested array types
210 * until it reaches a non-array type.
213 * @return the number of dimensions (always positive)
215 public int getDimensions() {
216 Type t = getComponentType();
217 int dimensions = 1; // always include this array type
218 while (t.isArrayType()) {
220 t = ((ArrayType) t).getComponentType();
225 /* (omit javadoc for this method)
226 * Method declared on ASTNode.
229 return BASE_NODE_SIZE + 1 * 4;
232 /* (omit javadoc for this method)
233 * Method declared on ASTNode.
238 + (this.componentType == null ? 0 : getComponentType().treeSize());