src/share/classes/com/sun/codemodel/internal/JMethod.java

Mon, 04 May 2009 21:10:41 -0700

author
tbell
date
Mon, 04 May 2009 21:10:41 -0700
changeset 50
42dfec6871f6
parent 45
31822b475baa
child 78
860b95cc8d1d
permissions
-rw-r--r--

6658158: Mutable statics in SAAJ (findbugs)
6658163: txw2.DatatypeWriter.BUILDIN is a mutable static (findbugs)
Reviewed-by: darcy

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

mercurial