KickJava   Java API By Example, From Geeks To Geeks.

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


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  * Array access expression AST node type.
19  *
20  * <pre>
21  * ArrayAccess:
22  * Expression <b>[</b> Expression <b>]</b>
23  * </pre>
24  *
25  * @since 2.0
26  */

27 public class ArrayAccess extends Expression {
28     
29     /**
30      * The "array" structural property of this node type.
31      * @since 3.0
32      */

33     public static final ChildPropertyDescriptor ARRAY_PROPERTY =
34         new ChildPropertyDescriptor(ArrayAccess.class, "array", Expression.class, MANDATORY, CYCLE_RISK); //$NON-NLS-1$
35

36     /**
37      * The "index" structural property of this node type.
38      * @since 3.0
39      */

40     public static final ChildPropertyDescriptor INDEX_PROPERTY =
41         new ChildPropertyDescriptor(ArrayAccess.class, "index", Expression.class, MANDATORY, CYCLE_RISK); //$NON-NLS-1$
42

43     /**
44      * A list of property descriptors (element type:
45      * {@link StructuralPropertyDescriptor}),
46      * or null if uninitialized.
47      */

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

69     public static List JavaDoc propertyDescriptors(int apiLevel) {
70         return PROPERTY_DESCRIPTORS;
71     }
72             
73     /**
74      * The array expression; lazily initialized; defaults to an unspecified,
75      * but legal, expression.
76      */

77     private Expression arrayExpression = null;
78
79     /**
80      * The index expression; lazily initialized; defaults to an unspecified,
81      * but legal, expression.
82      */

83     private Expression indexExpression = null;
84
85     /**
86      * Creates a new unparented array access expression node owned by the given
87      * AST. By default, the array and index expresssions are unspecified,
88      * but legal.
89      * <p>
90      * N.B. This constructor is package-private.
91      * </p>
92      *
93      * @param ast the AST that is to own this node
94      */

95     ArrayAccess(AST ast) {
96         super(ast);
97     }
98
99     /* (omit javadoc for this method)
100      * Method declared on ASTNode.
101      */

102     final List JavaDoc internalStructuralPropertiesForType(int apiLevel) {
103         return propertyDescriptors(apiLevel);
104     }
105     
106     /* (omit javadoc for this method)
107      * Method declared on ASTNode.
108      */

109     final ASTNode internalGetSetChildProperty(ChildPropertyDescriptor property, boolean get, ASTNode child) {
110         if (property == ARRAY_PROPERTY) {
111             if (get) {
112                 return getArray();
113             } else {
114                 setArray((Expression) child);
115                 return null;
116             }
117         }
118         if (property == INDEX_PROPERTY) {
119             if (get) {
120                 return getIndex();
121             } else {
122                 setIndex((Expression) child);
123                 return null;
124             }
125         }
126         // allow default implementation to flag the error
127
return super.internalGetSetChildProperty(property, get, child);
128     }
129     
130     /* (omit javadoc for this method)
131      * Method declared on ASTNode.
132      */

133     final int getNodeType0() {
134         return ARRAY_ACCESS;
135     }
136
137     /* (omit javadoc for this method)
138      * Method declared on ASTNode.
139      */

140     ASTNode clone0(AST target) {
141         ArrayAccess result = new ArrayAccess(target);
142         result.setSourceRange(this.getStartPosition(), this.getLength());
143         result.setArray((Expression) getArray().clone(target));
144         result.setIndex((Expression) getIndex().clone(target));
145         return result;
146     }
147
148     /* (omit javadoc for this method)
149      * Method declared on ASTNode.
150      */

151     final boolean subtreeMatch0(ASTMatcher matcher, Object JavaDoc other) {
152         // dispatch to correct overloaded match method
153
return matcher.match(this, other);
154     }
155
156     /* (omit javadoc for this method)
157      * Method declared on ASTNode.
158      */

159     void accept0(ASTVisitor visitor) {
160         boolean visitChildren = visitor.visit(this);
161         if (visitChildren) {
162             // visit children in normal left to right reading order
163
acceptChild(visitor, getArray());
164             acceptChild(visitor, getIndex());
165         }
166         visitor.endVisit(this);
167     }
168     
169     /**
170      * Returns the array expression of this array access expression.
171      *
172      * @return the array expression node
173      */

174     public Expression getArray() {
175         if (this.arrayExpression == null) {
176             // lazy init must be thread-safe for readers
177
synchronized (this) {
178                 if (this.arrayExpression == null) {
179                     preLazyInit();
180                     this.arrayExpression = new SimpleName(this.ast);
181                     postLazyInit(this.arrayExpression, ARRAY_PROPERTY);
182                 }
183             }
184         }
185         return this.arrayExpression;
186     }
187     
188     /**
189      * Sets the array expression of this array access expression.
190      *
191      * @param expression the array expression node
192      * @exception IllegalArgumentException if:
193      * <ul>
194      * <li>the node belongs to a different AST</li>
195      * <li>the node already has a parent</li>
196      * <li>a cycle in would be created</li>
197      * </ul>
198      */

199     public void setArray(Expression expression) {
200         if (expression == null) {
201             throw new IllegalArgumentException JavaDoc();
202         }
203         // an ArrayAccess may occur inside an Expression
204
// must check cycles
205
ASTNode oldChild = this.arrayExpression;
206         preReplaceChild(oldChild, expression, ARRAY_PROPERTY);
207         this.arrayExpression = expression;
208         postReplaceChild(oldChild, expression, ARRAY_PROPERTY);
209     }
210     
211     /**
212      * Returns the index expression of this array access expression.
213      *
214      * @return the index expression node
215      */

216     public Expression getIndex() {
217         if (this.indexExpression == null) {
218             // lazy init must be thread-safe for readers
219
synchronized (this) {
220                 if (this.indexExpression == null) {
221                     preLazyInit();
222                     this.indexExpression = new SimpleName(this.ast);
223                     postLazyInit(this.indexExpression, INDEX_PROPERTY);
224                 }
225             }
226         }
227         return this.indexExpression;
228     }
229     
230     /**
231      * Sets the index expression of this array access expression.
232      *
233      * @param expression the index expression node
234      * @exception IllegalArgumentException if:
235      * <ul>
236      * <li>the node belongs to a different AST</li>
237      * <li>the node already has a parent</li>
238      * <li>a cycle in would be created</li>
239      * </ul>
240      */

241     public void setIndex(Expression expression) {
242         if (expression == null) {
243             throw new IllegalArgumentException JavaDoc();
244         }
245         // an ArrayAccess may occur inside an Expression
246
// must check cycles
247
ASTNode oldChild = this.indexExpression;
248         preReplaceChild(oldChild, expression, INDEX_PROPERTY);
249         this.indexExpression = expression;
250         postReplaceChild(oldChild, expression, INDEX_PROPERTY);
251     }
252
253     /* (omit javadoc for this method)
254      * Method declared on ASTNode.
255      */

256     int memSize() {
257         return BASE_NODE_SIZE + 2 * 4;
258     }
259
260     /* (omit javadoc for this method)
261      * Method declared on ASTNode.
262      */

263     int treeSize() {
264         return
265             memSize()
266             + (this.arrayExpression == null ? 0 : getArray().treeSize())
267             + (this.indexExpression == null ? 0 : getIndex().treeSize());
268     }
269 }
270
271
Popular Tags