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 parameter within a method reference ({@link MethodRef}).
19 * These nodes only occur within doc comments ({@link Javadoc}).
25 * For JLS3, the variable arity indicator was added:
28 * Type [ <b>...</b> ] [ Identifier ]
31 * Note: The 1.5 spec for the Javadoc tool does not mention the possibility
32 * of a variable arity indicator in method references. However, the 1.5
33 * Javadoc tool itself does indeed support it. Since it makes sense to have
34 * a way to explicitly refer to variable arity methods, it seems more likely
35 * that the Javadoc spec is wrong in this case.
40 * @noinstantiate This class is not intended to be instantiated by clients.
42 public class MethodRefParameter extends ASTNode {
45 * The "type" structural property of this node type.
48 public static final ChildPropertyDescriptor TYPE_PROPERTY =
49 new ChildPropertyDescriptor(MethodRefParameter.class, "type", Type.class, MANDATORY, NO_CYCLE_RISK); //$NON-NLS-1$
52 * The "varargs" structural property of this node type (added in JLS3 API).
55 public static final SimplePropertyDescriptor VARARGS_PROPERTY =
56 new SimplePropertyDescriptor(MethodRefParameter.class, "varargs", boolean.class, MANDATORY); //$NON-NLS-1$
59 * The "name" structural property of this node type.
62 public static final ChildPropertyDescriptor NAME_PROPERTY =
63 new ChildPropertyDescriptor(MethodRefParameter.class, "name", SimpleName.class, OPTIONAL, NO_CYCLE_RISK); //$NON-NLS-1$
66 * A list of property descriptors (element type:
67 * {@link StructuralPropertyDescriptor}),
68 * or null if uninitialized.
71 private static final List PROPERTY_DESCRIPTORS_2_0;
74 * A list of property descriptors (element type:
75 * {@link StructuralPropertyDescriptor}),
76 * or null if uninitialized.
79 private static final List PROPERTY_DESCRIPTORS_3_0;
82 List properyList = new ArrayList(3);
83 createPropertyList(MethodRefParameter.class, properyList);
84 addProperty(TYPE_PROPERTY, properyList);
85 addProperty(NAME_PROPERTY, properyList);
86 PROPERTY_DESCRIPTORS_2_0 = reapPropertyList(properyList);
88 properyList = new ArrayList(3);
89 createPropertyList(MethodRefParameter.class, properyList);
90 addProperty(TYPE_PROPERTY, properyList);
91 addProperty(VARARGS_PROPERTY, properyList);
92 addProperty(NAME_PROPERTY, properyList);
93 PROPERTY_DESCRIPTORS_3_0 = reapPropertyList(properyList);
97 * Returns a list of structural property descriptors for this node type.
98 * Clients must not modify the result.
100 * @param apiLevel the API level; one of the AST.JLS* constants
101 * @return a list of property descriptors (element type:
102 * {@link StructuralPropertyDescriptor})
105 public static List propertyDescriptors(int apiLevel) {
106 if (apiLevel == AST.JLS2_INTERNAL) {
107 return PROPERTY_DESCRIPTORS_2_0;
109 return PROPERTY_DESCRIPTORS_3_0;
114 * The type; lazily initialized; defaults to a unspecified,
117 private Type type = null;
120 * Indicates the last parameter of a variable arity method;
125 private boolean variableArity = false;
128 * The parameter name, or <code>null</code> if none; none by
131 private SimpleName optionalParameterName = null;
134 * Creates a new AST node for a method referenece parameter owned by the given
135 * AST. By default, the node has an unspecified (but legal) type,
136 * not variable arity, and no parameter name.
138 * N.B. This constructor is package-private.
141 * @param ast the AST that is to own this node
143 MethodRefParameter(AST ast) {
147 /* (omit javadoc for this method)
148 * Method declared on ASTNode.
150 final List internalStructuralPropertiesForType(int apiLevel) {
151 return propertyDescriptors(apiLevel);
154 /* (omit javadoc for this method)
155 * Method declared on ASTNode.
157 final ASTNode internalGetSetChildProperty(ChildPropertyDescriptor property, boolean get, ASTNode child) {
158 if (property == TYPE_PROPERTY) {
162 setType((Type) child);
166 if (property == NAME_PROPERTY) {
170 setName((SimpleName) child);
174 // allow default implementation to flag the error
175 return super.internalGetSetChildProperty(property, get, child);
178 /* (omit javadoc for this method)
179 * Method declared on ASTNode.
181 final boolean internalGetSetBooleanProperty(SimplePropertyDescriptor property, boolean get, boolean value) {
182 if (property == VARARGS_PROPERTY) {
190 // allow default implementation to flag the error
191 return super.internalGetSetBooleanProperty(property, get, value);
194 /* (omit javadoc for this method)
195 * Method declared on ASTNode.
197 final int getNodeType0() {
198 return METHOD_REF_PARAMETER;
201 /* (omit javadoc for this method)
202 * Method declared on ASTNode.
204 ASTNode clone0(AST target) {
205 MethodRefParameter result = new MethodRefParameter(target);
206 result.setSourceRange(this.getStartPosition(), this.getLength());
207 result.setType((Type) ASTNode.copySubtree(target, getType()));
208 if (this.ast.apiLevel >= AST.JLS3) {
209 result.setVarargs(isVarargs());
211 result.setName((SimpleName) ASTNode.copySubtree(target, getName()));
215 /* (omit javadoc for this method)
216 * Method declared on ASTNode.
218 final boolean subtreeMatch0(ASTMatcher matcher, Object other) {
219 // dispatch to correct overloaded match method
220 return matcher.match(this, other);
223 /* (omit javadoc for this method)
224 * Method declared on ASTNode.
226 void accept0(ASTVisitor visitor) {
227 boolean visitChildren = visitor.visit(this);
229 // visit children in normal left to right reading order
230 acceptChild(visitor, getType());
231 acceptChild(visitor, getName());
233 visitor.endVisit(this);
237 * Returns the paramter type.
239 * @return the parameter type
241 public Type getType() {
242 if (this.type == null) {
243 // lazy init must be thread-safe for readers
244 synchronized (this) {
245 if (this.type == null) {
247 this.type = this.ast.newPrimitiveType(PrimitiveType.INT);
248 postLazyInit(this.type, TYPE_PROPERTY);
256 * Sets the paramter type to the given type.
258 * @param type the new type
259 * @exception IllegalArgumentException if:
261 * <li>the type is <code>null</code></li>
262 * <li>the node belongs to a different AST</li>
263 * <li>the node already has a parent</li>
266 public void setType(Type type) {
268 throw new IllegalArgumentException();
270 ASTNode oldChild = this.type;
271 preReplaceChild(oldChild, type, TYPE_PROPERTY);
273 postReplaceChild(oldChild, type, TYPE_PROPERTY);
277 * Returns whether this method reference parameter is for
278 * the last parameter of a variable arity method (added in JLS3 API).
280 * Note that the binding for the type <code>Foo</code>in the vararg method
281 * reference <code>#fun(Foo...)</code> is always for the type as
282 * written; i.e., the type binding for <code>Foo</code>. However, if you
283 * navigate from the MethodRef to its method binding to the
284 * type binding for its last parameter, the type binding for the vararg
285 * parameter is always an array type (i.e., <code>Foo[]</code>) reflecting
286 * the way vararg methods get compiled.
289 * @return <code>true</code> if this is a variable arity parameter,
290 * and <code>false</code> otherwise
291 * @exception UnsupportedOperationException if this operation is used in
295 public boolean isVarargs() {
297 return this.variableArity;
301 * Sets whether this method reference parameter is for the last parameter of
302 * a variable arity method (added in JLS3 API).
304 * @param variableArity <code>true</code> if this is a variable arity
305 * parameter, and <code>false</code> otherwise
308 public void setVarargs(boolean variableArity) {
310 preValueChange(VARARGS_PROPERTY);
311 this.variableArity = variableArity;
312 postValueChange(VARARGS_PROPERTY);
316 * Returns the parameter name, or <code>null</code> if there is none.
318 * @return the parameter name node, or <code>null</code> if there is none
320 public SimpleName getName() {
321 return this.optionalParameterName;
325 * Sets or clears the parameter name.
327 * @param name the parameter name node, or <code>null</code> if
329 * @exception IllegalArgumentException if:
331 * <li>the node belongs to a different AST</li>
332 * <li>the node already has a parent</li>
335 public void setName(SimpleName name) {
336 ASTNode oldChild = this.optionalParameterName;
337 preReplaceChild(oldChild, name, NAME_PROPERTY);
338 this.optionalParameterName = name;
339 postReplaceChild(oldChild, name, NAME_PROPERTY);
342 /* (omit javadoc for this method)
343 * Method declared on ASTNode.
346 return BASE_NODE_SIZE + 2 * 5;
349 /* (omit javadoc for this method)
350 * Method declared on ASTNode.
355 + (this.type == null ? 0 : getType().treeSize())
356 + (this.optionalParameterName == null ? 0 : getName().treeSize());