EclipseJDT Source Viewer

Home|eclipse_jdt/src/org/eclipse/jdt/core/dom/ArrayCreation.java
1/*******************************************************************************
2 * Copyright (c) 2000, 2019 IBM Corporation and others.
3 *
4 * This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License 2.0
6 * which accompanies this distribution, and is available at
7 * https://www.eclipse.org/legal/epl-2.0/
8 *
9 * SPDX-License-Identifier: EPL-2.0
10 *
11 * Contributors:
12 *     IBM Corporation - initial API and implementation
13 *******************************************************************************/
14package org.eclipse.jdt.core.dom;
15
16import java.util.ArrayList;
17import java.util.List;
18
19/**
20 * Array creation expression AST node type.
21 * <pre>
22 * ArrayCreation:
23 *    <b>new</b> PrimitiveType <b>[</b> Expression <b>]</b> { <b>[</b> Expression <b>]</b> } { <b>[</b> <b>]</b> }
24 *    <b>new</b> TypeName [ <b>&lt;</b> Type { <b>,</b> Type } <b>&gt;</b> ]
25 *        <b>[</b> Expression <b>]</b> { <b>[</b> Expression <b>]</b> } { <b>[</b> <b>]</b> }
26 *    <b>new</b> PrimitiveType <b>[</b> <b>]</b> { <b>[</b> <b>]</b> } ArrayInitializer
27 *    <b>new</b> TypeName [ <b>&lt;</b> Type { <b>,</b> Type } <b>&gt;</b> ]
28 *        <b>[</b> <b>]</b> { <b>[</b> <b>]</b> } ArrayInitializer
29 * </pre>
30 * <p>
31 * The mapping from Java language syntax to AST nodes is as follows:
32 * <ul>
33 * <li>the type node is the array type of the creation expression,
34 *   with one level of array per set of square brackets,</li>
35 * <li>the dimension expressions are collected into the <code>dimensions</code>
36 *   list.</li>
37 * </ul>
38 *
39 * @since 2.0
40 * @noinstantiate This class is not intended to be instantiated by clients.
41 */
42@SuppressWarnings({"rawtypes""unchecked"})
43public class ArrayCreation extends Expression {
44
45    /**
46     * The "type" structural property of this node type (child type: {@link ArrayType}).
47     * @since 3.0
48     */
49    public static final ChildPropertyDescriptor TYPE_PROPERTY =
50        new ChildPropertyDescriptor(ArrayCreation.class"type"ArrayType.classMANDATORYNO_CYCLE_RISK); //$NON-NLS-1$
51
52    /**
53     * The "dimensions" structural property of this node type (element type: {@link Expression}).
54     * @since 3.0
55     */
56    public static final ChildListPropertyDescriptor DIMENSIONS_PROPERTY =
57        new ChildListPropertyDescriptor(ArrayCreation.class"dimensions"Expression.classCYCLE_RISK); //$NON-NLS-1$
58
59    /**
60     * The "initializer" structural property of this node type (child type: {@link ArrayInitializer}).
61     * @since 3.0
62     */
63    public static final ChildPropertyDescriptor INITIALIZER_PROPERTY =
64        new ChildPropertyDescriptor(ArrayCreation.class"initializer"ArrayInitializer.classOPTIONALCYCLE_RISK); //$NON-NLS-1$
65
66    /**
67     * A list of property descriptors (element type:
68     * {@link StructuralPropertyDescriptor}),
69     * or null if uninitialized.
70     */
71    private static final List PROPERTY_DESCRIPTORS;
72
73    static {
74        List properyList = new ArrayList(4);
75        createPropertyList(ArrayCreation.classproperyList);
76        addProperty(TYPE_PROPERTYproperyList);
77        addProperty(DIMENSIONS_PROPERTYproperyList);
78        addProperty(INITIALIZER_PROPERTYproperyList);
79        PROPERTY_DESCRIPTORS = reapPropertyList(properyList);
80    }
81
82    /**
83     * Returns a list of structural property descriptors for this node type.
84     * Clients must not modify the result.
85     *
86     * @param apiLevel the API level; one of the
87     * <code>AST.JLS*</code> constants
88
89     * @return a list of property descriptors (element type:
90     * {@link StructuralPropertyDescriptor})
91     * @since 3.0
92     */
93    public static List propertyDescriptors(int apiLevel) {
94        return PROPERTY_DESCRIPTORS;
95    }
96
97    /**
98     * The array type; lazily initialized; defaults to a unspecified,
99     * legal array type.
100     */
101    private ArrayType arrayType = null;
102
103    /**
104     * The list of dimension expressions (element type:
105     * {@link Expression}). Defaults to an empty list.
106     */
107    private ASTNode.NodeList dimensions =
108        new ASTNode.NodeList(DIMENSIONS_PROPERTY);
109
110    /**
111     * The optional array initializer, or <code>null</code> if none;
112     * defaults to none.
113     */
114    private ArrayInitializer optionalInitializer = null;
115
116    /**
117     * Creates a new AST node for an array creation expression owned by the
118     * given AST. By default, the array type is an unspecified 1-dimensional
119     * array, the list of dimensions is empty, and there is no array
120     * initializer.
121     *
122     * @param ast the AST that is to own this node
123     */
124    ArrayCreation(AST ast) {
125        super(ast);
126    }
127
128    @Override
129    final List internalStructuralPropertiesForType(int apiLevel) {
130        return propertyDescriptors(apiLevel);
131    }
132
133    @Override
134    final ASTNode internalGetSetChildProperty(ChildPropertyDescriptor propertyboolean getASTNode child) {
135        if (property == INITIALIZER_PROPERTY) {
136            if (get) {
137                return getInitializer();
138            } else {
139                setInitializer((ArrayInitializerchild);
140                return null;
141            }
142        }
143        if (property == TYPE_PROPERTY) {
144            if (get) {
145                return getType();
146            } else {
147                setType((ArrayTypechild);
148                return null;
149            }
150        }
151        // allow default implementation to flag the error
152        return super.internalGetSetChildProperty(propertygetchild);
153    }
154
155    @Override
156    final List internalGetChildListProperty(ChildListPropertyDescriptor property) {
157        if (property == DIMENSIONS_PROPERTY) {
158            return dimensions();
159        }
160        // allow default implementation to flag the error
161        return super.internalGetChildListProperty(property);
162    }
163
164    @Override
165    final int getNodeType0() {
166        return ARRAY_CREATION;
167    }
168
169    @Override
170    ASTNode clone0(AST target) {
171        ArrayCreation result = new ArrayCreation(target);
172        result.setSourceRange(getStartPosition(), getLength());
173        result.setType((ArrayTypegetType().clone(target));
174        result.dimensions().addAll(ASTNode.copySubtrees(targetdimensions()));
175        result.setInitializer(
176            (ArrayInitializerASTNode.copySubtree(targetgetInitializer()));
177        return result;
178    }
179
180    @Override
181    final boolean subtreeMatch0(ASTMatcher matcherObject other) {
182        // dispatch to correct overloaded match method
183        return matcher.match(this, other);
184    }
185
186    @Override
187    void accept0(ASTVisitor visitor) {
188        boolean visitChildren = visitor.visit(this);
189        if (visitChildren) {
190            // visit children in normal left to right reading order
191            acceptChild(visitorgetType());
192            acceptChildren(visitor, this.dimensions);
193            acceptChild(visitorgetInitializer());
194        }
195        visitor.endVisit(this);
196    }
197
198    /**
199     * Returns the array type in this array creation expression.
200     *
201     * @return the array type
202     */
203    public ArrayType getType() {
204        if (this.arrayType == null) {
205            // lazy init must be thread-safe for readers
206            synchronized (this) {
207                if (this.arrayType == null) {
208                    preLazyInit();
209                    this.arrayType = this.ast.newArrayType(
210                            this.ast.newPrimitiveType(PrimitiveType.INT));
211                    postLazyInit(this.arrayTypeTYPE_PROPERTY);
212                }
213            }
214        }
215        return this.arrayType;
216    }
217
218    /**
219     * Sets the array type in this array creation expression.
220     *
221     * @param type the new array type
222     * @exception IllegalArgumentException if:
223     * <ul>
224     * <li>the node belongs to a different AST</li>
225     * <li>the node already has a parent</li>
226     * </ul>
227     */
228    public void setType(ArrayType type) {
229        if (type == null) {
230            throw new IllegalArgumentException();
231        }
232        // an ArrayCreation cannot occur inside a ArrayType - cycles not possible
233        ASTNode oldChild = this.arrayType;
234        preReplaceChild(oldChildtypeTYPE_PROPERTY);
235        this.arrayType = type;
236        postReplaceChild(oldChildtypeTYPE_PROPERTY);
237    }
238
239    /**
240     * Returns the live ordered list of dimension expressions in this array
241     * initializer.
242     *
243     * @return the live list of dimension expressions
244     *    (element type: {@link Expression})
245     */
246    public List dimensions() {
247        return this.dimensions;
248    }
249
250    /**
251     * Returns the array initializer of this array creation expression, or
252     * <code>null</code> if there is none.
253     *
254     * @return the array initializer node, or <code>null</code> if
255     *    there is none
256     */
257    public ArrayInitializer getInitializer() {
258        return this.optionalInitializer;
259    }
260
261    /**
262     * Sets or clears the array initializer of this array creation expression.
263     *
264     * @param initializer the array initializer node, or <code>null</code>
265     *    if there is none
266     * @exception IllegalArgumentException if:
267     * <ul>
268     * <li>the node belongs to a different AST</li>
269     * <li>the node already has a parent</li>
270     * <li>a cycle in would be created</li>
271     * </ul>
272     */
273    public void setInitializer(ArrayInitializer initializer) {
274        // an ArrayCreation may occur inside an ArrayInitializer
275        // must check cycles
276        ASTNode oldChild = this.optionalInitializer;
277        preReplaceChild(oldChildinitializerINITIALIZER_PROPERTY);
278        this.optionalInitializer = initializer;
279        postReplaceChild(oldChildinitializerINITIALIZER_PROPERTY);
280    }
281
282    @Override
283    int memSize() {
284        return BASE_NODE_SIZE + 3 * 4;
285    }
286
287    @Override
288    int treeSize() {
289        int size = memSize()
290            + (this.arrayType == null ? 0 : getType().treeSize())
291            + (this.optionalInitializer == null ? 0 : getInitializer().treeSize())
292            + this.dimensions.listSize();
293        return size;
294    }
295}
296
297
MembersX
ArrayCreation:arrayType
ArrayCreation:internalStructuralPropertiesForType
ArrayCreation:DIMENSIONS_PROPERTY
ArrayCreation:accept0:Block:visitChildren
ArrayCreation:setInitializer:Block:oldChild
ArrayCreation:dimensions
ArrayCreation:ArrayCreation
ArrayCreation:INITIALIZER_PROPERTY
ArrayCreation:setType
ArrayCreation:setType:Block:oldChild
ArrayCreation:subtreeMatch0
ArrayCreation:propertyDescriptors
ArrayCreation:optionalInitializer
ArrayCreation:getNodeType0
ArrayCreation:internalGetSetChildProperty
ArrayCreation:accept0
ArrayCreation:TYPE_PROPERTY
ArrayCreation:getInitializer
ArrayCreation:treeSize
ArrayCreation:setInitializer
ArrayCreation:getType
ArrayCreation:memSize
ArrayCreation:treeSize:Block:size
ArrayCreation:PROPERTY_DESCRIPTORS
ArrayCreation:clone0
ArrayCreation:internalGetChildListProperty
ArrayCreation:Block:properyList
ArrayCreation:clone0:Block:result
Members
X