KickJava   Java API By Example, From Geeks To Geeks.

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


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 package org.eclipse.jdt.core.dom;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.List JavaDoc;
15
16 /**
17  * Array creation expression AST node type.
18  * For JLS2:
19  * <pre>
20  * ArrayCreation:
21  * <b>new</b> PrimitiveType <b>[</b> Expression <b>]</b> { <b>[</b> Expression <b>]</b> } { <b>[</b> <b>]</b> }
22  * <b>new</b> TypeName <b>[</b> Expression <b>]</b> { <b>[</b> Expression <b>]</b> } { <b>[</b> <b>]</b> }
23  * <b>new</b> PrimitiveType <b>[</b> <b>]</b> { <b>[</b> <b>]</b> } ArrayInitializer
24  * <b>new</b> TypeName <b>[</b> <b>]</b> { <b>[</b> <b>]</b> } ArrayInitializer
25  * </pre>
26  * <p>
27  * The mapping from Java language syntax to AST nodes is as follows:
28  * <ul>
29  * <li>the type node is the array type of the creation expression,
30  * with one level of array per set of square brackets,</li>
31  * <li>the dimension expressions are collected into the <code>dimensions</code>
32  * list.</li>
33  * </ul>
34  * </p>
35  * For JLS3, type arguments are added:
36  * <pre>
37  * ArrayCreation:
38  * <b>new</b> PrimitiveType <b>[</b> Expression <b>]</b> { <b>[</b> Expression <b>]</b> } { <b>[</b> <b>]</b> }
39  * <b>new</b> TypeName [ <b>&lt;</b> Type { <b>,</b> Type } <b>&gt;</b> ]
40  * <b>[</b> Expression <b>]</b> { <b>[</b> Expression <b>]</b> } { <b>[</b> <b>]</b> }
41  * <b>new</b> PrimitiveType <b>[</b> <b>]</b> { <b>[</b> <b>]</b> } ArrayInitializer
42  * <b>new</b> TypeName [ <b>&lt;</b> Type { <b>,</b> Type } <b>&gt;</b> ]
43  * <b>[</b> <b>]</b> { <b>[</b> <b>]</b> } ArrayInitializer
44  * </pre>
45  *
46  * @since 2.0
47  */

48 public class ArrayCreation extends Expression {
49     
50     /**
51      * The "type" structural property of this node type.
52      * @since 3.0
53      */

54     public static final ChildPropertyDescriptor TYPE_PROPERTY =
55         new ChildPropertyDescriptor(ArrayCreation.class, "type", ArrayType.class, MANDATORY, NO_CYCLE_RISK); //$NON-NLS-1$
56

57     /**
58      * The "dimensions" structural property of this node type.
59      * @since 3.0
60      */

61     public static final ChildListPropertyDescriptor DIMENSIONS_PROPERTY =
62         new ChildListPropertyDescriptor(ArrayCreation.class, "dimensions", Expression.class, CYCLE_RISK); //$NON-NLS-1$
63

64     /**
65      * The "initializer" structural property of this node type.
66      * @since 3.0
67      */

68     public static final ChildPropertyDescriptor INITIALIZER_PROPERTY =
69         new ChildPropertyDescriptor(ArrayCreation.class, "initializer", ArrayInitializer.class, OPTIONAL, CYCLE_RISK); //$NON-NLS-1$
70

71     /**
72      * A list of property descriptors (element type:
73      * {@link StructuralPropertyDescriptor}),
74      * or null if uninitialized.
75      */

76     private static final List JavaDoc PROPERTY_DESCRIPTORS;
77     
78     static {
79         List JavaDoc properyList = new ArrayList JavaDoc(4);
80         createPropertyList(ArrayCreation.class, properyList);
81         addProperty(TYPE_PROPERTY, properyList);
82         addProperty(DIMENSIONS_PROPERTY, properyList);
83         addProperty(INITIALIZER_PROPERTY, properyList);
84         PROPERTY_DESCRIPTORS = reapPropertyList(properyList);
85     }
86
87     /**
88      * Returns a list of structural property descriptors for this node type.
89      * Clients must not modify the result.
90      *
91      * @param apiLevel the API level; one of the
92      * <code>AST.JLS*</code> constants
93
94      * @return a list of property descriptors (element type:
95      * {@link StructuralPropertyDescriptor})
96      * @since 3.0
97      */

98     public static List JavaDoc propertyDescriptors(int apiLevel) {
99         return PROPERTY_DESCRIPTORS;
100     }
101             
102     /**
103      * The array type; lazily initialized; defaults to a unspecified,
104      * legal array type.
105      */

106     private ArrayType arrayType = null;
107
108     /**
109      * The list of dimension expressions (element type:
110      * <code>Expression</code>). Defaults to an empty list.
111      */

112     private ASTNode.NodeList dimensions =
113         new ASTNode.NodeList(DIMENSIONS_PROPERTY);
114
115     /**
116      * The optional array initializer, or <code>null</code> if none;
117      * defaults to none.
118      */

119     private ArrayInitializer optionalInitializer = null;
120
121     /**
122      * Creates a new AST node for an array creation expression owned by the
123      * given AST. By default, the array type is an unspecified 1-dimensional
124      * array, the list of dimensions is empty, and there is no array
125      * initializer.
126      *
127      * @param ast the AST that is to own this node
128      */

129     ArrayCreation(AST ast) {
130         super(ast);
131     }
132
133     /* (omit javadoc for this method)
134      * Method declared on ASTNode.
135      */

136     final List JavaDoc internalStructuralPropertiesForType(int apiLevel) {
137         return propertyDescriptors(apiLevel);
138     }
139         
140     /* (omit javadoc for this method)
141      * Method declared on ASTNode.
142      */

143     final ASTNode internalGetSetChildProperty(ChildPropertyDescriptor property, boolean get, ASTNode child) {
144         if (property == INITIALIZER_PROPERTY) {
145             if (get) {
146                 return getInitializer();
147             } else {
148                 setInitializer((ArrayInitializer) child);
149                 return null;
150             }
151         }
152         if (property == TYPE_PROPERTY) {
153             if (get) {
154                 return getType();
155             } else {
156                 setType((ArrayType) child);
157                 return null;
158             }
159         }
160         // allow default implementation to flag the error
161
return super.internalGetSetChildProperty(property, get, child);
162     }
163     
164     /* (omit javadoc for this method)
165      * Method declared on ASTNode.
166      */

167     final List JavaDoc internalGetChildListProperty(ChildListPropertyDescriptor property) {
168         if (property == DIMENSIONS_PROPERTY) {
169             return dimensions();
170         }
171         // allow default implementation to flag the error
172
return super.internalGetChildListProperty(property);
173     }
174     
175     /* (omit javadoc for this method)
176      * Method declared on ASTNode.
177      */

178     final int getNodeType0() {
179         return ARRAY_CREATION;
180     }
181
182     /* (omit javadoc for this method)
183      * Method declared on ASTNode.
184      */

185     ASTNode clone0(AST target) {
186         ArrayCreation result = new ArrayCreation(target);
187         result.setSourceRange(this.getStartPosition(), this.getLength());
188         result.setType((ArrayType) getType().clone(target));
189         result.dimensions().addAll(ASTNode.copySubtrees(target, dimensions()));
190         result.setInitializer(
191             (ArrayInitializer) ASTNode.copySubtree(target, getInitializer()));
192         return result;
193     }
194
195     /* (omit javadoc for this method)
196      * Method declared on ASTNode.
197      */

198     final boolean subtreeMatch0(ASTMatcher matcher, Object JavaDoc other) {
199         // dispatch to correct overloaded match method
200
return matcher.match(this, other);
201     }
202
203     /* (omit javadoc for this method)
204      * Method declared on ASTNode.
205      */

206     void accept0(ASTVisitor visitor) {
207         boolean visitChildren = visitor.visit(this);
208         if (visitChildren) {
209             // visit children in normal left to right reading order
210
acceptChild(visitor, getType());
211             acceptChildren(visitor, this.dimensions);
212             acceptChild(visitor, getInitializer());
213         }
214         visitor.endVisit(this);
215     }
216     
217     /**
218      * Returns the array type in this array creation expression.
219      *
220      * @return the array type
221      */

222     public ArrayType getType() {
223         if (this.arrayType == null) {
224             // lazy init must be thread-safe for readers
225
synchronized (this) {
226                 if (this.arrayType == null) {
227                     preLazyInit();
228                     this.arrayType = this.ast.newArrayType(
229                             this.ast.newPrimitiveType(PrimitiveType.INT));
230                     postLazyInit(this.arrayType, TYPE_PROPERTY);
231                 }
232             }
233         }
234         return this.arrayType;
235     }
236
237     /**
238      * Sets the array type in this array creation expression.
239      *
240      * @param type the new array type
241      * @exception IllegalArgumentException if:
242      * <ul>
243      * <li>the node belongs to a different AST</li>
244      * <li>the node already has a parent</li>
245      * </ul>
246      */

247     public void setType(ArrayType type) {
248         if (type == null) {
249             throw new IllegalArgumentException JavaDoc();
250         }
251         // an ArrayCreation cannot occur inside a ArrayType - cycles not possible
252
ASTNode oldChild = this.arrayType;
253         preReplaceChild(oldChild, type, TYPE_PROPERTY);
254         this.arrayType = type;
255         postReplaceChild(oldChild, type, TYPE_PROPERTY);
256     }
257     
258     /**
259      * Returns the live ordered list of dimension expressions in this array
260      * initializer.
261      *
262      * @return the live list of dimension expressions
263      * (element type: <code>Expression</code>)
264      */

265     public List JavaDoc dimensions() {
266         return this.dimensions;
267     }
268     
269     /**
270      * Returns the array initializer of this array creation expression, or
271      * <code>null</code> if there is none.
272      *
273      * @return the array initializer node, or <code>null</code> if
274      * there is none
275      */

276     public ArrayInitializer getInitializer() {
277         return optionalInitializer;
278     }
279     
280     /**
281      * Sets or clears the array initializer of this array creation expression.
282      *
283      * @param initializer the array initializer node, or <code>null</code>
284      * if there is none
285      * @exception IllegalArgumentException if:
286      * <ul>
287      * <li>the node belongs to a different AST</li>
288      * <li>the node already has a parent</li>
289      * <li>a cycle in would be created</li>
290      * </ul>
291      */

292     public void setInitializer(ArrayInitializer initializer) {
293         // an ArrayCreation may occur inside an ArrayInitializer
294
// must check cycles
295
ASTNode oldChild = this.optionalInitializer;
296         preReplaceChild(oldChild, initializer, INITIALIZER_PROPERTY);
297         this.optionalInitializer = initializer;
298         postReplaceChild(oldChild, initializer, INITIALIZER_PROPERTY);
299     }
300
301     /* (omit javadoc for this method)
302      * Method declared on ASTNode.
303      */

304     int memSize() {
305         return BASE_NODE_SIZE + 3 * 4;
306     }
307     
308     /* (omit javadoc for this method)
309      * Method declared on ASTNode.
310      */

311     int treeSize() {
312         int size = memSize()
313             + (this.arrayType == null ? 0 : getType().treeSize())
314             + (this.optionalInitializer == null ? 0 : getInitializer().treeSize())
315             + this.dimensions.listSize();
316         return size;
317     }
318 }
319
320
Popular Tags