KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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 an array type.
19  * <p>
20  * Array types are expressed in a recursive manner, one dimension at a time.
21  * </p>
22  * <pre>
23  * ArrayType:
24  * Type <b>[</b> <b>]</b>
25  * </pre>
26  *
27  * @since 2.0
28  */

29 public class ArrayType extends Type {
30     
31     /**
32      * The "componentType" structural property of this node type.
33      * @since 3.0
34      */

35     public static final ChildPropertyDescriptor COMPONENT_TYPE_PROPERTY =
36         new ChildPropertyDescriptor(ArrayType.class, "componentType", Type.class, MANDATORY, CYCLE_RISK); //$NON-NLS-1$
37

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

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

63     public static List JavaDoc propertyDescriptors(int apiLevel) {
64         return PROPERTY_DESCRIPTORS;
65     }
66             
67     /**
68      * The component type; lazily initialized; defaults to a simple type with
69      * an unspecfied, but legal, name.
70      */

71     private Type componentType = null;
72     
73     /**
74      * Creates a new unparented node for an array type owned by the given AST.
75      * By default, a 1-dimensional array of an unspecified simple type.
76      * <p>
77      * N.B. This constructor is package-private.
78      * </p>
79      *
80      * @param ast the AST that is to own this node
81      */

82     ArrayType(AST ast) {
83         super(ast);
84     }
85
86     /* (omit javadoc for this method)
87      * Method declared on ASTNode.
88      */

89     final List JavaDoc internalStructuralPropertiesForType(int apiLevel) {
90         return propertyDescriptors(apiLevel);
91     }
92     
93     /* (omit javadoc for this method)
94      * Method declared on ASTNode.
95      */

96     final ASTNode internalGetSetChildProperty(ChildPropertyDescriptor property, boolean get, ASTNode child) {
97         if (property == COMPONENT_TYPE_PROPERTY) {
98             if (get) {
99                 return getComponentType();
100             } else {
101                 setComponentType((Type) child);
102                 return null;
103             }
104         }
105         // allow default implementation to flag the error
106
return super.internalGetSetChildProperty(property, get, child);
107     }
108     
109     /* (omit javadoc for this method)
110      * Method declared on ASTNode.
111      */

112     final int getNodeType0() {
113         return ARRAY_TYPE;
114     }
115
116     /* (omit javadoc for this method)
117      * Method declared on ASTNode.
118      */

119     ASTNode clone0(AST target) {
120         ArrayType result = new ArrayType(target);
121         result.setSourceRange(this.getStartPosition(), this.getLength());
122         result.setComponentType((Type) getComponentType().clone(target));
123         return result;
124     }
125
126     /* (omit javadoc for this method)
127      * Method declared on ASTNode.
128      */

129     final boolean subtreeMatch0(ASTMatcher matcher, Object JavaDoc other) {
130         // dispatch to correct overloaded match method
131
return matcher.match(this, other);
132     }
133
134     /* (omit javadoc for this method)
135      * Method declared on ASTNode.
136      */

137     void accept0(ASTVisitor visitor) {
138         boolean visitChildren = visitor.visit(this);
139         if (visitChildren) {
140             acceptChild(visitor, getComponentType());
141         }
142         visitor.endVisit(this);
143     }
144     
145     /**
146      * Returns the component type of this array type. The component type
147      * may be another array type.
148      *
149      * @return the component type node
150      */

151     public Type getComponentType() {
152         if (this.componentType == null) {
153             // lazy init must be thread-safe for readers
154
synchronized (this) {
155                 if (this.componentType == null) {
156                     preLazyInit();
157                     this.componentType = new SimpleType(this.ast);
158                     postLazyInit(this.componentType, COMPONENT_TYPE_PROPERTY);
159                 }
160             }
161         }
162         return this.componentType;
163     }
164
165     /**
166      * Sets the component type of this array type. The component type
167      * may be another array type.
168      *
169      * @param componentType the component type
170      * @exception IllegalArgumentException if:
171      * <ul>
172      * <li>the node belongs to a different AST</li>
173      * <li>the node already has a parent</li>
174      * <li>a cycle in would be created</li>
175      * </ul>
176      */

177     public void setComponentType(Type componentType) {
178         if (componentType == null) {
179             throw new IllegalArgumentException JavaDoc();
180         }
181         ASTNode oldChild = this.componentType;
182         preReplaceChild(oldChild, componentType, COMPONENT_TYPE_PROPERTY);
183         this.componentType = componentType;
184         postReplaceChild(oldChild, componentType, COMPONENT_TYPE_PROPERTY);
185     }
186
187     /**
188      * Returns the element type of this array type. The element type is
189      * never an array type.
190      * <p>
191      * This is a convenience method that descends a chain of nested array types
192      * until it reaches a non-array type.
193      * </p>
194      *
195      * @return the component type node
196      */

197     public Type getElementType() {
198         Type t = getComponentType();
199         while (t.isArrayType()) {
200             t = ((ArrayType) t).getComponentType();
201         }
202         return t;
203     }
204     
205     /**
206      * Returns the number of dimensions in this array type.
207      * <p>
208      * This is a convenience method that descends a chain of nested array types
209      * until it reaches a non-array type.
210      * </p>
211      *
212      * @return the number of dimensions (always positive)
213      */

214     public int getDimensions() {
215         Type t = getComponentType();
216         int dimensions = 1; // always include this array type
217
while (t.isArrayType()) {
218             dimensions++;
219             t = ((ArrayType) t).getComponentType();
220         }
221         return dimensions;
222     }
223     
224     /* (omit javadoc for this method)
225      * Method declared on ASTNode.
226      */

227     int memSize() {
228         return BASE_NODE_SIZE + 1 * 4;
229     }
230     
231     /* (omit javadoc for this method)
232      * Method declared on ASTNode.
233      */

234     int treeSize() {
235         return
236             memSize()
237             + (this.componentType == null ? 0 : getComponentType().treeSize());
238     }
239 }
240
241
Popular Tags