KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2004 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  * AST node for a member reference within a doc comment
19  * ({@link Javadoc}). The principal uses of these are in "@see" and "@link"
20  * tag elements, for references to field members (and occasionally to method
21  * and constructor members).
22  * <pre>
23  * MemberRef:
24  * [ Name ] <b>#</b> Identifier
25  * </pre>
26  *
27  * @see Javadoc
28  * @since 3.0
29  */

30 public class MemberRef extends ASTNode implements IDocElement {
31     
32     /**
33      * The "qualifier" structural property of this node type.
34      * @since 3.0
35      */

36     public static final ChildPropertyDescriptor QUALIFIER_PROPERTY =
37         new ChildPropertyDescriptor(MemberRef.class, "qualifier", Name.class, OPTIONAL, NO_CYCLE_RISK); //$NON-NLS-1$
38

39     /**
40      * The "name" structural property of this node type.
41      * @since 3.0
42      */

43     public static final ChildPropertyDescriptor NAME_PROPERTY =
44         new ChildPropertyDescriptor(MemberRef.class, "name", SimpleName.class, MANDATORY, NO_CYCLE_RISK); //$NON-NLS-1$
45

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

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

70     public static List JavaDoc propertyDescriptors(int apiLevel) {
71         return PROPERTY_DESCRIPTORS;
72     }
73             
74     /**
75      * The optional qualifier; <code>null</code> for none; defaults to none.
76      */

77     private Name optionalQualifier = null;
78
79     /**
80      * The member name; lazily initialized; defaults to a unspecified,
81      * legal Java method name.
82      */

83     private SimpleName memberName = null;
84     
85     /**
86      * Creates a new AST node for a member reference owned by the given
87      * AST. By default, the method reference is for a member with an
88      * unspecified, but legal, name; and no qualifier.
89      * <p>
90      * N.B. This constructor is package-private; all subclasses must be
91      * declared in the same package; clients are unable to declare
92      * additional subclasses.
93      * </p>
94      *
95      * @param ast the AST that is to own this node
96      */

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

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

111     final ASTNode internalGetSetChildProperty(ChildPropertyDescriptor property, boolean get, ASTNode child) {
112         if (property == QUALIFIER_PROPERTY) {
113             if (get) {
114                 return getQualifier();
115             } else {
116                 setQualifier((Name) child);
117                 return null;
118             }
119         }
120         if (property == NAME_PROPERTY) {
121             if (get) {
122                 return getName();
123             } else {
124                 setName((SimpleName) child);
125                 return null;
126             }
127         }
128         // allow default implementation to flag the error
129
return super.internalGetSetChildProperty(property, get, child);
130     }
131     
132     /* (omit javadoc for this method)
133      * Method declared on ASTNode.
134      */

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

142     ASTNode clone0(AST target) {
143         MemberRef result = new MemberRef(target);
144         result.setSourceRange(this.getStartPosition(), this.getLength());
145         result.setQualifier((Name) ASTNode.copySubtree(target, getQualifier()));
146         result.setName((SimpleName) ASTNode.copySubtree(target, getName()));
147         return result;
148     }
149
150     /* (omit javadoc for this method)
151      * Method declared on ASTNode.
152      */

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

161     void accept0(ASTVisitor visitor) {
162         boolean visitChildren = visitor.visit(this);
163         if (visitChildren) {
164             // visit children in normal left to right reading order
165
acceptChild(visitor, getQualifier());
166             acceptChild(visitor, getName());
167         }
168         visitor.endVisit(this);
169     }
170     
171     /**
172      * Returns the qualifier of this member reference, or
173      * <code>null</code> if there is none.
174      *
175      * @return the qualifier name node, or <code>null</code> if there is none
176      */

177     public Name getQualifier() {
178         return this.optionalQualifier;
179     }
180     
181     /**
182      * Sets or clears the qualifier of this member reference.
183      *
184      * @param name the qualifier name node, or <code>null</code> if
185      * there is none
186      * @exception IllegalArgumentException if:
187      * <ul>
188      * <li>the node belongs to a different AST</li>
189      * <li>the node already has a parent</li>
190      * </ul>
191      */

192     public void setQualifier(Name name) {
193         ASTNode oldChild = this.optionalQualifier;
194         preReplaceChild(oldChild, name, QUALIFIER_PROPERTY);
195         this.optionalQualifier = name;
196         postReplaceChild(oldChild, name, QUALIFIER_PROPERTY);
197     }
198
199     /**
200      * Returns the name of the referenced member.
201      *
202      * @return the member name node
203      */

204     public SimpleName getName() {
205         if (this.memberName == null) {
206             // lazy init must be thread-safe for readers
207
synchronized (this) {
208                 if (this.memberName == null) {
209                     preLazyInit();
210                     this.memberName = new SimpleName(this.ast);
211                     postLazyInit(this.memberName, NAME_PROPERTY);
212                 }
213             }
214         }
215         return this.memberName;
216     }
217     
218     /**
219      * Sets the name of the referenced member to the given name.
220      *
221      * @param name the new member name node
222      * @exception IllegalArgumentException if:
223      * <ul>
224      * <li>the name is <code>null</code></li>
225      * <li>the node belongs to a different AST</li>
226      * <li>the node already has a parent</li>
227      * </ul>
228      */

229     public void setName(SimpleName name) {
230         if (name == null) {
231             throw new IllegalArgumentException JavaDoc();
232         }
233         ASTNode oldChild = this.memberName;
234         preReplaceChild(oldChild, name, NAME_PROPERTY);
235         this.memberName = name;
236         postReplaceChild(oldChild, name, NAME_PROPERTY);
237     }
238
239     /**
240      * Resolves and returns the binding for the entity referred to by
241      * this member reference.
242      * <p>
243      * Note that bindings are generally unavailable unless requested when the
244      * AST is being built.
245      * </p>
246      *
247      * @return the binding, or <code>null</code> if the binding cannot be
248      * resolved
249      */

250     public final IBinding resolveBinding() {
251         return this.ast.getBindingResolver().resolveReference(this);
252     }
253
254     /* (omit javadoc for this method)
255      * Method declared on ASTNode.
256      */

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

264     int treeSize() {
265         return
266             memSize()
267             + (this.optionalQualifier == null ? 0 : getQualifier().treeSize())
268             + (this.memberName == null ? 0 : getName().treeSize());
269     }
270 }
271
272
Popular Tags