1 /*******************************************************************************
2 * Copyright (c) 2004, 2008 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
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
12 package net.sourceforge.phpdt.core.dom;
14 import java.util.ArrayList;
15 import java.util.List;
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.
23 * [ Name ] <b>#</b> Identifier
24 * <b>(</b> [ MethodRefParameter | { <b>,</b> MethodRefParameter } ] <b>)</b>
29 * @noinstantiate This class is not intended to be instantiated by clients.
31 public class MethodRef extends ASTNode implements IDocElement {
34 * The "qualifier" structural property of this node type.
37 public static final ChildPropertyDescriptor QUALIFIER_PROPERTY =
38 new ChildPropertyDescriptor(MethodRef.class, "qualifier", Name.class, OPTIONAL, NO_CYCLE_RISK); //$NON-NLS-1$
41 * The "name" structural property of this node type.
44 public static final ChildPropertyDescriptor NAME_PROPERTY =
45 new ChildPropertyDescriptor(MethodRef.class, "name", SimpleName.class, MANDATORY, NO_CYCLE_RISK); //$NON-NLS-1$
48 * The "parameters" structural property of this node type.
51 public static final ChildListPropertyDescriptor PARAMETERS_PROPERTY =
52 new ChildListPropertyDescriptor(MethodRef.class, "parameters", MethodRefParameter.class, NO_CYCLE_RISK); //$NON-NLS-1$
55 * A list of property descriptors (element type:
56 * {@link StructuralPropertyDescriptor}),
57 * or null if uninitialized.
59 private static final List PROPERTY_DESCRIPTORS;
62 List properyList = new ArrayList(4);
63 createPropertyList(MethodRef.class, properyList);
64 addProperty(QUALIFIER_PROPERTY, properyList);
65 addProperty(NAME_PROPERTY, properyList);
66 addProperty(PARAMETERS_PROPERTY, properyList);
67 PROPERTY_DESCRIPTORS = reapPropertyList(properyList);
71 * Returns a list of structural property descriptors for this node type.
72 * Clients must not modify the result.
74 * @param apiLevel the API level; one of the AST.JLS* constants
75 * @return a list of property descriptors (element type:
76 * {@link StructuralPropertyDescriptor})
79 public static List propertyDescriptors(int apiLevel) {
80 return PROPERTY_DESCRIPTORS;
84 * The optional qualifier; <code>null</code> for none; defaults to none.
86 private Name optionalQualifier = null;
89 * The method name; lazily initialized; defaults to a unspecified,
90 * legal Java method name.
92 private SimpleName methodName = null;
95 * The parameter declarations
96 * (element type: <code>MethodRefParameter</code>).
97 * Defaults to an empty list.
99 private ASTNode.NodeList parameters =
100 new ASTNode.NodeList(PARAMETERS_PROPERTY);
104 * Creates a new AST node for a method reference owned by the given
105 * AST. By default, the method reference is for a method with an
106 * unspecified, but legal, name; no qualifier; and an empty parameter
109 * N.B. This constructor is package-private; all subclasses must be
110 * declared in the same package; clients are unable to declare
111 * additional subclasses.
114 * @param ast the AST that is to own this node
120 /* (omit javadoc for this method)
121 * Method declared on ASTNode.
123 final List internalStructuralPropertiesForType(int apiLevel) {
124 return propertyDescriptors(apiLevel);
127 /* (omit javadoc for this method)
128 * Method declared on ASTNode.
130 final ASTNode internalGetSetChildProperty(ChildPropertyDescriptor property, boolean get, ASTNode child) {
131 if (property == QUALIFIER_PROPERTY) {
133 return getQualifier();
135 setQualifier((Name) child);
139 if (property == NAME_PROPERTY) {
143 setName((SimpleName) child);
147 // allow default implementation to flag the error
148 return super.internalGetSetChildProperty(property, get, child);
151 /* (omit javadoc for this method)
152 * Method declared on ASTNode.
154 final List internalGetChildListProperty(ChildListPropertyDescriptor property) {
155 if (property == PARAMETERS_PROPERTY) {
158 // allow default implementation to flag the error
159 return super.internalGetChildListProperty(property);
162 /* (omit javadoc for this method)
163 * Method declared on ASTNode.
165 final int getNodeType0() {
169 /* (omit javadoc for this method)
170 * Method declared on ASTNode.
172 ASTNode clone0(AST target) {
173 MethodRef result = new MethodRef(target);
174 result.setSourceRange(this.getStartPosition(), this.getLength());
175 result.setQualifier((Name) ASTNode.copySubtree(target, getQualifier()));
176 result.setName((SimpleName) ASTNode.copySubtree(target, getName()));
177 result.parameters().addAll(
178 ASTNode.copySubtrees(target, parameters()));
182 /* (omit javadoc for this method)
183 * Method declared on ASTNode.
185 final boolean subtreeMatch0(ASTMatcher matcher, Object other) {
186 // dispatch to correct overloaded match method
187 return matcher.match(this, other);
190 /* (omit javadoc for this method)
191 * Method declared on ASTNode.
193 void accept0(ASTVisitor visitor) {
194 boolean visitChildren = visitor.visit(this);
196 // visit children in normal left to right reading order
197 acceptChild(visitor, getQualifier());
198 acceptChild(visitor, getName());
199 acceptChildren(visitor, this.parameters);
201 visitor.endVisit(this);
205 * Returns the qualifier of this method reference, or
206 * <code>null</code> if there is none.
208 * @return the qualifier name node, or <code>null</code> if there is none
210 public Name getQualifier() {
211 return this.optionalQualifier;
215 * Sets or clears the qualifier of this method reference.
217 * @param name the qualifier name node, or <code>null</code> if
219 * @exception IllegalArgumentException if:
221 * <li>the node belongs to a different AST</li>
222 * <li>the node already has a parent</li>
225 public void setQualifier(Name name) {
226 ASTNode oldChild = this.optionalQualifier;
227 preReplaceChild(oldChild, name, QUALIFIER_PROPERTY);
228 this.optionalQualifier = name;
229 postReplaceChild(oldChild, name, QUALIFIER_PROPERTY);
233 * Returns the name of the referenced method or constructor.
235 * @return the method or constructor name node
237 public SimpleName getName() {
238 if (this.methodName == null) {
239 // lazy init must be thread-safe for readers
240 synchronized (this) {
241 if (this.methodName == null) {
243 this.methodName = new SimpleName(this.ast);
244 postLazyInit(this.methodName, NAME_PROPERTY);
248 return this.methodName;
252 * Sets the name of the referenced method or constructor to the
255 * @param name the new method or constructor name node
256 * @exception IllegalArgumentException if:
258 * <li>the name is <code>null</code></li>
259 * <li>the node belongs to a different AST</li>
260 * <li>the node already has a parent</li>
263 public void setName(SimpleName name) {
265 throw new IllegalArgumentException();
267 ASTNode oldChild = this.methodName;
268 preReplaceChild(oldChild, name, NAME_PROPERTY);
269 this.methodName = name;
270 postReplaceChild(oldChild, name, NAME_PROPERTY);
274 * Returns the live ordered list of method parameter references for this
277 * @return the live list of method parameter references
278 * (element type: <code>MethodRefParameter</code>)
280 public List parameters() {
281 return this.parameters;
285 * Resolves and returns the binding for the entity referred to by
286 * this method reference.
288 * Note that bindings are generally unavailable unless requested when the
289 * AST is being built.
292 * @return the binding, or <code>null</code> if the binding cannot be
295 public final IBinding resolveBinding() {
296 return this.ast.getBindingResolver().resolveReference(this);
299 /* (omit javadoc for this method)
300 * Method declared on ASTNode.
303 return BASE_NODE_SIZE + 3 * 4;
306 /* (omit javadoc for this method)
307 * Method declared on ASTNode.
312 + (this.optionalQualifier == null ? 0 : getQualifier().treeSize())
313 + (this.methodName == null ? 0 : getName().treeSize())
314 + this.parameters.listSize();