KickJava   Java API By Example, From Geeks To Geeks.

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


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

30 public class MethodRef 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(MethodRef.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(MethodRef.class, "name", SimpleName.class, MANDATORY, NO_CYCLE_RISK); //$NON-NLS-1$
45

46     /**
47      * The "parameters" structural property of this node type.
48      * @since 3.0
49      */

50     public static final ChildListPropertyDescriptor PARAMETERS_PROPERTY =
51         new ChildListPropertyDescriptor(MethodRef.class, "parameters", MethodRefParameter.class, NO_CYCLE_RISK); //$NON-NLS-1$
52

53     /**
54      * A list of property descriptors (element type:
55      * {@link StructuralPropertyDescriptor}),
56      * or null if uninitialized.
57      */

58     private static final List JavaDoc PROPERTY_DESCRIPTORS;
59     
60     static {
61         List JavaDoc properyList = new ArrayList JavaDoc(4);
62         createPropertyList(MethodRef.class, properyList);
63         addProperty(QUALIFIER_PROPERTY, properyList);
64         addProperty(NAME_PROPERTY, properyList);
65         addProperty(PARAMETERS_PROPERTY, properyList);
66         PROPERTY_DESCRIPTORS = reapPropertyList(properyList);
67     }
68
69     /**
70      * Returns a list of structural property descriptors for this node type.
71      * Clients must not modify the result.
72      *
73      * @param apiLevel the API level; one of the AST.JLS* constants
74      * @return a list of property descriptors (element type:
75      * {@link StructuralPropertyDescriptor})
76      * @since 3.0
77      */

78     public static List JavaDoc propertyDescriptors(int apiLevel) {
79         return PROPERTY_DESCRIPTORS;
80     }
81             
82     /**
83      * The optional qualifier; <code>null</code> for none; defaults to none.
84      */

85     private Name optionalQualifier = null;
86
87     /**
88      * The method name; lazily initialized; defaults to a unspecified,
89      * legal Java method name.
90      */

91     private SimpleName methodName = null;
92     
93     /**
94      * The parameter declarations
95      * (element type: <code>MethodRefParameter</code>).
96      * Defaults to an empty list.
97      */

98     private ASTNode.NodeList parameters =
99         new ASTNode.NodeList(PARAMETERS_PROPERTY);
100     
101     
102     /**
103      * Creates a new AST node for a method reference owned by the given
104      * AST. By default, the method reference is for a method with an
105      * unspecified, but legal, name; no qualifier; and an empty parameter
106      * list.
107      * <p>
108      * N.B. This constructor is package-private; all subclasses must be
109      * declared in the same package; clients are unable to declare
110      * additional subclasses.
111      * </p>
112      *
113      * @param ast the AST that is to own this node
114      */

115     MethodRef(AST ast) {
116         super(ast);
117     }
118
119     /* (omit javadoc for this method)
120      * Method declared on ASTNode.
121      */

122     final List JavaDoc internalStructuralPropertiesForType(int apiLevel) {
123         return propertyDescriptors(apiLevel);
124     }
125     
126     /* (omit javadoc for this method)
127      * Method declared on ASTNode.
128      */

129     final ASTNode internalGetSetChildProperty(ChildPropertyDescriptor property, boolean get, ASTNode child) {
130         if (property == QUALIFIER_PROPERTY) {
131             if (get) {
132                 return getQualifier();
133             } else {
134                 setQualifier((Name) child);
135                 return null;
136             }
137         }
138         if (property == NAME_PROPERTY) {
139             if (get) {
140                 return getName();
141             } else {
142                 setName((SimpleName) child);
143                 return null;
144             }
145         }
146         // allow default implementation to flag the error
147
return super.internalGetSetChildProperty(property, get, child);
148     }
149     
150     /* (omit javadoc for this method)
151      * Method declared on ASTNode.
152      */

153     final List JavaDoc internalGetChildListProperty(ChildListPropertyDescriptor property) {
154         if (property == PARAMETERS_PROPERTY) {
155             return parameters();
156         }
157         // allow default implementation to flag the error
158
return super.internalGetChildListProperty(property);
159     }
160
161     /* (omit javadoc for this method)
162      * Method declared on ASTNode.
163      */

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

171     ASTNode clone0(AST target) {
172         MethodRef result = new MethodRef(target);
173         result.setSourceRange(this.getStartPosition(), this.getLength());
174         result.setQualifier((Name) ASTNode.copySubtree(target, getQualifier()));
175         result.setName((SimpleName) ASTNode.copySubtree(target, getName()));
176         result.parameters().addAll(
177             ASTNode.copySubtrees(target, parameters()));
178         return result;
179     }
180
181     /* (omit javadoc for this method)
182      * Method declared on ASTNode.
183      */

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

192     void accept0(ASTVisitor visitor) {
193         boolean visitChildren = visitor.visit(this);
194         if (visitChildren) {
195             // visit children in normal left to right reading order
196
acceptChild(visitor, getQualifier());
197             acceptChild(visitor, getName());
198             acceptChildren(visitor, this.parameters);
199         }
200         visitor.endVisit(this);
201     }
202     
203     /**
204      * Returns the qualifier of this method reference, or
205      * <code>null</code> if there is none.
206      *
207      * @return the qualifier name node, or <code>null</code> if there is none
208      */

209     public Name getQualifier() {
210         return this.optionalQualifier;
211     }
212     
213     /**
214      * Sets or clears the qualifier of this method reference.
215      *
216      * @param name the qualifier name node, or <code>null</code> if
217      * there is none
218      * @exception IllegalArgumentException if:
219      * <ul>
220      * <li>the node belongs to a different AST</li>
221      * <li>the node already has a parent</li>
222      * </ul>
223      */

224     public void setQualifier(Name name) {
225         ASTNode oldChild = this.optionalQualifier;
226         preReplaceChild(oldChild, name, QUALIFIER_PROPERTY);
227         this.optionalQualifier = name;
228         postReplaceChild(oldChild, name, QUALIFIER_PROPERTY);
229     }
230
231     /**
232      * Returns the name of the referenced method or constructor.
233      *
234      * @return the method or constructor name node
235      */

236     public SimpleName getName() {
237         if (this.methodName == null) {
238             // lazy init must be thread-safe for readers
239
synchronized (this) {
240                 if (this.methodName == null) {
241                     preLazyInit();
242                     this.methodName = new SimpleName(this.ast);
243                     postLazyInit(this.methodName, NAME_PROPERTY);
244                 }
245             }
246         }
247         return this.methodName;
248     }
249     
250     /**
251      * Sets the name of the referenced method or constructor to the
252      * given name.
253      *
254      * @param name the new method or constructor name node
255      * @exception IllegalArgumentException if:
256      * <ul>
257      * <li>the name is <code>null</code></li>
258      * <li>the node belongs to a different AST</li>
259      * <li>the node already has a parent</li>
260      * </ul>
261      */

262     public void setName(SimpleName name) {
263         if (name == null) {
264             throw new IllegalArgumentException JavaDoc();
265         }
266         ASTNode oldChild = this.methodName;
267         preReplaceChild(oldChild, name, NAME_PROPERTY);
268         this.methodName = name;
269         postReplaceChild(oldChild, name, NAME_PROPERTY);
270     }
271
272     /**
273      * Returns the live ordered list of method parameter references for this
274      * method reference.
275      *
276      * @return the live list of method parameter references
277      * (element type: <code>MethodRefParameter</code>)
278      */

279     public List JavaDoc parameters() {
280         return this.parameters;
281     }
282     
283     /**
284      * Resolves and returns the binding for the entity referred to by
285      * this method reference.
286      * <p>
287      * Note that bindings are generally unavailable unless requested when the
288      * AST is being built.
289      * </p>
290      *
291      * @return the binding, or <code>null</code> if the binding cannot be
292      * resolved
293      */

294     public final IBinding resolveBinding() {
295         return this.ast.getBindingResolver().resolveReference(this);
296     }
297
298     /* (omit javadoc for this method)
299      * Method declared on ASTNode.
300      */

301     int memSize() {
302         return BASE_NODE_SIZE + 3 * 4;
303     }
304     
305     /* (omit javadoc for this method)
306      * Method declared on ASTNode.
307      */

308     int treeSize() {
309         return
310             memSize()
311             + (this.optionalQualifier == null ? 0 : getQualifier().treeSize())
312             + (this.methodName == null ? 0 : getName().treeSize())
313             + this.parameters.listSize();
314     }
315 }
316
317
Popular Tags