KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2004, 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
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 parameter within a method reference ({@link MethodRef}).
19  * These nodes only occur within doc comments ({@link Javadoc}).
20  * For JLS2:
21  * <pre>
22  * MethodRefParameter:
23  * Type [ Identifier ]
24  * </pre>
25  * For JLS3, the variable arity indicator was added:
26  * <pre>
27  * MethodRefParameter:
28  * Type [ <b>...</b> ] [ Identifier ]
29  * </pre>
30  * <p>
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.
36  * </p>
37  *
38  * @see Javadoc
39  * @since 3.0
40  */

41 public class MethodRefParameter extends ASTNode {
42     
43     /**
44      * The "type" structural property of this node type.
45      * @since 3.0
46      */

47     public static final ChildPropertyDescriptor TYPE_PROPERTY =
48         new ChildPropertyDescriptor(MethodRefParameter.class, "type", Type.class, MANDATORY, NO_CYCLE_RISK); //$NON-NLS-1$
49

50     /**
51      * The "varargs" structural property of this node type (added in JLS3 API).
52      * @since 3.1
53      */

54     public static final SimplePropertyDescriptor VARARGS_PROPERTY =
55         new SimplePropertyDescriptor(MethodRefParameter.class, "varargs", boolean.class, MANDATORY); //$NON-NLS-1$
56

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

61     public static final ChildPropertyDescriptor NAME_PROPERTY =
62         new ChildPropertyDescriptor(MethodRefParameter.class, "name", SimpleName.class, OPTIONAL, NO_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(3);
82         createPropertyList(MethodRefParameter.class, properyList);
83         addProperty(TYPE_PROPERTY, properyList);
84         addProperty(NAME_PROPERTY, properyList);
85         PROPERTY_DESCRIPTORS_2_0 = reapPropertyList(properyList);
86         
87         properyList = new ArrayList JavaDoc(3);
88         createPropertyList(MethodRefParameter.class, properyList);
89         addProperty(TYPE_PROPERTY, properyList);
90         addProperty(VARARGS_PROPERTY, properyList);
91         addProperty(NAME_PROPERTY, properyList);
92         PROPERTY_DESCRIPTORS_3_0 = reapPropertyList(properyList);
93     }
94
95     /**
96      * Returns a list of structural property descriptors for this node type.
97      * Clients must not modify the result.
98      *
99      * @param apiLevel the API level; one of the AST.JLS* constants
100      * @return a list of property descriptors (element type:
101      * {@link StructuralPropertyDescriptor})
102      * @since 3.0
103      */

104     public static List JavaDoc propertyDescriptors(int apiLevel) {
105         if (apiLevel == AST.JLS2_INTERNAL) {
106             return PROPERTY_DESCRIPTORS_2_0;
107         } else {
108             return PROPERTY_DESCRIPTORS_3_0;
109         }
110     }
111             
112     /**
113      * The type; lazily initialized; defaults to a unspecified,
114      * legal type.
115      */

116     private Type type = null;
117
118     /**
119      * Indicates the last parameter of a variable arity method;
120      * defaults to false.
121      *
122      * @since 3.1
123      */

124     private boolean variableArity = false;
125
126     /**
127      * The parameter name, or <code>null</code> if none; none by
128      * default.
129      */

130     private SimpleName optionalParameterName = null;
131
132     /**
133      * Creates a new AST node for a method referenece parameter owned by the given
134      * AST. By default, the node has an unspecified (but legal) type,
135      * not variable arity, and no parameter name.
136      * <p>
137      * N.B. This constructor is package-private.
138      * </p>
139      *
140      * @param ast the AST that is to own this node
141      */

142     MethodRefParameter(AST ast) {
143         super(ast);
144     }
145
146     /* (omit javadoc for this method)
147      * Method declared on ASTNode.
148      */

149     final List JavaDoc internalStructuralPropertiesForType(int apiLevel) {
150         return propertyDescriptors(apiLevel);
151     }
152     
153     /* (omit javadoc for this method)
154      * Method declared on ASTNode.
155      */

156     final ASTNode internalGetSetChildProperty(ChildPropertyDescriptor property, boolean get, ASTNode child) {
157         if (property == TYPE_PROPERTY) {
158             if (get) {
159                 return getType();
160             } else {
161                 setType((Type) child);
162                 return null;
163             }
164         }
165         if (property == NAME_PROPERTY) {
166             if (get) {
167                 return getName();
168             } else {
169                 setName((SimpleName) child);
170                 return null;
171             }
172         }
173         // allow default implementation to flag the error
174
return super.internalGetSetChildProperty(property, get, child);
175     }
176     
177     /* (omit javadoc for this method)
178      * Method declared on ASTNode.
179      */

180     final boolean internalGetSetBooleanProperty(SimplePropertyDescriptor property, boolean get, boolean value) {
181         if (property == VARARGS_PROPERTY) {
182             if (get) {
183                 return isVarargs();
184             } else {
185                 setVarargs(value);
186                 return false;
187             }
188         }
189         // allow default implementation to flag the error
190
return super.internalGetSetBooleanProperty(property, get, value);
191     }
192     
193     /* (omit javadoc for this method)
194      * Method declared on ASTNode.
195      */

196     final int getNodeType0() {
197         return METHOD_REF_PARAMETER;
198     }
199
200     /* (omit javadoc for this method)
201      * Method declared on ASTNode.
202      */

203     ASTNode clone0(AST target) {
204         MethodRefParameter result = new MethodRefParameter(target);
205         result.setSourceRange(this.getStartPosition(), this.getLength());
206         result.setType((Type) ASTNode.copySubtree(target, getType()));
207         if (this.ast.apiLevel >= AST.JLS3) {
208             result.setVarargs(isVarargs());
209         }
210         result.setName((SimpleName) ASTNode.copySubtree(target, getName()));
211         return result;
212     }
213
214     /* (omit javadoc for this method)
215      * Method declared on ASTNode.
216      */

217     final boolean subtreeMatch0(ASTMatcher matcher, Object JavaDoc other) {
218         // dispatch to correct overloaded match method
219
return matcher.match(this, other);
220     }
221     
222     /* (omit javadoc for this method)
223      * Method declared on ASTNode.
224      */

225     void accept0(ASTVisitor visitor) {
226         boolean visitChildren = visitor.visit(this);
227         if (visitChildren) {
228             // visit children in normal left to right reading order
229
acceptChild(visitor, getType());
230             acceptChild(visitor, getName());
231         }
232         visitor.endVisit(this);
233     }
234     
235     /**
236      * Returns the paramter type.
237      *
238      * @return the parameter type
239      */

240     public Type getType() {
241         if (this.type == null) {
242             // lazy init must be thread-safe for readers
243
synchronized (this) {
244                 if (this.type == null) {
245                     preLazyInit();
246                     this.type = this.ast.newPrimitiveType(PrimitiveType.INT);
247                     postLazyInit(this.type, TYPE_PROPERTY);
248                 }
249             }
250         }
251         return this.type;
252     }
253
254     /**
255      * Sets the paramter type to the given type.
256      *
257      * @param type the new type
258      * @exception IllegalArgumentException if:
259      * <ul>
260      * <li>the type is <code>null</code></li>
261      * <li>the node belongs to a different AST</li>
262      * <li>the node already has a parent</li>
263      * </ul>
264      */

265     public void setType(Type type) {
266         if (type == null) {
267             throw new IllegalArgumentException JavaDoc();
268         }
269         ASTNode oldChild = this.type;
270         preReplaceChild(oldChild, type, TYPE_PROPERTY);
271         this.type = type;
272         postReplaceChild(oldChild, type, TYPE_PROPERTY);
273     }
274
275     /**
276      * Returns whether this method reference parameter is for
277      * the last parameter of a variable arity method (added in JLS3 API).
278      * <p>
279      * Note that the binding for the type <code>Foo</code>in the vararg method
280      * reference <code>#fun(Foo...)</code> is always for the type as
281      * written; i.e., the type binding for <code>Foo</code>. However, if you
282      * navigate from the MethodRef to its method binding to the
283      * type binding for its last parameter, the type binding for the vararg
284      * parameter is always an array type (i.e., <code>Foo[]</code>) reflecting
285      * the way vararg methods get compiled.
286      * </p>
287      *
288      * @return <code>true</code> if this is a variable arity parameter,
289      * and <code>false</code> otherwise
290      * @exception UnsupportedOperationException if this operation is used in
291      * a JLS2 AST
292      * @since 3.1
293      */

294     public boolean isVarargs() {
295         unsupportedIn2();
296         return this.variableArity;
297     }
298
299     /**
300      * Sets whether this method reference parameter is for the last parameter of
301      * a variable arity method (added in JLS3 API).
302      *
303      * @param variableArity <code>true</code> if this is a variable arity
304      * parameter, and <code>false</code> otherwise
305      * @since 3.1
306      */

307     public void setVarargs(boolean variableArity) {
308         unsupportedIn2();
309         preValueChange(VARARGS_PROPERTY);
310         this.variableArity = variableArity;
311         postValueChange(VARARGS_PROPERTY);
312     }
313
314     /**
315      * Returns the parameter name, or <code>null</code> if there is none.
316      *
317      * @return the parameter name node, or <code>null</code> if there is none
318      */

319     public SimpleName getName() {
320         return this.optionalParameterName;
321     }
322     
323     /**
324      * Sets or clears the parameter name.
325      *
326      * @param name the parameter name node, or <code>null</code> if
327      * there is none
328      * @exception IllegalArgumentException if:
329      * <ul>
330      * <li>the node belongs to a different AST</li>
331      * <li>the node already has a parent</li>
332      * </ul>
333      */

334     public void setName(SimpleName name) {
335         ASTNode oldChild = this.optionalParameterName;
336         preReplaceChild(oldChild, name, NAME_PROPERTY);
337         this.optionalParameterName = name;
338         postReplaceChild(oldChild, name, NAME_PROPERTY);
339     }
340
341     /* (omit javadoc for this method)
342      * Method declared on ASTNode.
343      */

344     int memSize() {
345         return BASE_NODE_SIZE + 2 * 5;
346     }
347     
348     /* (omit javadoc for this method)
349      * Method declared on ASTNode.
350      */

351     int treeSize() {
352         return
353             memSize()
354             + (this.type == null ? 0 : getType().treeSize())
355             + (this.optionalParameterName == null ? 0 : getName().treeSize());
356     }
357 }
358
Popular Tags