KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 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 package org.eclipse.jdt.core.dom;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.List JavaDoc;
15
16 /**
17  * Method invocation expression AST node type.
18  * For JLS2:
19  * <pre>
20  * MethodInvocation:
21  * [ Expression <b>.</b> ] Identifier
22  * <b>(</b> [ Expression { <b>,</b> Expression } ] <b>)</b>
23  * </pre>
24  * For JLS3, type arguments are added:
25  * <pre>
26  * MethodInvocation:
27  * [ Expression <b>.</b> ]
28  * [ <b>&lt;</b> Type { <b>,</b> Type } <b>&gt;</b> ]
29  * Identifier <b>(</b> [ Expression { <b>,</b> Expression } ] <b>)</b>
30  * </pre>
31  *
32  * @since 2.0
33  */

34 public class MethodInvocation extends Expression {
35     
36     /**
37      * The "expression" structural property of this node type.
38      * @since 3.0
39      */

40     public static final ChildPropertyDescriptor EXPRESSION_PROPERTY =
41         new ChildPropertyDescriptor(MethodInvocation.class, "expression", Expression.class, OPTIONAL, CYCLE_RISK); //$NON-NLS-1$
42

43     /**
44      * The "typeArguments" structural property of this node type (added in JLS3 API).
45      * @since 3.1
46      */

47     public static final ChildListPropertyDescriptor TYPE_ARGUMENTS_PROPERTY =
48         new ChildListPropertyDescriptor(MethodInvocation.class, "typeArguments", Type.class, NO_CYCLE_RISK); //$NON-NLS-1$
49

50     /**
51      * The "name" structural property of this node type.
52      * @since 3.0
53      */

54     public static final ChildPropertyDescriptor NAME_PROPERTY =
55         new ChildPropertyDescriptor(MethodInvocation.class, "name", SimpleName.class, MANDATORY, NO_CYCLE_RISK); //$NON-NLS-1$
56

57     /**
58      * The "arguments" structural property of this node type.
59      * @since 3.0
60      */

61     public static final ChildListPropertyDescriptor ARGUMENTS_PROPERTY =
62         new ChildListPropertyDescriptor(MethodInvocation.class, "arguments", Expression.class, CYCLE_RISK); //$NON-NLS-1$
63

64     /**
65      * A list of property descriptors (element type:
66      * {@link StructuralPropertyDescriptor}),
67      * or null if uninitialized.
68      * @since 3.0
69      */

70     private static final List JavaDoc PROPERTY_DESCRIPTORS_2_0;
71     
72     /**
73      * A list of property descriptors (element type:
74      * {@link StructuralPropertyDescriptor}),
75      * or null if uninitialized.
76      * @since 3.1
77      */

78     private static final List JavaDoc PROPERTY_DESCRIPTORS_3_0;
79     
80     static {
81         List JavaDoc properyList = new ArrayList JavaDoc(4);
82         createPropertyList(MethodInvocation.class, properyList);
83         addProperty(EXPRESSION_PROPERTY, properyList);
84         addProperty(NAME_PROPERTY, properyList);
85         addProperty(ARGUMENTS_PROPERTY, properyList);
86         PROPERTY_DESCRIPTORS_2_0 = reapPropertyList(properyList);
87         
88         properyList = new ArrayList JavaDoc(5);
89         createPropertyList(MethodInvocation.class, properyList);
90         addProperty(EXPRESSION_PROPERTY, properyList);
91         addProperty(TYPE_ARGUMENTS_PROPERTY, properyList);
92         addProperty(NAME_PROPERTY, properyList);
93         addProperty(ARGUMENTS_PROPERTY, properyList);
94         PROPERTY_DESCRIPTORS_3_0 = reapPropertyList(properyList);
95     }
96
97     /**
98      * Returns a list of structural property descriptors for this node type.
99      * Clients must not modify the result.
100      *
101      * @param apiLevel the API level; one of the
102      * <code>AST.JLS&ast;</code> constants
103
104      * @return a list of property descriptors (element type:
105      * {@link StructuralPropertyDescriptor})
106      * @since 3.0
107      */

108     public static List JavaDoc propertyDescriptors(int apiLevel) {
109         if (apiLevel == AST.JLS2_INTERNAL) {
110             return PROPERTY_DESCRIPTORS_2_0;
111         } else {
112             return PROPERTY_DESCRIPTORS_3_0;
113         }
114     }
115             
116     /**
117      * The expression; <code>null</code> for none; defaults to none.
118      */

119     private Expression optionalExpression = null;
120     
121     /**
122      * The type arguments (element type: <code>Type</code>).
123      * Null in JLS2. Added in JLS3; defaults to an empty list
124      * (see constructor).
125      * @since 3.1
126      */

127     private ASTNode.NodeList typeArguments = null;
128
129     /**
130      * The method name; lazily initialized; defaults to a unspecified,
131      * legal Java method name.
132      */

133     private SimpleName methodName = null;
134     
135     /**
136      * The list of argument expressions (element type:
137      * <code>Expression</code>). Defaults to an empty list.
138      */

139     private ASTNode.NodeList arguments =
140         new ASTNode.NodeList(ARGUMENTS_PROPERTY);
141
142     /**
143      * Creates a new AST node for a method invocation expression owned by the
144      * given AST. By default, no expression, no type arguments,
145      * an unspecified, but legal, method name, and an empty list of arguments.
146      *
147      * @param ast the AST that is to own this node
148      */

149     MethodInvocation(AST ast) {
150         super(ast);
151         if (ast.apiLevel >= AST.JLS3) {
152             this.typeArguments = new ASTNode.NodeList(TYPE_ARGUMENTS_PROPERTY);
153         }
154     }
155
156     /* (omit javadoc for this method)
157      * Method declared on ASTNode.
158      */

159     final List JavaDoc internalStructuralPropertiesForType(int apiLevel) {
160         return propertyDescriptors(apiLevel);
161     }
162     
163     /* (omit javadoc for this method)
164      * Method declared on ASTNode.
165      */

166     final ASTNode internalGetSetChildProperty(ChildPropertyDescriptor property, boolean get, ASTNode child) {
167         if (property == NAME_PROPERTY) {
168             if (get) {
169                 return getName();
170             } else {
171                 setName((SimpleName) child);
172                 return null;
173             }
174         }
175         if (property == EXPRESSION_PROPERTY) {
176             if (get) {
177                 return getExpression();
178             } else {
179                 setExpression((Expression) child);
180                 return null;
181             }
182         }
183         // allow default implementation to flag the error
184
return super.internalGetSetChildProperty(property, get, child);
185     }
186     
187     /* (omit javadoc for this method)
188      * Method declared on ASTNode.
189      */

190     final List JavaDoc internalGetChildListProperty(ChildListPropertyDescriptor property) {
191         if (property == ARGUMENTS_PROPERTY) {
192             return arguments();
193         }
194         if (property == TYPE_ARGUMENTS_PROPERTY) {
195             return typeArguments();
196         }
197         // allow default implementation to flag the error
198
return super.internalGetChildListProperty(property);
199     }
200
201     /* (omit javadoc for this method)
202      * Method declared on ASTNode.
203      */

204     final int getNodeType0() {
205         return METHOD_INVOCATION;
206     }
207
208     /* (omit javadoc for this method)
209      * Method declared on ASTNode.
210      */

211     ASTNode clone0(AST target) {
212         MethodInvocation result = new MethodInvocation(target);
213         result.setSourceRange(this.getStartPosition(), this.getLength());
214         result.setName((SimpleName) getName().clone(target));
215         result.setExpression(
216             (Expression) ASTNode.copySubtree(target, getExpression()));
217         if (this.ast.apiLevel >= AST.JLS3) {
218             result.typeArguments().addAll(ASTNode.copySubtrees(target, typeArguments()));
219         }
220         result.arguments().addAll(ASTNode.copySubtrees(target, arguments()));
221         return result;
222     }
223
224     /* (omit javadoc for this method)
225      * Method declared on ASTNode.
226      */

227     final boolean subtreeMatch0(ASTMatcher matcher, Object JavaDoc other) {
228         // dispatch to correct overloaded match method
229
return matcher.match(this, other);
230     }
231
232     /* (omit javadoc for this method)
233      * Method declared on ASTNode.
234      */

235     void accept0(ASTVisitor visitor) {
236         boolean visitChildren = visitor.visit(this);
237         if (visitChildren) {
238             // visit children in normal left to right reading order
239
acceptChild(visitor, getExpression());
240             if (this.ast.apiLevel >= AST.JLS3) {
241                 acceptChildren(visitor, this.typeArguments);
242             }
243             acceptChild(visitor, getName());
244             acceptChildren(visitor, this.arguments);
245         }
246         visitor.endVisit(this);
247     }
248     
249     /**
250      * Returns the expression of this method invocation expression, or
251      * <code>null</code> if there is none.
252      *
253      * @return the expression node, or <code>null</code> if there is none
254      */

255     public Expression getExpression() {
256         return this.optionalExpression;
257     }
258     
259     /**
260      * Sets or clears the expression of this method invocation expression.
261      *
262      * @param expression the expression node, or <code>null</code> if
263      * there is none
264      * @exception IllegalArgumentException if:
265      * <ul>
266      * <li>the node belongs to a different AST</li>
267      * <li>the node already has a parent</li>
268      * <li>a cycle in would be created</li>
269      * </ul>
270      */

271     public void setExpression(Expression expression) {
272         ASTNode oldChild = this.optionalExpression;
273         preReplaceChild(oldChild, expression, EXPRESSION_PROPERTY);
274         this.optionalExpression = expression;
275         postReplaceChild(oldChild, expression, EXPRESSION_PROPERTY);
276     }
277
278     /**
279      * Returns the live ordered list of type arguments of this method
280      * invocation (added in JLS3 API).
281      *
282      * @return the live list of type arguments
283      * (element type: <code>Type</code>)
284      * @exception UnsupportedOperationException if this operation is used in
285      * a JLS2 AST
286      * @since 3.1
287      */

288     public List JavaDoc typeArguments() {
289         // more efficient than just calling unsupportedIn2() to check
290
if (this.typeArguments == null) {
291             unsupportedIn2();
292         }
293         return this.typeArguments;
294     }
295     
296     /**
297      * Returns the name of the method invoked in this expression.
298      *
299      * @return the method name node
300      */

301     public SimpleName getName() {
302         if (this.methodName == null) {
303             // lazy init must be thread-safe for readers
304
synchronized (this) {
305                 if (this.methodName == null) {
306                     preLazyInit();
307                     this.methodName = new SimpleName(this.ast);
308                     postLazyInit(this.methodName, NAME_PROPERTY);
309                 }
310             }
311         }
312         return this.methodName;
313     }
314     
315     /**
316      * Sets the name of the method invoked in this expression to the
317      * given name.
318      *
319      * @param name the new method name
320      * @exception IllegalArgumentException if:
321      * <ul>
322      * <li>the node belongs to a different AST</li>
323      * <li>the node already has a parent</li>
324      * </ul>
325      */

326     public void setName(SimpleName name) {
327         if (name == null) {
328             throw new IllegalArgumentException JavaDoc();
329         }
330         ASTNode oldChild = this.methodName;
331         preReplaceChild(oldChild, name, NAME_PROPERTY);
332         this.methodName = name;
333         postReplaceChild(oldChild, name, NAME_PROPERTY);
334     }
335
336     /**
337      * Returns the live ordered list of argument expressions in this method
338      * invocation expression.
339      *
340      * @return the live list of argument expressions
341      * (element type: <code>Expression</code>)
342      */

343     public List JavaDoc arguments() {
344         return this.arguments;
345     }
346
347     /**
348      * Resolves and returns the binding for the method invoked by this
349      * expression.
350      * <p>
351      * Note that bindings are generally unavailable unless requested when the
352      * AST is being built.
353      * </p>
354      *
355      * @return the method binding, or <code>null</code> if the binding cannot
356      * be resolved
357      * @since 2.1
358      */

359     public IMethodBinding resolveMethodBinding() {
360         return this.ast.getBindingResolver().resolveMethod(this);
361     }
362
363     /* (omit javadoc for this method)
364      * Method declared on ASTNode.
365      */

366     int memSize() {
367         // treat Code as free
368
return BASE_NODE_SIZE + 4 * 4;
369     }
370     
371     /* (omit javadoc for this method)
372      * Method declared on ASTNode.
373      */

374     int treeSize() {
375         return
376             memSize()
377             + (this.optionalExpression == null ? 0 : getExpression().treeSize())
378             + (this.typeArguments == null ? 0 : this.typeArguments.listSize())
379             + (this.methodName == null ? 0 : getName().treeSize())
380             + (this.arguments == null ? 0 : this.arguments.listSize());
381     }
382 }
383
384
Popular Tags