author | tbell |
Mon, 04 May 2009 21:10:41 -0700 | |
changeset 2719 | 99d59312294b |
parent 2678 | 57cf2a1c1a05 |
child 3530 | 18fb7507984b |
permissions | -rw-r--r-- |
8 | 1 |
/* |
2678
57cf2a1c1a05
6831313: update jaxws in OpenJDK7 to 2.1 plus bug fixes from OpenJDK 6
tbell
parents:
8
diff
changeset
|
2 |
* Copyright 2005-2006 Sun Microsystems, Inc. All Rights Reserved. |
8 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
7 |
* published by the Free Software Foundation. Sun designates this |
|
8 |
* particular file as subject to the "Classpath" exception as provided |
|
9 |
* by Sun in the LICENSE file that accompanied this code. |
|
10 |
* |
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
21 |
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
|
22 |
* CA 95054 USA or visit www.sun.com if you need additional information or |
|
23 |
* have any questions. |
|
24 |
*/ |
|
25 |
||
26 |
package com.sun.codemodel.internal; |
|
27 |
||
28 |
import java.lang.annotation.Annotation; |
|
29 |
import java.util.ArrayList; |
|
30 |
import java.util.List; |
|
31 |
import java.util.Set; |
|
32 |
import java.util.TreeSet; |
|
33 |
||
34 |
import com.sun.codemodel.internal.util.ClassNameComparator; |
|
35 |
||
36 |
/** |
|
37 |
* Java method. |
|
38 |
*/ |
|
39 |
public class JMethod extends JGenerifiableImpl implements JDeclaration, JAnnotatable { |
|
40 |
||
2719 | 41 |
/** |
42 |
* Modifiers for this method |
|
43 |
*/ |
|
44 |
private JMods mods; |
|
8 | 45 |
|
2719 | 46 |
/** |
47 |
* Return type for this method |
|
48 |
*/ |
|
49 |
private JType type = null; |
|
8 | 50 |
|
2719 | 51 |
/** |
52 |
* Name of this method |
|
53 |
*/ |
|
54 |
private String name = null; |
|
8 | 55 |
|
2719 | 56 |
/** |
57 |
* List of parameters for this method's declaration |
|
58 |
*/ |
|
59 |
private final List<JVar> params = new ArrayList<JVar>(); |
|
8 | 60 |
|
2719 | 61 |
/** |
62 |
* Set of exceptions that this method may throw. |
|
8 | 63 |
* A set instance lazily created. |
2719 | 64 |
*/ |
65 |
private Set<JClass> _throws; |
|
8 | 66 |
|
2719 | 67 |
/** |
68 |
* JBlock of statements that makes up the body this method |
|
69 |
*/ |
|
70 |
private JBlock body = null; |
|
8 | 71 |
|
2719 | 72 |
private JDefinedClass outer; |
8 | 73 |
|
2719 | 74 |
/** |
75 |
* javadoc comments for this JMethod |
|
76 |
*/ |
|
77 |
private JDocComment jdoc = null; |
|
8 | 78 |
|
2719 | 79 |
/** |
80 |
* Variable parameter for this method's varargs declaration |
|
81 |
* introduced in J2SE 1.5 |
|
82 |
*/ |
|
83 |
private JVar varParam = null; |
|
8 | 84 |
|
85 |
/** |
|
86 |
* Annotations on this variable. Lazily created. |
|
87 |
*/ |
|
88 |
private List<JAnnotationUse> annotations = null; |
|
89 |
||
90 |
||
2719 | 91 |
private boolean isConstructor() { |
92 |
return type == null; |
|
93 |
} |
|
8 | 94 |
|
95 |
/** To set the default value for the |
|
96 |
* annotation member |
|
97 |
*/ |
|
98 |
private JExpression defaultValue = null; |
|
99 |
||
100 |
||
2719 | 101 |
/** |
102 |
* JMethod constructor |
|
103 |
* |
|
104 |
* @param mods |
|
105 |
* Modifiers for this method's declaration |
|
106 |
* |
|
107 |
* @param type |
|
108 |
* Return type for the method |
|
109 |
* |
|
110 |
* @param name |
|
111 |
* Name of this method |
|
112 |
*/ |
|
113 |
JMethod(JDefinedClass outer, int mods, JType type, String name) { |
|
114 |
this.mods = JMods.forMethod(mods); |
|
115 |
this.type = type; |
|
116 |
this.name = name; |
|
117 |
this.outer = outer; |
|
118 |
} |
|
8 | 119 |
|
2719 | 120 |
/** |
121 |
* Constructor constructor |
|
122 |
* |
|
123 |
* @param mods |
|
124 |
* Modifiers for this constructor's declaration |
|
125 |
* |
|
126 |
* @param _class |
|
127 |
* JClass containing this constructor |
|
128 |
*/ |
|
129 |
JMethod(int mods, JDefinedClass _class) { |
|
130 |
this.mods = JMods.forMethod(mods); |
|
131 |
this.type = null; |
|
132 |
this.name = _class.name(); |
|
133 |
this.outer = _class; |
|
134 |
} |
|
8 | 135 |
|
136 |
private Set<JClass> getThrows() { |
|
137 |
if(_throws==null) |
|
138 |
_throws = new TreeSet<JClass>(ClassNameComparator.theInstance); |
|
139 |
return _throws; |
|
140 |
} |
|
141 |
||
2719 | 142 |
/** |
143 |
* Add an exception to the list of exceptions that this |
|
144 |
* method may throw. |
|
145 |
* |
|
146 |
* @param exception |
|
147 |
* Name of an exception that this method may throw |
|
148 |
*/ |
|
149 |
public JMethod _throws(JClass exception) { |
|
8 | 150 |
getThrows().add(exception); |
2719 | 151 |
return this; |
152 |
} |
|
8 | 153 |
|
2719 | 154 |
public JMethod _throws(Class exception) { |
155 |
return _throws(outer.owner().ref(exception)); |
|
156 |
} |
|
8 | 157 |
|
2719 | 158 |
/** |
159 |
* Add the specified variable to the list of parameters |
|
160 |
* for this method signature. |
|
161 |
* |
|
162 |
* @param type |
|
163 |
* JType of the parameter being added |
|
164 |
* |
|
165 |
* @param name |
|
166 |
* Name of the parameter being added |
|
167 |
* |
|
168 |
* @return New parameter variable |
|
169 |
*/ |
|
170 |
public JVar param(int mods, JType type, String name) { |
|
171 |
JVar v = new JVar(JMods.forVar(mods), type, name, null); |
|
172 |
params.add(v); |
|
173 |
return v; |
|
174 |
} |
|
8 | 175 |
|
2719 | 176 |
public JVar param(JType type, String name) { |
177 |
return param(JMod.NONE, type, name); |
|
178 |
} |
|
8 | 179 |
|
2719 | 180 |
public JVar param(int mods, Class type, String name) { |
181 |
return param(mods, outer.owner()._ref(type), name); |
|
182 |
} |
|
8 | 183 |
|
2719 | 184 |
public JVar param(Class type, String name) { |
185 |
return param(outer.owner()._ref(type), name); |
|
186 |
} |
|
8 | 187 |
|
2719 | 188 |
/** |
189 |
* @see #varParam(JType, String) |
|
190 |
*/ |
|
191 |
public JVar varParam(Class type, String name) { |
|
8 | 192 |
return varParam(outer.owner()._ref(type),name); |
193 |
} |
|
194 |
||
195 |
/** |
|
196 |
* Add the specified variable argument to the list of parameters |
|
197 |
* for this method signature. |
|
198 |
* |
|
199 |
* @param type |
|
200 |
* Type of the parameter being added. |
|
201 |
* |
|
202 |
* @param name |
|
203 |
* Name of the parameter being added |
|
204 |
* |
|
205 |
* @return the variable parameter |
|
206 |
* |
|
207 |
* @throws IllegalStateException |
|
208 |
* If this method is called twice. |
|
209 |
* varargs in J2SE 1.5 can appear only once in the |
|
210 |
* method signature. |
|
211 |
*/ |
|
212 |
public JVar varParam(JType type, String name) { |
|
2719 | 213 |
if (!hasVarArgs()) { |
8 | 214 |
|
215 |
varParam = |
|
2719 | 216 |
new JVar( |
217 |
JMods.forVar(JMod.NONE), |
|
218 |
type.array(), |
|
219 |
name, |
|
220 |
null); |
|
221 |
return varParam; |
|
222 |
} else { |
|
223 |
throw new IllegalStateException( |
|
224 |
"Cannot have two varargs in a method,\n" |
|
225 |
+ "Check if varParam method of JMethod is" |
|
226 |
+ " invoked more than once"); |
|
227 |
||
228 |
} |
|
8 | 229 |
|
230 |
} |
|
231 |
||
232 |
/** |
|
233 |
* Adds an annotation to this variable. |
|
234 |
* @param clazz |
|
235 |
* The annotation class to annotate the field with |
|
236 |
*/ |
|
237 |
public JAnnotationUse annotate(JClass clazz){ |
|
238 |
if(annotations==null) |
|
239 |
annotations = new ArrayList<JAnnotationUse>(); |
|
240 |
JAnnotationUse a = new JAnnotationUse(clazz); |
|
241 |
annotations.add(a); |
|
242 |
return a; |
|
243 |
} |
|
244 |
||
245 |
/** |
|
246 |
* Adds an annotation to this variable. |
|
247 |
* |
|
248 |
* @param clazz |
|
249 |
* The annotation class to annotate the field with |
|
250 |
*/ |
|
251 |
public JAnnotationUse annotate(Class <? extends Annotation> clazz){ |
|
252 |
return annotate(owner().ref(clazz)); |
|
253 |
} |
|
254 |
||
255 |
public <W extends JAnnotationWriter> W annotate2(Class<W> clazz) { |
|
256 |
return TypedAnnotationWriter.create(clazz,this); |
|
257 |
} |
|
258 |
||
2719 | 259 |
/** |
260 |
* Check if there are any varargs declared |
|
261 |
* for this method signature. |
|
262 |
*/ |
|
263 |
public boolean hasVarArgs() { |
|
264 |
return this.varParam!=null; |
|
265 |
} |
|
8 | 266 |
|
2719 | 267 |
public String name() { |
268 |
return name; |
|
269 |
} |
|
8 | 270 |
|
2678
57cf2a1c1a05
6831313: update jaxws in OpenJDK7 to 2.1 plus bug fixes from OpenJDK 6
tbell
parents:
8
diff
changeset
|
271 |
/** |
57cf2a1c1a05
6831313: update jaxws in OpenJDK7 to 2.1 plus bug fixes from OpenJDK 6
tbell
parents:
8
diff
changeset
|
272 |
* Changes the name of the method. |
57cf2a1c1a05
6831313: update jaxws in OpenJDK7 to 2.1 plus bug fixes from OpenJDK 6
tbell
parents:
8
diff
changeset
|
273 |
*/ |
57cf2a1c1a05
6831313: update jaxws in OpenJDK7 to 2.1 plus bug fixes from OpenJDK 6
tbell
parents:
8
diff
changeset
|
274 |
public void name(String n) { |
57cf2a1c1a05
6831313: update jaxws in OpenJDK7 to 2.1 plus bug fixes from OpenJDK 6
tbell
parents:
8
diff
changeset
|
275 |
this.name = n; |
57cf2a1c1a05
6831313: update jaxws in OpenJDK7 to 2.1 plus bug fixes from OpenJDK 6
tbell
parents:
8
diff
changeset
|
276 |
} |
8 | 277 |
|
2678
57cf2a1c1a05
6831313: update jaxws in OpenJDK7 to 2.1 plus bug fixes from OpenJDK 6
tbell
parents:
8
diff
changeset
|
278 |
/** |
2719 | 279 |
* Returns the return type. |
280 |
*/ |
|
281 |
public JType type() { |
|
282 |
return type; |
|
283 |
} |
|
8 | 284 |
|
2678
57cf2a1c1a05
6831313: update jaxws in OpenJDK7 to 2.1 plus bug fixes from OpenJDK 6
tbell
parents:
8
diff
changeset
|
285 |
/** |
57cf2a1c1a05
6831313: update jaxws in OpenJDK7 to 2.1 plus bug fixes from OpenJDK 6
tbell
parents:
8
diff
changeset
|
286 |
* Overrides the return type. |
57cf2a1c1a05
6831313: update jaxws in OpenJDK7 to 2.1 plus bug fixes from OpenJDK 6
tbell
parents:
8
diff
changeset
|
287 |
*/ |
57cf2a1c1a05
6831313: update jaxws in OpenJDK7 to 2.1 plus bug fixes from OpenJDK 6
tbell
parents:
8
diff
changeset
|
288 |
public void type(JType t) { |
57cf2a1c1a05
6831313: update jaxws in OpenJDK7 to 2.1 plus bug fixes from OpenJDK 6
tbell
parents:
8
diff
changeset
|
289 |
this.type = t; |
57cf2a1c1a05
6831313: update jaxws in OpenJDK7 to 2.1 plus bug fixes from OpenJDK 6
tbell
parents:
8
diff
changeset
|
290 |
} |
57cf2a1c1a05
6831313: update jaxws in OpenJDK7 to 2.1 plus bug fixes from OpenJDK 6
tbell
parents:
8
diff
changeset
|
291 |
|
57cf2a1c1a05
6831313: update jaxws in OpenJDK7 to 2.1 plus bug fixes from OpenJDK 6
tbell
parents:
8
diff
changeset
|
292 |
/** |
2719 | 293 |
* Returns all the parameter types in an array. |
294 |
* @return |
|
295 |
* If there's no parameter, an empty array will be returned. |
|
296 |
*/ |
|
297 |
public JType[] listParamTypes() { |
|
298 |
JType[] r = new JType[params.size()]; |
|
299 |
for (int i = 0; i < r.length; i++) |
|
300 |
r[i] = params.get(i).type(); |
|
301 |
return r; |
|
302 |
} |
|
8 | 303 |
|
2719 | 304 |
/** |
305 |
* Returns the varags parameter type. |
|
306 |
* @return |
|
307 |
* If there's no vararg parameter type, null will be returned. |
|
308 |
*/ |
|
309 |
public JType listVarParamType() { |
|
310 |
if (varParam != null) |
|
311 |
return varParam.type(); |
|
312 |
else |
|
313 |
return null; |
|
314 |
} |
|
8 | 315 |
|
2719 | 316 |
/** |
317 |
* Returns all the parameters in an array. |
|
318 |
* @return |
|
319 |
* If there's no parameter, an empty array will be returned. |
|
320 |
*/ |
|
321 |
public JVar[] listParams() { |
|
322 |
return params.toArray(new JVar[params.size()]); |
|
323 |
} |
|
8 | 324 |
|
2719 | 325 |
/** |
326 |
* Returns the variable parameter |
|
327 |
* @return |
|
328 |
* If there's no parameter, null will be returned. |
|
329 |
*/ |
|
330 |
public JVar listVarParam() { |
|
331 |
return varParam; |
|
332 |
} |
|
8 | 333 |
|
2719 | 334 |
/** |
335 |
* Returns true if the method has the specified signature. |
|
336 |
*/ |
|
337 |
public boolean hasSignature(JType[] argTypes) { |
|
338 |
JVar[] p = listParams(); |
|
339 |
if (p.length != argTypes.length) |
|
340 |
return false; |
|
8 | 341 |
|
2719 | 342 |
for (int i = 0; i < p.length; i++) |
343 |
if (!p[i].type().equals(argTypes[i])) |
|
344 |
return false; |
|
2678
57cf2a1c1a05
6831313: update jaxws in OpenJDK7 to 2.1 plus bug fixes from OpenJDK 6
tbell
parents:
8
diff
changeset
|
345 |
|
2719 | 346 |
return true; |
347 |
} |
|
8 | 348 |
|
2719 | 349 |
/** |
350 |
* Get the block that makes up body of this method |
|
351 |
* |
|
352 |
* @return Body of method |
|
353 |
*/ |
|
354 |
public JBlock body() { |
|
355 |
if (body == null) |
|
356 |
body = new JBlock(); |
|
357 |
return body; |
|
358 |
} |
|
8 | 359 |
|
360 |
/** |
|
361 |
* Specify the default value for this annotation member |
|
362 |
* @param value |
|
363 |
* Default value for the annotation member |
|
364 |
* |
|
365 |
*/ |
|
366 |
public void declareDefaultValue(JExpression value){ |
|
367 |
this.defaultValue = value; |
|
368 |
} |
|
369 |
||
2719 | 370 |
/** |
371 |
* Creates, if necessary, and returns the class javadoc for this |
|
372 |
* JDefinedClass |
|
373 |
* |
|
374 |
* @return JDocComment containing javadocs for this class |
|
375 |
*/ |
|
376 |
public JDocComment javadoc() { |
|
377 |
if (jdoc == null) |
|
378 |
jdoc = new JDocComment(owner()); |
|
379 |
return jdoc; |
|
380 |
} |
|
8 | 381 |
|
2719 | 382 |
public void declare(JFormatter f) { |
383 |
if (jdoc != null) |
|
384 |
f.g(jdoc); |
|
8 | 385 |
|
386 |
if (annotations != null){ |
|
387 |
for (JAnnotationUse a : annotations) |
|
388 |
f.g(a).nl(); |
|
389 |
} |
|
390 |
||
2719 | 391 |
// declare the generics parameters |
392 |
super.declare(f); |
|
8 | 393 |
|
2719 | 394 |
f.g(mods); |
395 |
if (!isConstructor()) |
|
396 |
f.g(type); |
|
397 |
f.id(name).p('(').i(); |
|
8 | 398 |
// when parameters are printed in new lines, we want them to be indented. |
399 |
// there's a good chance no newlines happen, too, but just in case it does. |
|
2719 | 400 |
boolean first = true; |
8 | 401 |
for (JVar var : params) { |
402 |
if (!first) |
|
403 |
f.p(','); |
|
404 |
if(var.isAnnotated()) |
|
405 |
f.nl(); |
|
406 |
f.b(var); |
|
407 |
first = false; |
|
408 |
} |
|
2719 | 409 |
if (hasVarArgs()) { |
410 |
if (!first) |
|
411 |
f.p(','); |
|
412 |
f.g(varParam.type().elementType()); |
|
413 |
f.p("... "); |
|
414 |
f.id(varParam.name()); |
|
415 |
} |
|
8 | 416 |
|
2719 | 417 |
f.o().p(')'); |
418 |
if (_throws!=null && !_throws.isEmpty()) { |
|
419 |
f.nl().i().p("throws").g(_throws).nl().o(); |
|
420 |
} |
|
8 | 421 |
|
422 |
if (defaultValue != null) { |
|
423 |
f.p("default "); |
|
424 |
f.g(defaultValue); |
|
425 |
} |
|
2719 | 426 |
if (body != null) { |
427 |
f.s(body); |
|
428 |
} else if ( |
|
429 |
!outer.isInterface() && !outer.isAnnotationTypeDeclaration() && !mods.isAbstract() && !mods.isNative()) { |
|
430 |
// Print an empty body for non-native, non-abstract methods |
|
431 |
f.s(new JBlock()); |
|
432 |
} else { |
|
433 |
f.p(';').nl(); |
|
434 |
} |
|
8 | 435 |
} |
436 |
||
437 |
/** |
|
438 |
* @return |
|
439 |
* the current modifiers of this method. |
|
440 |
* Always return non-null valid object. |
|
441 |
*/ |
|
442 |
public JMods mods() { |
|
443 |
return mods; |
|
444 |
} |
|
445 |
||
446 |
/** |
|
447 |
* @deprecated use {@link #mods()} |
|
448 |
*/ |
|
449 |
public JMods getMods() { |
|
2719 | 450 |
return mods; |
451 |
} |
|
8 | 452 |
|
2719 | 453 |
protected JCodeModel owner() { |
454 |
return outer.owner(); |
|
455 |
} |
|
8 | 456 |
} |