1 /*******************************************************************************
2 * Copyright (c) 2000, 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 *******************************************************************************/
11 package net.sourceforge.phpdt.core.dom;
13 import java.util.ArrayList;
14 import java.util.List;
17 * Method invocation expression AST node type.
21 * [ Expression <b>.</b> ] Identifier
22 * <b>(</b> [ Expression { <b>,</b> Expression } ] <b>)</b>
24 * For JLS3, type arguments are added:
27 * [ Expression <b>.</b> ]
28 * [ <b><</b> Type { <b>,</b> Type } <b>></b> ]
29 * Identifier <b>(</b> [ Expression { <b>,</b> Expression } ] <b>)</b>
33 * @noinstantiate This class is not intended to be instantiated by clients.
35 public class MethodInvocation extends Expression {
38 * The "expression" structural property of this node type.
41 public static final ChildPropertyDescriptor EXPRESSION_PROPERTY =
42 new ChildPropertyDescriptor(MethodInvocation.class, "expression", Expression.class, OPTIONAL, CYCLE_RISK); //$NON-NLS-1$
45 * The "typeArguments" structural property of this node type (added in JLS3 API).
48 public static final ChildListPropertyDescriptor TYPE_ARGUMENTS_PROPERTY =
49 new ChildListPropertyDescriptor(MethodInvocation.class, "typeArguments", Type.class, NO_CYCLE_RISK); //$NON-NLS-1$
52 * The "name" structural property of this node type.
55 public static final ChildPropertyDescriptor NAME_PROPERTY =
56 new ChildPropertyDescriptor(MethodInvocation.class, "name", SimpleName.class, MANDATORY, NO_CYCLE_RISK); //$NON-NLS-1$
59 * The "arguments" structural property of this node type.
62 public static final ChildListPropertyDescriptor ARGUMENTS_PROPERTY =
63 new ChildListPropertyDescriptor(MethodInvocation.class, "arguments", Expression.class, 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(4);
83 createPropertyList(MethodInvocation.class, properyList);
84 addProperty(EXPRESSION_PROPERTY, properyList);
85 addProperty(NAME_PROPERTY, properyList);
86 addProperty(ARGUMENTS_PROPERTY, properyList);
87 PROPERTY_DESCRIPTORS_2_0 = reapPropertyList(properyList);
89 properyList = new ArrayList(5);
90 createPropertyList(MethodInvocation.class, properyList);
91 addProperty(EXPRESSION_PROPERTY, properyList);
92 addProperty(TYPE_ARGUMENTS_PROPERTY, properyList);
93 addProperty(NAME_PROPERTY, properyList);
94 addProperty(ARGUMENTS_PROPERTY, properyList);
95 PROPERTY_DESCRIPTORS_3_0 = reapPropertyList(properyList);
99 * Returns a list of structural property descriptors for this node type.
100 * Clients must not modify the result.
102 * @param apiLevel the API level; one of the
103 * <code>AST.JLS*</code> constants
105 * @return a list of property descriptors (element type:
106 * {@link StructuralPropertyDescriptor})
109 public static List propertyDescriptors(int apiLevel) {
110 if (apiLevel == AST.JLS2_INTERNAL) {
111 return PROPERTY_DESCRIPTORS_2_0;
113 return PROPERTY_DESCRIPTORS_3_0;
118 * The expression; <code>null</code> for none; defaults to none.
120 private Expression optionalExpression = null;
123 * The type arguments (element type: <code>Type</code>).
124 * Null in JLS2. Added in JLS3; defaults to an empty list
128 private ASTNode.NodeList typeArguments = null;
131 * The method name; lazily initialized; defaults to a unspecified,
132 * legal Java method name.
134 private SimpleName methodName = null;
137 * The list of argument expressions (element type:
138 * <code>Expression</code>). Defaults to an empty list.
140 private ASTNode.NodeList arguments =
141 new ASTNode.NodeList(ARGUMENTS_PROPERTY);
144 * Creates a new AST node for a method invocation expression owned by the
145 * given AST. By default, no expression, no type arguments,
146 * an unspecified, but legal, method name, and an empty list of arguments.
148 * @param ast the AST that is to own this node
150 MethodInvocation(AST ast) {
152 if (ast.apiLevel >= AST.JLS3) {
153 this.typeArguments = new ASTNode.NodeList(TYPE_ARGUMENTS_PROPERTY);
157 /* (omit javadoc for this method)
158 * Method declared on ASTNode.
160 final List internalStructuralPropertiesForType(int apiLevel) {
161 return propertyDescriptors(apiLevel);
164 /* (omit javadoc for this method)
165 * Method declared on ASTNode.
167 final ASTNode internalGetSetChildProperty(ChildPropertyDescriptor property, boolean get, ASTNode child) {
168 if (property == NAME_PROPERTY) {
172 setName((SimpleName) child);
176 if (property == EXPRESSION_PROPERTY) {
178 return getExpression();
180 setExpression((Expression) child);
184 // allow default implementation to flag the error
185 return super.internalGetSetChildProperty(property, get, child);
188 /* (omit javadoc for this method)
189 * Method declared on ASTNode.
191 final List internalGetChildListProperty(ChildListPropertyDescriptor property) {
192 if (property == ARGUMENTS_PROPERTY) {
195 if (property == TYPE_ARGUMENTS_PROPERTY) {
196 return typeArguments();
198 // allow default implementation to flag the error
199 return super.internalGetChildListProperty(property);
202 /* (omit javadoc for this method)
203 * Method declared on ASTNode.
205 final int getNodeType0() {
206 return METHOD_INVOCATION;
209 /* (omit javadoc for this method)
210 * Method declared on ASTNode.
212 ASTNode clone0(AST target) {
213 MethodInvocation result = new MethodInvocation(target);
214 result.setSourceRange(this.getStartPosition(), this.getLength());
215 result.setName((SimpleName) getName().clone(target));
216 result.setExpression(
217 (Expression) ASTNode.copySubtree(target, getExpression()));
218 if (this.ast.apiLevel >= AST.JLS3) {
219 result.typeArguments().addAll(ASTNode.copySubtrees(target, typeArguments()));
221 result.arguments().addAll(ASTNode.copySubtrees(target, arguments()));
225 /* (omit javadoc for this method)
226 * Method declared on ASTNode.
228 final boolean subtreeMatch0(ASTMatcher matcher, Object other) {
229 // dispatch to correct overloaded match method
230 return matcher.match(this, other);
233 /* (omit javadoc for this method)
234 * Method declared on ASTNode.
236 void accept0(ASTVisitor visitor) {
237 boolean visitChildren = visitor.visit(this);
239 // visit children in normal left to right reading order
240 acceptChild(visitor, getExpression());
241 if (this.ast.apiLevel >= AST.JLS3) {
242 acceptChildren(visitor, this.typeArguments);
244 acceptChild(visitor, getName());
245 acceptChildren(visitor, this.arguments);
247 visitor.endVisit(this);
251 * Returns the expression of this method invocation expression, or
252 * <code>null</code> if there is none.
254 * @return the expression node, or <code>null</code> if there is none
256 public Expression getExpression() {
257 return this.optionalExpression;
261 * Returns <code>true</code> if the resolved return type has been inferred
262 * from the assignment context (JLS3 15.12.2.8), <code>false</code> otherwise.
264 * This information is available only when bindings are requested when the AST is being built
267 * @return <code>true</code> if the resolved return type has been inferred
268 * from the assignment context (JLS3 15.12.2.8), <code>false</code> otherwise
271 public boolean isResolvedTypeInferredFromExpectedType() {
272 return this.ast.getBindingResolver().isResolvedTypeInferredFromExpectedType(this);
276 * Sets or clears the expression of this method invocation expression.
278 * @param expression the expression node, or <code>null</code> if
280 * @exception IllegalArgumentException if:
282 * <li>the node belongs to a different AST</li>
283 * <li>the node already has a parent</li>
284 * <li>a cycle in would be created</li>
287 public void setExpression(Expression expression) {
288 ASTNode oldChild = this.optionalExpression;
289 preReplaceChild(oldChild, expression, EXPRESSION_PROPERTY);
290 this.optionalExpression = expression;
291 postReplaceChild(oldChild, expression, EXPRESSION_PROPERTY);
295 * Returns the live ordered list of type arguments of this method
296 * invocation (added in JLS3 API).
298 * @return the live list of type arguments
299 * (element type: <code>Type</code>)
300 * @exception UnsupportedOperationException if this operation is used in
304 public List typeArguments() {
305 // more efficient than just calling unsupportedIn2() to check
306 if (this.typeArguments == null) {
309 return this.typeArguments;
313 * Returns the name of the method invoked in this expression.
315 * @return the method name node
317 public SimpleName getName() {
318 if (this.methodName == null) {
319 // lazy init must be thread-safe for readers
320 synchronized (this) {
321 if (this.methodName == null) {
323 this.methodName = new SimpleName(this.ast);
324 postLazyInit(this.methodName, NAME_PROPERTY);
328 return this.methodName;
332 * Sets the name of the method invoked in this expression to the
335 * @param name the new method name
336 * @exception IllegalArgumentException if:
338 * <li>the node belongs to a different AST</li>
339 * <li>the node already has a parent</li>
342 public void setName(SimpleName name) {
344 throw new IllegalArgumentException();
346 ASTNode oldChild = this.methodName;
347 preReplaceChild(oldChild, name, NAME_PROPERTY);
348 this.methodName = name;
349 postReplaceChild(oldChild, name, NAME_PROPERTY);
353 * Returns the live ordered list of argument expressions in this method
354 * invocation expression.
356 * @return the live list of argument expressions
357 * (element type: <code>Expression</code>)
359 public List arguments() {
360 return this.arguments;
364 * Resolves and returns the binding for the method invoked by this
367 * Note that bindings are generally unavailable unless requested when the
368 * AST is being built.
371 * @return the method binding, or <code>null</code> if the binding cannot
375 public IMethodBinding resolveMethodBinding() {
376 return this.ast.getBindingResolver().resolveMethod(this);
379 /* (omit javadoc for this method)
380 * Method declared on ASTNode.
383 // treat Code as free
384 return BASE_NODE_SIZE + 4 * 4;
387 /* (omit javadoc for this method)
388 * Method declared on ASTNode.
393 + (this.optionalExpression == null ? 0 : getExpression().treeSize())
394 + (this.typeArguments == null ? 0 : this.typeArguments.listSize())
395 + (this.methodName == null ? 0 : getName().treeSize())
396 + (this.arguments == null ? 0 : this.arguments.listSize());