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 *******************************************************************************/
11 package net.sourceforge.phpdt.core.dom;
13 import java.util.ArrayList;
14 import java.util.List;
17 * Array creation expression AST node type.
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
27 * The mapping from Java language syntax to AST nodes is as follows:
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>
35 * For JLS3, type arguments are added:
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><</b> Type { <b>,</b> Type } <b>></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><</b> Type { <b>,</b> Type } <b>></b> ]
43 * <b>[</b> <b>]</b> { <b>[</b> <b>]</b> } ArrayInitializer
47 * @noinstantiate This class is not intended to be instantiated by clients.
49 public class ArrayCreation extends Expression {
52 * The "type" structural property of this node type.
55 public static final ChildPropertyDescriptor TYPE_PROPERTY =
56 new ChildPropertyDescriptor(ArrayCreation.class, "type", ArrayType.class, MANDATORY, NO_CYCLE_RISK); //$NON-NLS-1$
59 * The "dimensions" structural property of this node type.
62 public static final ChildListPropertyDescriptor DIMENSIONS_PROPERTY =
63 new ChildListPropertyDescriptor(ArrayCreation.class, "dimensions", Expression.class, CYCLE_RISK); //$NON-NLS-1$
66 * The "initializer" structural property of this node type.
69 public static final ChildPropertyDescriptor INITIALIZER_PROPERTY =
70 new ChildPropertyDescriptor(ArrayCreation.class, "initializer", ArrayInitializer.class, OPTIONAL, CYCLE_RISK); //$NON-NLS-1$
73 * A list of property descriptors (element type:
74 * {@link StructuralPropertyDescriptor}),
75 * or null if uninitialized.
77 private static final List PROPERTY_DESCRIPTORS;
80 List properyList = new ArrayList(4);
81 createPropertyList(ArrayCreation.class, properyList);
82 addProperty(TYPE_PROPERTY, properyList);
83 addProperty(DIMENSIONS_PROPERTY, properyList);
84 addProperty(INITIALIZER_PROPERTY, properyList);
85 PROPERTY_DESCRIPTORS = reapPropertyList(properyList);
89 * Returns a list of structural property descriptors for this node type.
90 * Clients must not modify the result.
92 * @param apiLevel the API level; one of the
93 * <code>AST.JLS*</code> constants
95 * @return a list of property descriptors (element type:
96 * {@link StructuralPropertyDescriptor})
99 public static List propertyDescriptors(int apiLevel) {
100 return PROPERTY_DESCRIPTORS;
104 * The array type; lazily initialized; defaults to a unspecified,
107 private ArrayType arrayType = null;
110 * The list of dimension expressions (element type:
111 * <code>Expression</code>). Defaults to an empty list.
113 private ASTNode.NodeList dimensions =
114 new ASTNode.NodeList(DIMENSIONS_PROPERTY);
117 * The optional array initializer, or <code>null</code> if none;
120 private ArrayInitializer optionalInitializer = null;
123 * Creates a new AST node for an array creation expression owned by the
124 * given AST. By default, the array type is an unspecified 1-dimensional
125 * array, the list of dimensions is empty, and there is no array
128 * @param ast the AST that is to own this node
130 ArrayCreation(AST ast) {
134 /* (omit javadoc for this method)
135 * Method declared on ASTNode.
137 final List internalStructuralPropertiesForType(int apiLevel) {
138 return propertyDescriptors(apiLevel);
141 /* (omit javadoc for this method)
142 * Method declared on ASTNode.
144 final ASTNode internalGetSetChildProperty(ChildPropertyDescriptor property, boolean get, ASTNode child) {
145 if (property == INITIALIZER_PROPERTY) {
147 return getInitializer();
149 setInitializer((ArrayInitializer) child);
153 if (property == TYPE_PROPERTY) {
157 setType((ArrayType) child);
161 // allow default implementation to flag the error
162 return super.internalGetSetChildProperty(property, get, child);
165 /* (omit javadoc for this method)
166 * Method declared on ASTNode.
168 final List internalGetChildListProperty(ChildListPropertyDescriptor property) {
169 if (property == DIMENSIONS_PROPERTY) {
172 // allow default implementation to flag the error
173 return super.internalGetChildListProperty(property);
176 /* (omit javadoc for this method)
177 * Method declared on ASTNode.
179 final int getNodeType0() {
180 return ARRAY_CREATION;
183 /* (omit javadoc for this method)
184 * Method declared on ASTNode.
186 ASTNode clone0(AST target) {
187 ArrayCreation result = new ArrayCreation(target);
188 result.setSourceRange(this.getStartPosition(), this.getLength());
189 result.setType((ArrayType) getType().clone(target));
190 result.dimensions().addAll(ASTNode.copySubtrees(target, dimensions()));
191 result.setInitializer(
192 (ArrayInitializer) ASTNode.copySubtree(target, getInitializer()));
196 /* (omit javadoc for this method)
197 * Method declared on ASTNode.
199 final boolean subtreeMatch0(ASTMatcher matcher, Object other) {
200 // dispatch to correct overloaded match method
201 return matcher.match(this, other);
204 /* (omit javadoc for this method)
205 * Method declared on ASTNode.
207 void accept0(ASTVisitor visitor) {
208 boolean visitChildren = visitor.visit(this);
210 // visit children in normal left to right reading order
211 acceptChild(visitor, getType());
212 acceptChildren(visitor, this.dimensions);
213 acceptChild(visitor, getInitializer());
215 visitor.endVisit(this);
219 * Returns the array type in this array creation expression.
221 * @return the array type
223 public ArrayType getType() {
224 if (this.arrayType == null) {
225 // lazy init must be thread-safe for readers
226 synchronized (this) {
227 if (this.arrayType == null) {
229 this.arrayType = this.ast.newArrayType(
230 this.ast.newPrimitiveType(PrimitiveType.INT));
231 postLazyInit(this.arrayType, TYPE_PROPERTY);
235 return this.arrayType;
239 * Sets the array type in this array creation expression.
241 * @param type the new array type
242 * @exception IllegalArgumentException if:
244 * <li>the node belongs to a different AST</li>
245 * <li>the node already has a parent</li>
248 public void setType(ArrayType type) {
250 throw new IllegalArgumentException();
252 // an ArrayCreation cannot occur inside a ArrayType - cycles not possible
253 ASTNode oldChild = this.arrayType;
254 preReplaceChild(oldChild, type, TYPE_PROPERTY);
255 this.arrayType = type;
256 postReplaceChild(oldChild, type, TYPE_PROPERTY);
260 * Returns the live ordered list of dimension expressions in this array
263 * @return the live list of dimension expressions
264 * (element type: <code>Expression</code>)
266 public List dimensions() {
267 return this.dimensions;
271 * Returns the array initializer of this array creation expression, or
272 * <code>null</code> if there is none.
274 * @return the array initializer node, or <code>null</code> if
277 public ArrayInitializer getInitializer() {
278 return optionalInitializer;
282 * Sets or clears the array initializer of this array creation expression.
284 * @param initializer the array initializer node, or <code>null</code>
286 * @exception IllegalArgumentException if:
288 * <li>the node belongs to a different AST</li>
289 * <li>the node already has a parent</li>
290 * <li>a cycle in would be created</li>
293 public void setInitializer(ArrayInitializer initializer) {
294 // an ArrayCreation may occur inside an ArrayInitializer
296 ASTNode oldChild = this.optionalInitializer;
297 preReplaceChild(oldChild, initializer, INITIALIZER_PROPERTY);
298 this.optionalInitializer = initializer;
299 postReplaceChild(oldChild, initializer, INITIALIZER_PROPERTY);
302 /* (omit javadoc for this method)
303 * Method declared on ASTNode.
306 return BASE_NODE_SIZE + 3 * 4;
309 /* (omit javadoc for this method)
310 * Method declared on ASTNode.
314 + (this.arrayType == null ? 0 : getType().treeSize())
315 + (this.optionalInitializer == null ? 0 : getInitializer().treeSize())
316 + this.dimensions.listSize();