Mon, 04 May 2009 21:10:41 -0700
6658158: Mutable statics in SAAJ (findbugs)
6658163: txw2.DatatypeWriter.BUILDIN is a mutable static (findbugs)
Reviewed-by: darcy
1 /*
2 * Copyright 2005-2006 Sun Microsystems, Inc. All Rights Reserved.
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 */
26 package com.sun.codemodel.internal;
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;
34 import com.sun.codemodel.internal.util.ClassNameComparator;
36 /**
37 * Java method.
38 */
39 public class JMethod extends JGenerifiableImpl implements JDeclaration, JAnnotatable {
41 /**
42 * Modifiers for this method
43 */
44 private JMods mods;
46 /**
47 * Return type for this method
48 */
49 private JType type = null;
51 /**
52 * Name of this method
53 */
54 private String name = null;
56 /**
57 * List of parameters for this method's declaration
58 */
59 private final List<JVar> params = new ArrayList<JVar>();
61 /**
62 * Set of exceptions that this method may throw.
63 * A set instance lazily created.
64 */
65 private Set<JClass> _throws;
67 /**
68 * JBlock of statements that makes up the body this method
69 */
70 private JBlock body = null;
72 private JDefinedClass outer;
74 /**
75 * javadoc comments for this JMethod
76 */
77 private JDocComment jdoc = null;
79 /**
80 * Variable parameter for this method's varargs declaration
81 * introduced in J2SE 1.5
82 */
83 private JVar varParam = null;
85 /**
86 * Annotations on this variable. Lazily created.
87 */
88 private List<JAnnotationUse> annotations = null;
91 private boolean isConstructor() {
92 return type == null;
93 }
95 /** To set the default value for the
96 * annotation member
97 */
98 private JExpression defaultValue = null;
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 }
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 }
136 private Set<JClass> getThrows() {
137 if(_throws==null)
138 _throws = new TreeSet<JClass>(ClassNameComparator.theInstance);
139 return _throws;
140 }
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) {
150 getThrows().add(exception);
151 return this;
152 }
154 public JMethod _throws(Class exception) {
155 return _throws(outer.owner().ref(exception));
156 }
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 }
176 public JVar param(JType type, String name) {
177 return param(JMod.NONE, type, name);
178 }
180 public JVar param(int mods, Class type, String name) {
181 return param(mods, outer.owner()._ref(type), name);
182 }
184 public JVar param(Class type, String name) {
185 return param(outer.owner()._ref(type), name);
186 }
188 /**
189 * @see #varParam(JType, String)
190 */
191 public JVar varParam(Class type, String name) {
192 return varParam(outer.owner()._ref(type),name);
193 }
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) {
213 if (!hasVarArgs()) {
215 varParam =
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");
228 }
230 }
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 }
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 }
255 public <W extends JAnnotationWriter> W annotate2(Class<W> clazz) {
256 return TypedAnnotationWriter.create(clazz,this);
257 }
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 }
267 public String name() {
268 return name;
269 }
271 /**
272 * Changes the name of the method.
273 */
274 public void name(String n) {
275 this.name = n;
276 }
278 /**
279 * Returns the return type.
280 */
281 public JType type() {
282 return type;
283 }
285 /**
286 * Overrides the return type.
287 */
288 public void type(JType t) {
289 this.type = t;
290 }
292 /**
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 }
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 }
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 }
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 }
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;
342 for (int i = 0; i < p.length; i++)
343 if (!p[i].type().equals(argTypes[i]))
344 return false;
346 return true;
347 }
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 }
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 }
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 }
382 public void declare(JFormatter f) {
383 if (jdoc != null)
384 f.g(jdoc);
386 if (annotations != null){
387 for (JAnnotationUse a : annotations)
388 f.g(a).nl();
389 }
391 // declare the generics parameters
392 super.declare(f);
394 f.g(mods);
395 if (!isConstructor())
396 f.g(type);
397 f.id(name).p('(').i();
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.
400 boolean first = true;
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 }
409 if (hasVarArgs()) {
410 if (!first)
411 f.p(',');
412 f.g(varParam.type().elementType());
413 f.p("... ");
414 f.id(varParam.name());
415 }
417 f.o().p(')');
418 if (_throws!=null && !_throws.isEmpty()) {
419 f.nl().i().p("throws").g(_throws).nl().o();
420 }
422 if (defaultValue != null) {
423 f.p("default ");
424 f.g(defaultValue);
425 }
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 }
435 }
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 }
446 /**
447 * @deprecated use {@link #mods()}
448 */
449 public JMods getMods() {
450 return mods;
451 }
453 protected JCodeModel owner() {
454 return outer.owner();
455 }
456 }