KickJava   Java API By Example, From Geeks To Geeks.

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


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  * Field access expression AST node type.
19  *
20  * <pre>
21  * FieldAccess:
22  * Expression <b>.</b> Identifier
23  * </pre>
24  *
25  * <p>
26  * Note that there are several kinds of expressions that resemble field access
27  * expressions: qualified names, this expressions, and super field access
28  * expressions. The following guidelines help with correct usage:
29  * <ul>
30  * <li>An expression like "foo.this" can only be represented as a this
31  * expression (<code>ThisExpression</code>) containing a simple name.
32  * "this" is a keyword, and therefore invalid as an identifier.</li>
33  * <li>An expression like "this.foo" can only be represented as a field
34  * access expression (<code>FieldAccess</code>) containing a this expression
35  * and a simple name. Again, this is because "this" is a keyword, and
36  * therefore invalid as an identifier.</li>
37  * <li>An expression with "super" can only be represented as a super field
38  * access expression (<code>SuperFieldAccess</code>). "super" is a also
39  * keyword, and therefore invalid as an identifier.</li>
40  * <li>An expression like "foo.bar" can be represented either as a
41  * qualified name (<code>QualifiedName</code>) or as a field access
42  * expression (<code>FieldAccess</code>) containing simple names. Either
43  * is acceptable, and there is no way to choose between them without
44  * information about what the names resolve to
45  * (<code>ASTParser</code> may return either).</li>
46  * <li>Other expressions ending in an identifier, such as "foo().bar" can
47  * only be represented as field access expressions
48  * (<code>FieldAccess</code>).</li>
49  * </ul>
50  * </p>
51  *
52  * @see QualifiedName
53  * @see ThisExpression
54  * @see SuperFieldAccess
55  * @since 2.0
56  */

57 public class FieldAccess extends Expression {
58     
59     /**
60      * The "expression" structural property of this node type.
61      * @since 3.0
62      */

63     public static final ChildPropertyDescriptor EXPRESSION_PROPERTY =
64         new ChildPropertyDescriptor(FieldAccess.class, "expression", Expression.class, MANDATORY, CYCLE_RISK); //$NON-NLS-1$
65

66     /**
67      * The "name" structural property of this node type.
68      * @since 3.0
69      */

70     public static final ChildPropertyDescriptor NAME_PROPERTY =
71         new ChildPropertyDescriptor(FieldAccess.class, "name", SimpleName.class, MANDATORY, NO_CYCLE_RISK); //$NON-NLS-1$
72

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

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

99     public static List JavaDoc propertyDescriptors(int apiLevel) {
100         return PROPERTY_DESCRIPTORS;
101     }
102             
103     /**
104      * The expression; lazily initialized; defaults to an unspecified,
105      * but legal, simple name.
106      */

107     private Expression expression = null;
108
109     /**
110      * The field; lazily initialized; defaults to an unspecified,
111      * but legal, simple field name.
112      */

113     private SimpleName fieldName = null;
114
115     /**
116      * Creates a new unparented node for a field access expression owned by the
117      * given AST. By default, the expression and field are both unspecified,
118      * but legal, names.
119      * <p>
120      * N.B. This constructor is package-private.
121      * </p>
122      *
123      * @param ast the AST that is to own this node
124      */

125     FieldAccess(AST ast) {
126         super(ast);
127     }
128
129     /* (omit javadoc for this method)
130      * Method declared on ASTNode.
131      */

132     final List JavaDoc internalStructuralPropertiesForType(int apiLevel) {
133         return propertyDescriptors(apiLevel);
134     }
135     
136     /* (omit javadoc for this method)
137      * Method declared on ASTNode.
138      */

139     final ASTNode internalGetSetChildProperty(ChildPropertyDescriptor property, boolean get, ASTNode child) {
140         if (property == EXPRESSION_PROPERTY) {
141             if (get) {
142                 return getExpression();
143             } else {
144                 setExpression((Expression) child);
145                 return null;
146             }
147         }
148         if (property == NAME_PROPERTY) {
149             if (get) {
150                 return getName();
151             } else {
152                 setName((SimpleName) child);
153                 return null;
154             }
155         }
156         // allow default implementation to flag the error
157
return super.internalGetSetChildProperty(property, get, child);
158     }
159     
160     /* (omit javadoc for this method)
161      * Method declared on ASTNode.
162      */

163     final int getNodeType0() {
164         return FIELD_ACCESS;
165     }
166
167     /* (omit javadoc for this method)
168      * Method declared on ASTNode.
169      */

170     ASTNode clone0(AST target) {
171         FieldAccess result = new FieldAccess(target);
172         result.setSourceRange(this.getStartPosition(), this.getLength());
173         result.setExpression((Expression) getExpression().clone(target));
174         result.setName((SimpleName) getName().clone(target));
175         return result;
176     }
177
178     /* (omit javadoc for this method)
179      * Method declared on ASTNode.
180      */

181     final boolean subtreeMatch0(ASTMatcher matcher, Object JavaDoc other) {
182         // dispatch to correct overloaded match method
183
return matcher.match(this, other);
184     }
185
186     /* (omit javadoc for this method)
187      * Method declared on ASTNode.
188      */

189     void accept0(ASTVisitor visitor) {
190         boolean visitChildren = visitor.visit(this);
191         if (visitChildren) {
192             // visit children in normal left to right reading order
193
acceptChild(visitor, getExpression());
194             acceptChild(visitor, getName());
195         }
196         visitor.endVisit(this);
197     }
198     
199     /**
200      * Returns the expression of this field access expression.
201      *
202      * @return the expression node
203      */

204     public Expression getExpression() {
205         if (this.expression == null) {
206             // lazy init must be thread-safe for readers
207
synchronized (this) {
208                 if (this.expression == null) {
209                     preLazyInit();
210                     this.expression = new SimpleName(this.ast);
211                     postLazyInit(this.expression, EXPRESSION_PROPERTY);
212                 }
213             }
214         }
215         return this.expression;
216     }
217         
218     /**
219      * Sets the expression of this field access expression.
220      *
221      * @param expression the new expression
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      * <li>a cycle in would be created</li>
227      * </ul>
228      */

229     public void setExpression(Expression expression) {
230         if (expression == null) {
231             throw new IllegalArgumentException JavaDoc();
232         }
233         ASTNode oldChild = this.expression;
234         preReplaceChild(oldChild, expression, EXPRESSION_PROPERTY);
235         this.expression = expression;
236         postReplaceChild(oldChild, expression, EXPRESSION_PROPERTY);
237     }
238
239     /**
240      * Returns the name of the field accessed in this field access expression.
241      *
242      * @return the field name
243      */

244     public SimpleName getName() {
245         if (this.fieldName == null) {
246             // lazy init must be thread-safe for readers
247
synchronized (this) {
248                 if (this.fieldName == null) {
249                     preLazyInit();
250                     this.fieldName = new SimpleName(this.ast);
251                     postLazyInit(this.fieldName, NAME_PROPERTY);
252                 }
253             }
254         }
255         return this.fieldName;
256     }
257         
258     /**
259      * Sets the name of the field accessed in this field access expression.
260      *
261      * @param fieldName the field name
262      * @exception IllegalArgumentException if:
263      * <ul>
264      * <li>the node belongs to a different AST</li>
265      * <li>the node already has a parent</li>
266      * </ul>
267      */

268     public void setName(SimpleName fieldName) {
269         if (fieldName == null) {
270             throw new IllegalArgumentException JavaDoc();
271         }
272         ASTNode oldChild = this.fieldName;
273         preReplaceChild(oldChild, fieldName, NAME_PROPERTY);
274         this.fieldName = fieldName;
275         postReplaceChild(oldChild, fieldName, NAME_PROPERTY);
276     }
277
278     /* (omit javadoc for this method)
279      * Method declared on ASTNode.
280      */

281     int memSize() {
282         // treat Code as free
283
return BASE_NODE_SIZE + 2 * 4;
284     }
285     
286     /**
287      * Resolves and returns the binding for the field accessed by this
288      * expression.
289      * <p>
290      * Note that bindings are generally unavailable unless requested when the
291      * AST is being built.
292      * </p>
293      *
294      * @return the variable binding, or <code>null</code> if the binding cannot
295      * be resolved
296      * @since 3.0
297      */

298     public IVariableBinding resolveFieldBinding() {
299         return this.ast.getBindingResolver().resolveField(this);
300     }
301     
302     /* (omit javadoc for this method)
303      * Method declared on ASTNode.
304      */

305     int treeSize() {
306         return
307             memSize()
308             + (this.expression == null ? 0 : getExpression().treeSize())
309             + (this.fieldName == null ? 0 : getName().treeSize());
310     }
311 }
312
313
Popular Tags