1 | /******************************************************************************* |
---|---|
2 | * Copyright (c) 2000, 2013 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 | *******************************************************************************/ |
14 | |
15 | package org.eclipse.jdt.core.dom; |
16 | |
17 | import java.util.ArrayList; |
18 | import java.util.List; |
19 | |
20 | /** |
21 | * Array access expression AST node type. |
22 | * |
23 | * <pre> |
24 | * ArrayAccess: |
25 | * Expression <b>[</b> Expression <b>]</b> |
26 | * </pre> |
27 | * |
28 | * @since 2.0 |
29 | * @noinstantiate This class is not intended to be instantiated by clients. |
30 | */ |
31 | @SuppressWarnings("rawtypes") |
32 | public class ArrayAccess extends Expression { |
33 | |
34 | /** |
35 | * The "array" structural property of this node type (child type: {@link Expression}). |
36 | * @since 3.0 |
37 | */ |
38 | public static final ChildPropertyDescriptor ARRAY_PROPERTY = |
39 | new ChildPropertyDescriptor(ArrayAccess.class, "array", Expression.class, MANDATORY, CYCLE_RISK); //$NON-NLS-1$ |
40 | |
41 | /** |
42 | * The "index" structural property of this node type (child type: {@link Expression}). |
43 | * @since 3.0 |
44 | */ |
45 | public static final ChildPropertyDescriptor INDEX_PROPERTY = |
46 | new ChildPropertyDescriptor(ArrayAccess.class, "index", Expression.class, MANDATORY, CYCLE_RISK); //$NON-NLS-1$ |
47 | |
48 | /** |
49 | * A list of property descriptors (element type: |
50 | * {@link StructuralPropertyDescriptor}), |
51 | * or null if uninitialized. |
52 | */ |
53 | private static final List PROPERTY_DESCRIPTORS; |
54 | |
55 | static { |
56 | List properyList = new ArrayList(3); |
57 | createPropertyList(ArrayAccess.class, properyList); |
58 | addProperty(ARRAY_PROPERTY, properyList); |
59 | addProperty(INDEX_PROPERTY, properyList); |
60 | PROPERTY_DESCRIPTORS = reapPropertyList(properyList); |
61 | } |
62 | |
63 | /** |
64 | * Returns a list of structural property descriptors for this node type. |
65 | * Clients must not modify the result. |
66 | * |
67 | * @param apiLevel the API level; one of the |
68 | * <code>AST.JLS*</code> constants |
69 | |
70 | * @return a list of property descriptors (element type: |
71 | * {@link StructuralPropertyDescriptor}) |
72 | * @since 3.0 |
73 | */ |
74 | public static List propertyDescriptors(int apiLevel) { |
75 | return PROPERTY_DESCRIPTORS; |
76 | } |
77 | |
78 | /** |
79 | * The array expression; lazily initialized; defaults to an unspecified, |
80 | * but legal, expression. |
81 | */ |
82 | private Expression arrayExpression = null; |
83 | |
84 | /** |
85 | * The index expression; lazily initialized; defaults to an unspecified, |
86 | * but legal, expression. |
87 | */ |
88 | private Expression indexExpression = null; |
89 | |
90 | /** |
91 | * Creates a new unparented array access expression node owned by the given |
92 | * AST. By default, the array and index expresssions are unspecified, |
93 | * but legal. |
94 | * <p> |
95 | * N.B. This constructor is package-private. |
96 | * </p> |
97 | * |
98 | * @param ast the AST that is to own this node |
99 | */ |
100 | ArrayAccess(AST ast) { |
101 | super(ast); |
102 | } |
103 | |
104 | @Override |
105 | final List internalStructuralPropertiesForType(int apiLevel) { |
106 | return propertyDescriptors(apiLevel); |
107 | } |
108 | |
109 | @Override |
110 | final ASTNode internalGetSetChildProperty(ChildPropertyDescriptor property, boolean get, ASTNode child) { |
111 | if (property == ARRAY_PROPERTY) { |
112 | if (get) { |
113 | return getArray(); |
114 | } else { |
115 | setArray((Expression) child); |
116 | return null; |
117 | } |
118 | } |
119 | if (property == INDEX_PROPERTY) { |
120 | if (get) { |
121 | return getIndex(); |
122 | } else { |
123 | setIndex((Expression) child); |
124 | return null; |
125 | } |
126 | } |
127 | // allow default implementation to flag the error |
128 | return super.internalGetSetChildProperty(property, get, child); |
129 | } |
130 | |
131 | @Override |
132 | final int getNodeType0() { |
133 | return ARRAY_ACCESS; |
134 | } |
135 | |
136 | @Override |
137 | ASTNode clone0(AST target) { |
138 | ArrayAccess result = new ArrayAccess(target); |
139 | result.setSourceRange(getStartPosition(), getLength()); |
140 | result.setArray((Expression) getArray().clone(target)); |
141 | result.setIndex((Expression) getIndex().clone(target)); |
142 | return result; |
143 | } |
144 | |
145 | @Override |
146 | final boolean subtreeMatch0(ASTMatcher matcher, Object other) { |
147 | // dispatch to correct overloaded match method |
148 | return matcher.match(this, other); |
149 | } |
150 | |
151 | @Override |
152 | void accept0(ASTVisitor visitor) { |
153 | boolean visitChildren = visitor.visit(this); |
154 | if (visitChildren) { |
155 | // visit children in normal left to right reading order |
156 | acceptChild(visitor, getArray()); |
157 | acceptChild(visitor, getIndex()); |
158 | } |
159 | visitor.endVisit(this); |
160 | } |
161 | |
162 | /** |
163 | * Returns the array expression of this array access expression. |
164 | * |
165 | * @return the array expression node |
166 | */ |
167 | public Expression getArray() { |
168 | if (this.arrayExpression == null) { |
169 | // lazy init must be thread-safe for readers |
170 | synchronized (this) { |
171 | if (this.arrayExpression == null) { |
172 | preLazyInit(); |
173 | this.arrayExpression = new SimpleName(this.ast); |
174 | postLazyInit(this.arrayExpression, ARRAY_PROPERTY); |
175 | } |
176 | } |
177 | } |
178 | return this.arrayExpression; |
179 | } |
180 | |
181 | /** |
182 | * Sets the array expression of this array access expression. |
183 | * |
184 | * @param expression the array expression node |
185 | * @exception IllegalArgumentException if: |
186 | * <ul> |
187 | * <li>the node belongs to a different AST</li> |
188 | * <li>the node already has a parent</li> |
189 | * <li>a cycle in would be created</li> |
190 | * </ul> |
191 | */ |
192 | public void setArray(Expression expression) { |
193 | if (expression == null) { |
194 | throw new IllegalArgumentException(); |
195 | } |
196 | // an ArrayAccess may occur inside an Expression |
197 | // must check cycles |
198 | ASTNode oldChild = this.arrayExpression; |
199 | preReplaceChild(oldChild, expression, ARRAY_PROPERTY); |
200 | this.arrayExpression = expression; |
201 | postReplaceChild(oldChild, expression, ARRAY_PROPERTY); |
202 | } |
203 | |
204 | /** |
205 | * Returns the index expression of this array access expression. |
206 | * |
207 | * @return the index expression node |
208 | */ |
209 | public Expression getIndex() { |
210 | if (this.indexExpression == null) { |
211 | // lazy init must be thread-safe for readers |
212 | synchronized (this) { |
213 | if (this.indexExpression == null) { |
214 | preLazyInit(); |
215 | this.indexExpression = new SimpleName(this.ast); |
216 | postLazyInit(this.indexExpression, INDEX_PROPERTY); |
217 | } |
218 | } |
219 | } |
220 | return this.indexExpression; |
221 | } |
222 | |
223 | /** |
224 | * Sets the index expression of this array access expression. |
225 | * |
226 | * @param expression the index expression node |
227 | * @exception IllegalArgumentException if: |
228 | * <ul> |
229 | * <li>the node belongs to a different AST</li> |
230 | * <li>the node already has a parent</li> |
231 | * <li>a cycle in would be created</li> |
232 | * </ul> |
233 | */ |
234 | public void setIndex(Expression expression) { |
235 | if (expression == null) { |
236 | throw new IllegalArgumentException(); |
237 | } |
238 | // an ArrayAccess may occur inside an Expression |
239 | // must check cycles |
240 | ASTNode oldChild = this.indexExpression; |
241 | preReplaceChild(oldChild, expression, INDEX_PROPERTY); |
242 | this.indexExpression = expression; |
243 | postReplaceChild(oldChild, expression, INDEX_PROPERTY); |
244 | } |
245 | |
246 | @Override |
247 | int memSize() { |
248 | return BASE_NODE_SIZE + 2 * 4; |
249 | } |
250 | |
251 | @Override |
252 | int treeSize() { |
253 | return |
254 | memSize() |
255 | + (this.arrayExpression == null ? 0 : getArray().treeSize()) |
256 | + (this.indexExpression == null ? 0 : getIndex().treeSize()); |
257 | } |
258 | } |
259 | |
260 |
Members