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

Thu, 12 Oct 2017 19:44:07 +0800

author
aoqi
date
Thu, 12 Oct 2017 19:44:07 +0800
changeset 760
e530533619ec
parent 0
373ffda63c9a
permissions
-rw-r--r--

merge

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

mercurial