| 1 | /******************************************************************************* |
|---|---|
| 2 | * Copyright (c) 2000, 2021 IBM Corporation and others. |
| 3 | * |
| 4 | * This program and the accompanying materials |
| 5 | * are made available under the terms of the Eclipse Public License 2.0 |
| 6 | * which accompanies this distribution, and is available at |
| 7 | * https://www.eclipse.org/legal/epl-2.0/ |
| 8 | * |
| 9 | * SPDX-License-Identifier: EPL-2.0 |
| 10 | * |
| 11 | * Contributors: |
| 12 | * IBM Corporation - initial API and implementation |
| 13 | *******************************************************************************/ |
| 14 | |
| 15 | package org.eclipse.jdt.core.dom; |
| 16 | |
| 17 | import java.util.ArrayList; |
| 18 | import java.util.List; |
| 19 | |
| 20 | import org.eclipse.jdt.internal.core.dom.util.DOMASTUtil; |
| 21 | |
| 22 | /** |
| 23 | * Method declaration AST node type. A method declaration |
| 24 | * is the union of a method declaration and a constructor declaration. |
| 25 | * |
| 26 | * <pre> |
| 27 | * MethodDeclaration: |
| 28 | * [ Javadoc ] { ExtendedModifier } [ <b><</b> TypeParameter { <b>,</b> TypeParameter } <b>></b> ] ( Type | <b>void</b> ) |
| 29 | * Identifier <b>(</b> |
| 30 | * [ ReceiverParameter <b>,</b> ] [ FormalParameter { <b>,</b> FormalParameter } ] |
| 31 | * <b>)</b> { Dimension } |
| 32 | * [ <b>throws</b> Type { <b>,</b> Type } ] |
| 33 | * ( Block | <b>;</b> ) |
| 34 | * ConstructorDeclaration: |
| 35 | * [ Javadoc ] { ExtendedModifier } [ <b><</b> TypeParameter { <b>,</b> TypeParameter } <b>></b> ] |
| 36 | * Identifier <b>(</b> |
| 37 | * [ ReceiverParameter <b>,</b> ] [ FormalParameter { <b>,</b> FormalParameter } ] |
| 38 | * <b>)</b> { Dimension } |
| 39 | * [ <b>throws</b> Type { <b>,</b> Type } ] |
| 40 | * ( Block | <b>;</b> ) |
| 41 | * CompactConstructorDeclaration: |
| 42 | * [ Javadoc ] ExtendedModifier { ExtendedModifier} |
| 43 | * Identifier |
| 44 | * ( Block | <b>;</b> ) |
| 45 | * </pre> |
| 46 | * <p> |
| 47 | * The ReceiverParameter is represented as: |
| 48 | * <code>Type [ SimpleName <b>.</b> ] <b>this</b></code><br> |
| 49 | * The FormalParameter is represented by a {@link SingleVariableDeclaration}. |
| 50 | * </p> |
| 51 | * <p> |
| 52 | * When a Javadoc comment is present, the source |
| 53 | * range begins with the first character of the "/**" comment delimiter. |
| 54 | * When there is no Javadoc comment, the source range begins with the first |
| 55 | * character of the first modifier keyword (if modifiers), or the |
| 56 | * first character of the "<" token (method, no modifiers, type parameters), |
| 57 | * or the first character of the return type (method, no modifiers, no type |
| 58 | * parameters), or the first character of the identifier (constructor, |
| 59 | * no modifiers). The source range extends through the last character of the |
| 60 | * ";" token (if no body), or the last character of the block (if body). |
| 61 | * The compact constructor must be declared public. (jls-8.10.5) |
| 62 | * </p> |
| 63 | * |
| 64 | * @since 2.0 |
| 65 | * @noinstantiate This class is not intended to be instantiated by clients. |
| 66 | */ |
| 67 | @SuppressWarnings({ "rawtypes", "unchecked" }) |
| 68 | public class MethodDeclaration extends BodyDeclaration { |
| 69 | |
| 70 | /** |
| 71 | * The "javadoc" structural property of this node type (child type: |
| 72 | * {@link Javadoc}). |
| 73 | * |
| 74 | * @since 3.0 |
| 75 | */ |
| 76 | public static final ChildPropertyDescriptor JAVADOC_PROPERTY = internalJavadocPropertyFactory( |
| 77 | MethodDeclaration.class); |
| 78 | |
| 79 | /** |
| 80 | * The "modifiers" structural property of this node type (type: {@link Integer}) |
| 81 | * (JLS2 API only). |
| 82 | * |
| 83 | * @since 3.0 |
| 84 | * @deprecated In the JLS3 API, this property is replaced by |
| 85 | * {@link #MODIFIERS2_PROPERTY}. |
| 86 | */ |
| 87 | public static final SimplePropertyDescriptor MODIFIERS_PROPERTY = internalModifiersPropertyFactory( |
| 88 | MethodDeclaration.class); |
| 89 | |
| 90 | /** |
| 91 | * The "modifiers" structural property of this node type (element type: |
| 92 | * {@link IExtendedModifier}) (added in JLS3 API). |
| 93 | * |
| 94 | * @since 3.1 |
| 95 | */ |
| 96 | public static final ChildListPropertyDescriptor MODIFIERS2_PROPERTY = internalModifiers2PropertyFactory( |
| 97 | MethodDeclaration.class); |
| 98 | |
| 99 | /** |
| 100 | * The "constructor" structural property of this node type (type: |
| 101 | * {@link Boolean}). |
| 102 | * |
| 103 | * @since 3.0 |
| 104 | */ |
| 105 | public static final SimplePropertyDescriptor CONSTRUCTOR_PROPERTY = new SimplePropertyDescriptor( |
| 106 | MethodDeclaration.class, "constructor", boolean.class, MANDATORY); //$NON-NLS-1$ |
| 107 | |
| 108 | /** |
| 109 | * The "compact constructor" structural property of record node type (type: |
| 110 | * {@link Boolean}). |
| 111 | * |
| 112 | * @since 3.22 |
| 113 | */ |
| 114 | public static final SimplePropertyDescriptor COMPACT_CONSTRUCTOR_PROPERTY = new SimplePropertyDescriptor( |
| 115 | MethodDeclaration.class, "compactConstructor", boolean.class, OPTIONAL); //$NON-NLS-1$ |
| 116 | |
| 117 | /** |
| 118 | * The "name" structural property of this node type (child type: |
| 119 | * {@link SimpleName}). |
| 120 | * |
| 121 | * @since 3.0 |
| 122 | */ |
| 123 | public static final ChildPropertyDescriptor NAME_PROPERTY = new ChildPropertyDescriptor(MethodDeclaration.class, |
| 124 | "name", SimpleName.class, MANDATORY, NO_CYCLE_RISK); //$NON-NLS-1$ |
| 125 | |
| 126 | /** |
| 127 | * The "returnType" structural property of this node type (child type: |
| 128 | * {@link Type}) (JLS2 API only). |
| 129 | * |
| 130 | * @since 3.0 |
| 131 | * @deprecated In the JLS3 API, this property is replaced by |
| 132 | * {@link #RETURN_TYPE2_PROPERTY}. |
| 133 | */ |
| 134 | public static final ChildPropertyDescriptor RETURN_TYPE_PROPERTY = new ChildPropertyDescriptor( |
| 135 | MethodDeclaration.class, "returnType", Type.class, MANDATORY, NO_CYCLE_RISK); //$NON-NLS-1$ |
| 136 | |
| 137 | /** |
| 138 | * The "returnType2" structural property of this node type (child type: |
| 139 | * {@link Type}) (added in JLS3 API). |
| 140 | * |
| 141 | * @since 3.1 |
| 142 | */ |
| 143 | public static final ChildPropertyDescriptor RETURN_TYPE2_PROPERTY = new ChildPropertyDescriptor( |
| 144 | MethodDeclaration.class, "returnType2", Type.class, OPTIONAL, NO_CYCLE_RISK); //$NON-NLS-1$ |
| 145 | |
| 146 | /** |
| 147 | * The "extraDimensions" structural property of this node type (type: |
| 148 | * {@link Integer}) (below JLS8 only). |
| 149 | * |
| 150 | * @since 3.0 |
| 151 | * @deprecated In JLS8 and later, use |
| 152 | * {@link MethodDeclaration#EXTRA_DIMENSIONS2_PROPERTY} instead. |
| 153 | */ |
| 154 | public static final SimplePropertyDescriptor EXTRA_DIMENSIONS_PROPERTY = new SimplePropertyDescriptor( |
| 155 | MethodDeclaration.class, "extraDimensions", int.class, MANDATORY); //$NON-NLS-1$ |
| 156 | |
| 157 | /** |
| 158 | * The "extraDimensions2" structural property of this node type (element type: |
| 159 | * {@link Dimension}) (added in JLS8 API). |
| 160 | * |
| 161 | * @since 3.10 |
| 162 | */ |
| 163 | public static final ChildListPropertyDescriptor EXTRA_DIMENSIONS2_PROPERTY = new ChildListPropertyDescriptor( |
| 164 | MethodDeclaration.class, "extraDimensions2", Dimension.class, NO_CYCLE_RISK); //$NON-NLS-1$ |
| 165 | |
| 166 | /** |
| 167 | * The "typeParameters" structural property of this node type (element type: |
| 168 | * {@link TypeParameter}) (added in JLS3 API). |
| 169 | * |
| 170 | * @since 3.1 |
| 171 | */ |
| 172 | public static final ChildListPropertyDescriptor TYPE_PARAMETERS_PROPERTY = new ChildListPropertyDescriptor( |
| 173 | MethodDeclaration.class, "typeParameters", TypeParameter.class, NO_CYCLE_RISK); //$NON-NLS-1$ |
| 174 | |
| 175 | /** |
| 176 | * The "parameters" structural property of this node type (element type: |
| 177 | * {@link SingleVariableDeclaration}). |
| 178 | * |
| 179 | * @since 3.0 |
| 180 | */ |
| 181 | public static final ChildListPropertyDescriptor PARAMETERS_PROPERTY = new ChildListPropertyDescriptor( |
| 182 | MethodDeclaration.class, "parameters", SingleVariableDeclaration.class, CYCLE_RISK); //$NON-NLS-1$ |
| 183 | |
| 184 | /** |
| 185 | * The "receiverType" structural property of this node type (child type: |
| 186 | * {@link Type}) (added in JLS8 API). |
| 187 | * |
| 188 | * @since 3.10 |
| 189 | */ |
| 190 | public static final ChildPropertyDescriptor RECEIVER_TYPE_PROPERTY = new ChildPropertyDescriptor( |
| 191 | MethodDeclaration.class, "receiverType", Type.class, OPTIONAL, NO_CYCLE_RISK); //$NON-NLS-1$ |
| 192 | |
| 193 | /** |
| 194 | * The "receiverQualifier" structural property of this node type (child type: |
| 195 | * {@link SimpleName}) (added in JLS8 API). |
| 196 | * |
| 197 | * @since 3.10 |
| 198 | */ |
| 199 | public static final ChildPropertyDescriptor RECEIVER_QUALIFIER_PROPERTY = new ChildPropertyDescriptor( |
| 200 | MethodDeclaration.class, "receiverQualifier", SimpleName.class, OPTIONAL, NO_CYCLE_RISK); //$NON-NLS-1$ |
| 201 | |
| 202 | /** |
| 203 | * The "thrownExceptions" structural property of this node type (element type: |
| 204 | * {@link Name}) (before JLS8 only). |
| 205 | * |
| 206 | * @deprecated In JLS8 and later, use |
| 207 | * {@link MethodDeclaration#THROWN_EXCEPTION_TYPES_PROPERTY} |
| 208 | * instead. |
| 209 | * @since 3.0 |
| 210 | */ |
| 211 | public static final ChildListPropertyDescriptor THROWN_EXCEPTIONS_PROPERTY = new ChildListPropertyDescriptor( |
| 212 | MethodDeclaration.class, "thrownExceptions", Name.class, NO_CYCLE_RISK); //$NON-NLS-1$ |
| 213 | |
| 214 | /** |
| 215 | * The "thrownExceptionTypes" structural property of this node type (element |
| 216 | * type: {@link Type}) (added in JLS8 API). |
| 217 | * |
| 218 | * @since 3.10 |
| 219 | */ |
| 220 | public static final ChildListPropertyDescriptor THROWN_EXCEPTION_TYPES_PROPERTY = new ChildListPropertyDescriptor( |
| 221 | MethodDeclaration.class, "thrownExceptionTypes", Type.class, NO_CYCLE_RISK); //$NON-NLS-1$ |
| 222 | |
| 223 | /** |
| 224 | * The "body" structural property of this node type (child type: {@link Block}). |
| 225 | * |
| 226 | * @since 3.0 |
| 227 | */ |
| 228 | public static final ChildPropertyDescriptor BODY_PROPERTY = new ChildPropertyDescriptor(MethodDeclaration.class, |
| 229 | "body", Block.class, OPTIONAL, CYCLE_RISK); //$NON-NLS-1$ |
| 230 | |
| 231 | /** |
| 232 | * A list of property descriptors (element type: |
| 233 | * {@link StructuralPropertyDescriptor}), |
| 234 | * or null if uninitialized. |
| 235 | * |
| 236 | * @since 3.0 |
| 237 | */ |
| 238 | private static final List PROPERTY_DESCRIPTORS_2_0; |
| 239 | |
| 240 | /** |
| 241 | * A list of property descriptors (element type: |
| 242 | * {@link StructuralPropertyDescriptor}), |
| 243 | * or null if uninitialized. |
| 244 | * |
| 245 | * @since 3.1 |
| 246 | */ |
| 247 | private static final List PROPERTY_DESCRIPTORS_3_0; |
| 248 | |
| 249 | /** |
| 250 | * A list of property descriptors (element type: |
| 251 | * {@link StructuralPropertyDescriptor}), |
| 252 | * or null if uninitialized. |
| 253 | * |
| 254 | * @since 3.10 |
| 255 | */ |
| 256 | private static final List PROPERTY_DESCRIPTORS_8_0; |
| 257 | |
| 258 | /** |
| 259 | * A list of property descriptors (element type: |
| 260 | * {@link StructuralPropertyDescriptor}), |
| 261 | * or null if uninitialized. |
| 262 | * |
| 263 | * @since 3.22 |
| 264 | */ |
| 265 | private static final List PROPERTY_DESCRIPTORS_9_0; |
| 266 | |
| 267 | static { |
| 268 | List propertyList = new ArrayList(10); |
| 269 | createPropertyList(MethodDeclaration.class, propertyList); |
| 270 | addProperty(JAVADOC_PROPERTY, propertyList); |
| 271 | addProperty(MODIFIERS_PROPERTY, propertyList); |
| 272 | addProperty(CONSTRUCTOR_PROPERTY, propertyList); |
| 273 | addProperty(RETURN_TYPE_PROPERTY, propertyList); |
| 274 | addProperty(NAME_PROPERTY, propertyList); |
| 275 | addProperty(PARAMETERS_PROPERTY, propertyList); |
| 276 | addProperty(EXTRA_DIMENSIONS_PROPERTY, propertyList); |
| 277 | addProperty(THROWN_EXCEPTIONS_PROPERTY, propertyList); |
| 278 | addProperty(BODY_PROPERTY, propertyList); |
| 279 | PROPERTY_DESCRIPTORS_2_0 = reapPropertyList(propertyList); |
| 280 | |
| 281 | propertyList = new ArrayList(11); |
| 282 | createPropertyList(MethodDeclaration.class, propertyList); |
| 283 | addProperty(JAVADOC_PROPERTY, propertyList); |
| 284 | addProperty(MODIFIERS2_PROPERTY, propertyList); |
| 285 | addProperty(CONSTRUCTOR_PROPERTY, propertyList); |
| 286 | addProperty(TYPE_PARAMETERS_PROPERTY, propertyList); |
| 287 | addProperty(RETURN_TYPE2_PROPERTY, propertyList); |
| 288 | addProperty(NAME_PROPERTY, propertyList); |
| 289 | addProperty(PARAMETERS_PROPERTY, propertyList); |
| 290 | addProperty(EXTRA_DIMENSIONS_PROPERTY, propertyList); |
| 291 | addProperty(THROWN_EXCEPTIONS_PROPERTY, propertyList); |
| 292 | addProperty(BODY_PROPERTY, propertyList); |
| 293 | PROPERTY_DESCRIPTORS_3_0 = reapPropertyList(propertyList); |
| 294 | |
| 295 | propertyList = new ArrayList(13); |
| 296 | createPropertyList(MethodDeclaration.class, propertyList); |
| 297 | addProperty(JAVADOC_PROPERTY, propertyList); |
| 298 | addProperty(MODIFIERS2_PROPERTY, propertyList); |
| 299 | addProperty(CONSTRUCTOR_PROPERTY, propertyList); |
| 300 | addProperty(TYPE_PARAMETERS_PROPERTY, propertyList); |
| 301 | addProperty(RETURN_TYPE2_PROPERTY, propertyList); |
| 302 | addProperty(NAME_PROPERTY, propertyList); |
| 303 | addProperty(RECEIVER_TYPE_PROPERTY, propertyList); |
| 304 | addProperty(RECEIVER_QUALIFIER_PROPERTY, propertyList); |
| 305 | addProperty(PARAMETERS_PROPERTY, propertyList); |
| 306 | addProperty(EXTRA_DIMENSIONS2_PROPERTY, propertyList); |
| 307 | addProperty(THROWN_EXCEPTION_TYPES_PROPERTY, propertyList); |
| 308 | addProperty(BODY_PROPERTY, propertyList); |
| 309 | PROPERTY_DESCRIPTORS_8_0 = reapPropertyList(propertyList); |
| 310 | |
| 311 | propertyList = new ArrayList(14); |
| 312 | createPropertyList(MethodDeclaration.class, propertyList); |
| 313 | addProperty(JAVADOC_PROPERTY, propertyList); |
| 314 | addProperty(MODIFIERS2_PROPERTY, propertyList); |
| 315 | addProperty(CONSTRUCTOR_PROPERTY, propertyList); |
| 316 | addProperty(TYPE_PARAMETERS_PROPERTY, propertyList); |
| 317 | addProperty(RETURN_TYPE2_PROPERTY, propertyList); |
| 318 | addProperty(NAME_PROPERTY, propertyList); |
| 319 | addProperty(RECEIVER_TYPE_PROPERTY, propertyList); |
| 320 | addProperty(RECEIVER_QUALIFIER_PROPERTY, propertyList); |
| 321 | addProperty(PARAMETERS_PROPERTY, propertyList); |
| 322 | addProperty(EXTRA_DIMENSIONS2_PROPERTY, propertyList); |
| 323 | addProperty(THROWN_EXCEPTION_TYPES_PROPERTY, propertyList); |
| 324 | addProperty(BODY_PROPERTY, propertyList); |
| 325 | addProperty(COMPACT_CONSTRUCTOR_PROPERTY, propertyList); |
| 326 | PROPERTY_DESCRIPTORS_9_0 = reapPropertyList(propertyList); |
| 327 | } |
| 328 | |
| 329 | /** |
| 330 | * Returns a list of structural property descriptors for this node type. |
| 331 | * Clients must not modify the result. |
| 332 | * |
| 333 | * @param apiLevel the API level; one of the AST.JLS* constants |
| 334 | * @return a list of property descriptors (element type: |
| 335 | * {@link StructuralPropertyDescriptor}) |
| 336 | * @since 3.0 |
| 337 | */ |
| 338 | public static List propertyDescriptors(int apiLevel) { |
| 339 | if (apiLevel == AST.JLS2_INTERNAL) { |
| 340 | return PROPERTY_DESCRIPTORS_2_0; |
| 341 | } else if (apiLevel < AST.JLS8_INTERNAL) { |
| 342 | return PROPERTY_DESCRIPTORS_3_0; |
| 343 | } else if (DOMASTUtil.isRecordDeclarationSupported(apiLevel)) { // >= JLS 16 |
| 344 | return PROPERTY_DESCRIPTORS_9_0; |
| 345 | } else { |
| 346 | return PROPERTY_DESCRIPTORS_8_0; |
| 347 | } |
| 348 | } |
| 349 | |
| 350 | /** |
| 351 | * <code>true</code> for a constructor, <code>false</code> for a method. |
| 352 | * Defaults to method. |
| 353 | */ |
| 354 | private boolean isConstructor = false; |
| 355 | |
| 356 | /** |
| 357 | * <code>true</code> for a compact constructor in a record, <code>false</code> |
| 358 | * for a method. |
| 359 | * Defaults to method. |
| 360 | */ |
| 361 | private boolean isCompactConstructor = false; |
| 362 | |
| 363 | /** |
| 364 | * The method name; lazily initialized; defaults to an unspecified, |
| 365 | * legal Java identifier. |
| 366 | */ |
| 367 | private SimpleName methodName = null; |
| 368 | |
| 369 | /** |
| 370 | * The explicit receiver type, or <code>null</code> if none. |
| 371 | * Defaults to none. |
| 372 | * |
| 373 | * @since 3.10 |
| 374 | */ |
| 375 | private Type optionalReceiverType = null; |
| 376 | |
| 377 | /** |
| 378 | * Qualifying name of the explicit </code>this</code> parameter, or |
| 379 | * <code>null</code> if none. |
| 380 | * Defaults to none. |
| 381 | * |
| 382 | * @since 3.10 |
| 383 | */ |
| 384 | private SimpleName optionalReceiverQualifier = null; |
| 385 | |
| 386 | /** |
| 387 | * The parameter declarations |
| 388 | * (element type: {@link SingleVariableDeclaration}). |
| 389 | * Defaults to an empty list. |
| 390 | */ |
| 391 | private ASTNode.NodeList parameters = new ASTNode.NodeList(PARAMETERS_PROPERTY); |
| 392 | |
| 393 | /** |
| 394 | * The return type. |
| 395 | * JLS2 behavior: lazily initialized; defaults to void. |
| 396 | * JLS3 and later: lazily initialized; defaults to void; null allowed. |
| 397 | * Note that this field is ignored for constructor declarations. |
| 398 | */ |
| 399 | private Type returnType = null; |
| 400 | |
| 401 | /** |
| 402 | * Indicated whether the return type has been initialized. |
| 403 | * |
| 404 | * @since 3.1 |
| 405 | */ |
| 406 | private boolean returnType2Initialized = false; |
| 407 | |
| 408 | /** |
| 409 | * The type paramters (element type: {@link TypeParameter}). |
| 410 | * Null in JLS2. Added in JLS3; defaults to an empty list |
| 411 | * (see constructor). |
| 412 | * |
| 413 | * @since 3.1 |
| 414 | */ |
| 415 | private ASTNode.NodeList typeParameters = null; |
| 416 | |
| 417 | /** |
| 418 | * The number of array dimensions that appear after the parameters, rather |
| 419 | * than after the return type itself; defaults to 0. Not used in JLS8 and later. |
| 420 | * |
| 421 | * @since 2.1 |
| 422 | * @deprecated In JLS8 and later, use {@link #extraDimensions} instead. |
| 423 | */ |
| 424 | private int extraArrayDimensions = 0; |
| 425 | |
| 426 | /** |
| 427 | * List of extra dimensions this node has with optional annotations |
| 428 | * (element type: {@link Dimension}). |
| 429 | * Null before JLS8. Added in JLS8; defaults to an empty list |
| 430 | * (see constructor). |
| 431 | * |
| 432 | * @since 3.10 |
| 433 | */ |
| 434 | private ASTNode.NodeList extraDimensions = null; |
| 435 | |
| 436 | /** |
| 437 | * The list of thrown exception names (element type: {@link Name}). |
| 438 | * Before JLS8: defaults to an empty list (see constructor). |
| 439 | * JLS8 and later: null. |
| 440 | * |
| 441 | * @deprecated In JLS8 and later, use {@link #thrownExceptionTypes} instead. |
| 442 | */ |
| 443 | private ASTNode.NodeList thrownExceptions = null; |
| 444 | |
| 445 | /** |
| 446 | * The list of thrown exception Types (element type: {@link Type}). |
| 447 | * Null before JLS8. Added in JLS8; defaults to an empty list |
| 448 | * (see constructor). |
| 449 | * |
| 450 | * @since 3.10 |
| 451 | */ |
| 452 | private ASTNode.NodeList thrownExceptionTypes = null; |
| 453 | |
| 454 | /** |
| 455 | * The method body, or <code>null</code> if none. |
| 456 | * Defaults to none. |
| 457 | */ |
| 458 | private Block optionalBody = null; |
| 459 | |
| 460 | /** |
| 461 | * Creates a new AST node for a method declaration owned |
| 462 | * by the given AST. By default, the declaration is for a method of an |
| 463 | * unspecified, but legal, name; no modifiers; no javadoc; no type |
| 464 | * parameters; void return type; no parameters; no array dimensions after |
| 465 | * the parameters; no thrown exceptions; and no body (as opposed to an |
| 466 | * empty body). |
| 467 | * <p> |
| 468 | * N.B. This constructor is package-private; all subclasses must be |
| 469 | * declared in the same package; clients are unable to declare |
| 470 | * additional subclasses. |
| 471 | * </p> |
| 472 | * |
| 473 | * @param ast the AST that is to own this node |
| 474 | */ |
| 475 | MethodDeclaration(AST ast) { |
| 476 | super(ast); |
| 477 | if (ast.apiLevel >= AST.JLS3_INTERNAL) { |
| 478 | this.typeParameters = new ASTNode.NodeList(TYPE_PARAMETERS_PROPERTY); |
| 479 | } |
| 480 | if (ast.apiLevel < AST.JLS8_INTERNAL) { |
| 481 | this.thrownExceptions = new ASTNode.NodeList(THROWN_EXCEPTIONS_PROPERTY); |
| 482 | } else { |
| 483 | this.extraDimensions = new ASTNode.NodeList(EXTRA_DIMENSIONS2_PROPERTY); |
| 484 | this.thrownExceptionTypes = new ASTNode.NodeList(THROWN_EXCEPTION_TYPES_PROPERTY); |
| 485 | } |
| 486 | } |
| 487 | |
| 488 | /* |
| 489 | * (omit javadoc for this method) |
| 490 | * Method declared on ASTNode. |
| 491 | * |
| 492 | * @since 3.0 |
| 493 | */ |
| 494 | @Override |
| 495 | final List internalStructuralPropertiesForType(int apiLevel) { |
| 496 | return propertyDescriptors(apiLevel); |
| 497 | } |
| 498 | |
| 499 | @Override |
| 500 | final int internalGetSetIntProperty(SimplePropertyDescriptor property, boolean get, int value) { |
| 501 | if (property == MODIFIERS_PROPERTY) { |
| 502 | if (get) { |
| 503 | return getModifiers(); |
| 504 | } else { |
| 505 | internalSetModifiers(value); |
| 506 | return 0; |
| 507 | } |
| 508 | } |
| 509 | if (property == EXTRA_DIMENSIONS_PROPERTY) { |
| 510 | if (get) { |
| 511 | return getExtraDimensions(); |
| 512 | } else { |
| 513 | setExtraDimensions(value); |
| 514 | return 0; |
| 515 | } |
| 516 | } |
| 517 | // allow default implementation to flag the error |
| 518 | return super.internalGetSetIntProperty(property, get, value); |
| 519 | } |
| 520 | |
| 521 | @Override |
| 522 | final boolean internalGetSetBooleanProperty(SimplePropertyDescriptor property, boolean get, boolean value) { |
| 523 | if (property == CONSTRUCTOR_PROPERTY) { |
| 524 | if (get) { |
| 525 | return isConstructor(); |
| 526 | } else { |
| 527 | setConstructor(value); |
| 528 | return false; |
| 529 | } |
| 530 | } else if (property == COMPACT_CONSTRUCTOR_PROPERTY) { |
| 531 | if (get) { |
| 532 | return isCompactConstructor(); |
| 533 | } else { |
| 534 | setCompactConstructor(value); |
| 535 | return false; |
| 536 | } |
| 537 | } |
| 538 | // allow default implementation to flag the error |
| 539 | return super.internalGetSetBooleanProperty(property, get, value); |
| 540 | } |
| 541 | |
| 542 | @Override |
| 543 | final ASTNode internalGetSetChildProperty(ChildPropertyDescriptor property, boolean get, ASTNode child) { |
| 544 | if (property == JAVADOC_PROPERTY) { |
| 545 | if (get) { |
| 546 | return getJavadoc(); |
| 547 | } else { |
| 548 | setJavadoc((Javadoc) child); |
| 549 | return null; |
| 550 | } |
| 551 | } |
| 552 | if (property == NAME_PROPERTY) { |
| 553 | if (get) { |
| 554 | return getName(); |
| 555 | } else { |
| 556 | setName((SimpleName) child); |
| 557 | return null; |
| 558 | } |
| 559 | } |
| 560 | if (property == RETURN_TYPE_PROPERTY) { |
| 561 | if (get) { |
| 562 | return getReturnType(); |
| 563 | } else { |
| 564 | setReturnType((Type) child); |
| 565 | return null; |
| 566 | } |
| 567 | } |
| 568 | if (property == RETURN_TYPE2_PROPERTY) { |
| 569 | if (get) { |
| 570 | return getReturnType2(); |
| 571 | } else { |
| 572 | setReturnType2((Type) child); |
| 573 | return null; |
| 574 | } |
| 575 | } |
| 576 | if (property == RECEIVER_TYPE_PROPERTY) { |
| 577 | if (get) { |
| 578 | return getReceiverType(); |
| 579 | } else { |
| 580 | setReceiverType((Type) child); |
| 581 | return null; |
| 582 | } |
| 583 | } |
| 584 | if (property == RECEIVER_QUALIFIER_PROPERTY) { |
| 585 | if (get) { |
| 586 | return getReceiverQualifier(); |
| 587 | } else { |
| 588 | setReceiverQualifier((SimpleName) child); |
| 589 | return null; |
| 590 | } |
| 591 | } |
| 592 | if (property == BODY_PROPERTY) { |
| 593 | if (get) { |
| 594 | return getBody(); |
| 595 | } else { |
| 596 | setBody((Block) child); |
| 597 | return null; |
| 598 | } |
| 599 | } |
| 600 | // allow default implementation to flag the error |
| 601 | return super.internalGetSetChildProperty(property, get, child); |
| 602 | } |
| 603 | |
| 604 | @Override |
| 605 | final List internalGetChildListProperty(ChildListPropertyDescriptor property) { |
| 606 | if (property == MODIFIERS2_PROPERTY) { |
| 607 | return modifiers(); |
| 608 | } |
| 609 | if (property == TYPE_PARAMETERS_PROPERTY) { |
| 610 | return typeParameters(); |
| 611 | } |
| 612 | if (property == PARAMETERS_PROPERTY) { |
| 613 | return parameters(); |
| 614 | } |
| 615 | if (property == THROWN_EXCEPTIONS_PROPERTY) { |
| 616 | return thrownExceptions(); |
| 617 | } |
| 618 | if (property == THROWN_EXCEPTION_TYPES_PROPERTY) { |
| 619 | return thrownExceptionTypes(); |
| 620 | } |
| 621 | if (property == EXTRA_DIMENSIONS2_PROPERTY) { |
| 622 | return extraDimensions(); |
| 623 | } |
| 624 | // allow default implementation to flag the error |
| 625 | return super.internalGetChildListProperty(property); |
| 626 | } |
| 627 | |
| 628 | @Override |
| 629 | final ChildPropertyDescriptor internalJavadocProperty() { |
| 630 | return JAVADOC_PROPERTY; |
| 631 | } |
| 632 | |
| 633 | @Override |
| 634 | final ChildListPropertyDescriptor internalModifiers2Property() { |
| 635 | return MODIFIERS2_PROPERTY; |
| 636 | } |
| 637 | |
| 638 | @Override |
| 639 | final SimplePropertyDescriptor internalModifiersProperty() { |
| 640 | return MODIFIERS_PROPERTY; |
| 641 | } |
| 642 | |
| 643 | @Override |
| 644 | final int getNodeType0() { |
| 645 | return METHOD_DECLARATION; |
| 646 | } |
| 647 | |
| 648 | @Override |
| 649 | ASTNode clone0(AST target) { |
| 650 | MethodDeclaration result = new MethodDeclaration(target); |
| 651 | result.setSourceRange(getStartPosition(), getLength()); |
| 652 | result.setJavadoc( |
| 653 | (Javadoc) ASTNode.copySubtree(target, getJavadoc())); |
| 654 | if (this.ast.apiLevel == AST.JLS2_INTERNAL) { |
| 655 | result.internalSetModifiers(getModifiers()); |
| 656 | result.setReturnType( |
| 657 | (Type) ASTNode.copySubtree(target, getReturnType())); |
| 658 | } |
| 659 | if (this.ast.apiLevel >= AST.JLS3_INTERNAL) { |
| 660 | result.modifiers().addAll(ASTNode.copySubtrees(target, modifiers())); |
| 661 | result.typeParameters().addAll( |
| 662 | ASTNode.copySubtrees(target, typeParameters())); |
| 663 | result.setReturnType2( |
| 664 | (Type) ASTNode.copySubtree(target, getReturnType2())); |
| 665 | } |
| 666 | result.setConstructor(isConstructor()); |
| 667 | result.setName((SimpleName) getName().clone(target)); |
| 668 | if (this.ast.apiLevel >= AST.JLS8_INTERNAL) { |
| 669 | result.setReceiverType((Type) ASTNode.copySubtree(target, getReceiverType())); |
| 670 | result.setReceiverQualifier((SimpleName) ASTNode.copySubtree(target, getReceiverQualifier())); |
| 671 | } |
| 672 | result.parameters().addAll( |
| 673 | ASTNode.copySubtrees(target, parameters())); |
| 674 | if (this.ast.apiLevel >= AST.JLS8_INTERNAL) { |
| 675 | result.extraDimensions().addAll(ASTNode.copySubtrees(target, extraDimensions())); |
| 676 | } else { |
| 677 | result.setExtraDimensions(getExtraDimensions()); |
| 678 | } |
| 679 | if (this.ast.apiLevel() >= AST.JLS8_INTERNAL) { |
| 680 | result.thrownExceptionTypes().addAll(ASTNode.copySubtrees(target, thrownExceptionTypes())); |
| 681 | } else { |
| 682 | result.thrownExceptions().addAll(ASTNode.copySubtrees(target, thrownExceptions())); |
| 683 | } |
| 684 | if (DOMASTUtil.isRecordDeclarationSupported(this.ast)) { |
| 685 | result.setCompactConstructor(isCompactConstructor()); |
| 686 | } |
| 687 | result.setBody( |
| 688 | (Block) ASTNode.copySubtree(target, getBody())); |
| 689 | return result; |
| 690 | } |
| 691 | |
| 692 | @Override |
| 693 | final boolean subtreeMatch0(ASTMatcher matcher, Object other) { |
| 694 | // dispatch to correct overloaded match method |
| 695 | return matcher.match(this, other); |
| 696 | } |
| 697 | |
| 698 | @Override |
| 699 | void accept0(ASTVisitor visitor) { |
| 700 | boolean visitChildren = visitor.visit(this); |
| 701 | if (visitChildren) { |
| 702 | visitor.pushScope(this.getName().getIdentifier()); |
| 703 | // visit children in normal left to right reading order |
| 704 | acceptChild(visitor, getJavadoc()); |
| 705 | if (this.ast.apiLevel == AST.JLS2_INTERNAL) { |
| 706 | acceptChild(visitor, getReturnType()); |
| 707 | } else { |
| 708 | acceptChildren(visitor, this.modifiers); |
| 709 | acceptChildren(visitor, this.typeParameters); |
| 710 | acceptChild(visitor, getReturnType2()); |
| 711 | } |
| 712 | // n.b. visit return type even for constructors |
| 713 | acceptChild(visitor, getName()); |
| 714 | if (this.ast.apiLevel >= AST.JLS8_INTERNAL) { |
| 715 | acceptChild(visitor, this.optionalReceiverType); |
| 716 | acceptChild(visitor, this.optionalReceiverQualifier); |
| 717 | } |
| 718 | acceptChildren(visitor, this.parameters); |
| 719 | if (this.ast.apiLevel() >= AST.JLS8_INTERNAL) { |
| 720 | acceptChildren(visitor, this.extraDimensions); |
| 721 | acceptChildren(visitor, this.thrownExceptionTypes); |
| 722 | } else { |
| 723 | acceptChildren(visitor, this.thrownExceptions); |
| 724 | } |
| 725 | acceptChild(visitor, getBody()); |
| 726 | visitor.popScope(); |
| 727 | } |
| 728 | visitor.endVisit(this); |
| 729 | } |
| 730 | |
| 731 | /** |
| 732 | * Returns whether this declaration declares a constructor or a method. |
| 733 | * |
| 734 | * @return <code>true</code> if this is a constructor declaration, |
| 735 | * and <code>false</code> if this is a method declaration |
| 736 | */ |
| 737 | public boolean isConstructor() { |
| 738 | return this.isConstructor; |
| 739 | } |
| 740 | |
| 741 | /** |
| 742 | * Sets whether this declaration declares a constructor or a method. |
| 743 | * |
| 744 | * @param isConstructor <code>true</code> for a constructor declaration, |
| 745 | * and <code>false</code> for a method declaration |
| 746 | */ |
| 747 | public void setConstructor(boolean isConstructor) { |
| 748 | preValueChange(CONSTRUCTOR_PROPERTY); |
| 749 | this.isConstructor = isConstructor; |
| 750 | postValueChange(CONSTRUCTOR_PROPERTY); |
| 751 | } |
| 752 | |
| 753 | /** |
| 754 | * Returns whether this declaration declares a constructor or a method. |
| 755 | * |
| 756 | * @return <code>true</code> if this is a compact constructor declaration in a |
| 757 | * record, |
| 758 | * and <code>false</code> if this is a method declaration |
| 759 | * @since 3.26 |
| 760 | * @exception UnsupportedOperationException if this operation is used below |
| 761 | * JLS16 |
| 762 | */ |
| 763 | |
| 764 | public boolean isCompactConstructor() { |
| 765 | unsupportedBelow16(); |
| 766 | return this.isCompactConstructor; |
| 767 | } |
| 768 | |
| 769 | /** |
| 770 | * Sets whether this declaration declares a compact constructor in a record or a |
| 771 | * method. |
| 772 | * |
| 773 | * @param isCompactConstructor <code>true</code> for a constructor declaration, |
| 774 | * and <code>false</code> for a method declaration |
| 775 | * @since 3.26 |
| 776 | * @exception UnsupportedOperationException if this operation is used below |
| 777 | * JLS16 |
| 778 | */ |
| 779 | |
| 780 | public void setCompactConstructor(boolean isCompactConstructor) { |
| 781 | unsupportedBelow16(); |
| 782 | preValueChange(COMPACT_CONSTRUCTOR_PROPERTY); |
| 783 | this.isCompactConstructor = isCompactConstructor; |
| 784 | postValueChange(COMPACT_CONSTRUCTOR_PROPERTY); |
| 785 | } |
| 786 | |
| 787 | /** |
| 788 | * Returns the live ordered list of type parameters of this method |
| 789 | * declaration (added in JLS3 API). This list is non-empty for parameterized |
| 790 | * methods. |
| 791 | * |
| 792 | * @return the live list of type parameters |
| 793 | * (element type: {@link TypeParameter}) |
| 794 | * @exception UnsupportedOperationException if this operation is used in |
| 795 | * a JLS2 AST |
| 796 | * @since 3.1 |
| 797 | */ |
| 798 | public List typeParameters() { |
| 799 | // more efficient than just calling unsupportedIn2() to check |
| 800 | if (this.typeParameters == null) { |
| 801 | unsupportedIn2(); |
| 802 | } |
| 803 | return this.typeParameters; |
| 804 | } |
| 805 | |
| 806 | /** |
| 807 | * Returns the name of the method declared in this method declaration. |
| 808 | * For a constructor declaration, this should be the same as the name |
| 809 | * of the class. |
| 810 | * |
| 811 | * @return the method name node |
| 812 | */ |
| 813 | public SimpleName getName() { |
| 814 | if (this.methodName == null) { |
| 815 | // lazy init must be thread-safe for readers |
| 816 | synchronized (this) { |
| 817 | if (this.methodName == null) { |
| 818 | preLazyInit(); |
| 819 | this.methodName = new SimpleName(this.ast); |
| 820 | postLazyInit(this.methodName, NAME_PROPERTY); |
| 821 | } |
| 822 | } |
| 823 | } |
| 824 | return this.methodName; |
| 825 | } |
| 826 | |
| 827 | /** |
| 828 | * Sets the name of the method declared in this method declaration to the |
| 829 | * given name. For a constructor declaration, this should be the same as |
| 830 | * the name of the class. |
| 831 | * |
| 832 | * @param methodName the new method name |
| 833 | * @exception IllegalArgumentException if: |
| 834 | * <ul> |
| 835 | * <li>the node belongs to a different |
| 836 | * AST</li> |
| 837 | * <li>the node already has a parent</li> |
| 838 | * </ul> |
| 839 | */ |
| 840 | public void setName(SimpleName methodName) { |
| 841 | if (methodName == null) { |
| 842 | throw new IllegalArgumentException(); |
| 843 | } |
| 844 | ASTNode oldChild = this.methodName; |
| 845 | preReplaceChild(oldChild, methodName, NAME_PROPERTY); |
| 846 | this.methodName = methodName; |
| 847 | postReplaceChild(oldChild, methodName, NAME_PROPERTY); |
| 848 | } |
| 849 | |
| 850 | /** |
| 851 | * Returns the receiver type explicitly declared in the method or constructor |
| 852 | * declaration (added in JLS8 API). |
| 853 | * |
| 854 | * If the receiver is not explicitly declared in the method or constructor |
| 855 | * declaration, <code>null</code> is returned. |
| 856 | * |
| 857 | * @return the receiver type or <code>null</code> if receiver is not declared |
| 858 | * explicitly |
| 859 | * @exception UnsupportedOperationException if this operation is used below JLS8 |
| 860 | * @since 3.10 |
| 861 | */ |
| 862 | public Type getReceiverType() { |
| 863 | unsupportedIn2_3_4(); |
| 864 | return this.optionalReceiverType; |
| 865 | } |
| 866 | |
| 867 | /** |
| 868 | * Sets or clears the given type as the type of explicit receiver parameter |
| 869 | * (added in JLS8 API). |
| 870 | * <p> |
| 871 | * A receiver type is only legal in Java code if it appears on an instance |
| 872 | * method or on a constructor of an inner class. |
| 873 | * </p> |
| 874 | * |
| 875 | * @param receiverType type of the explicit receiver parameter, or |
| 876 | * <code>null</code> if there is none |
| 877 | * @exception UnsupportedOperationException if this operation is used below JLS8 |
| 878 | * @since 3.10 |
| 879 | */ |
| 880 | public void setReceiverType(Type receiverType) { |
| 881 | unsupportedIn2_3_4(); |
| 882 | ASTNode oldChild = this.optionalReceiverType; |
| 883 | preReplaceChild(oldChild, receiverType, RECEIVER_TYPE_PROPERTY); |
| 884 | this.optionalReceiverType = receiverType; |
| 885 | postReplaceChild(oldChild, receiverType, RECEIVER_TYPE_PROPERTY); |
| 886 | } |
| 887 | |
| 888 | /** |
| 889 | * Returns the qualifying name, if any, for the explicit receiver or |
| 890 | * <code>null</code> if not used (added in JLS8 API). |
| 891 | * <p> |
| 892 | * A receiver qualifier is only legal in Java code if it appears on a |
| 893 | * constructor of an inner class. |
| 894 | * </p> |
| 895 | * |
| 896 | * @return the qualifying name or <code>null</code> if a qualifier was not |
| 897 | * specified |
| 898 | * @exception UnsupportedOperationException if this operation is used below JLS8 |
| 899 | * @since 3.10 |
| 900 | */ |
| 901 | public SimpleName getReceiverQualifier() { |
| 902 | unsupportedIn2_3_4(); |
| 903 | return this.optionalReceiverQualifier; |
| 904 | } |
| 905 | |
| 906 | /** |
| 907 | * Sets the given simple name as the qualifier for the receiver (added in JLS8 |
| 908 | * API). |
| 909 | * |
| 910 | * @param receiverQualifier explicit receiver parameter to be added to the |
| 911 | * method declaration |
| 912 | * @exception UnsupportedOperationException if this operation is used below JLS8 |
| 913 | * @since 3.10 |
| 914 | */ |
| 915 | public void setReceiverQualifier(SimpleName receiverQualifier) { |
| 916 | unsupportedIn2_3_4(); |
| 917 | ASTNode oldChild = this.optionalReceiverQualifier; |
| 918 | preReplaceChild(oldChild, receiverQualifier, RECEIVER_QUALIFIER_PROPERTY); |
| 919 | this.optionalReceiverQualifier = receiverQualifier; |
| 920 | postReplaceChild(oldChild, receiverQualifier, RECEIVER_QUALIFIER_PROPERTY); |
| 921 | } |
| 922 | |
| 923 | /** |
| 924 | * Returns the live ordered list of method parameter declarations for this |
| 925 | * method declaration. |
| 926 | * |
| 927 | * @return the live list of method parameter declarations |
| 928 | * (element type: {@link SingleVariableDeclaration}) |
| 929 | */ |
| 930 | public List parameters() { |
| 931 | return this.parameters; |
| 932 | } |
| 933 | |
| 934 | /** |
| 935 | * Returns whether this method declaration declares a |
| 936 | * variable arity method (added in JLS3 API). The convenience method checks |
| 937 | * whether the last parameter is so marked. |
| 938 | * |
| 939 | * @return <code>true</code> if this is a variable arity method declaration, |
| 940 | * and <code>false</code> otherwise |
| 941 | * @exception UnsupportedOperationException if this operation is used in |
| 942 | * a JLS2 AST |
| 943 | * @see SingleVariableDeclaration#isVarargs() |
| 944 | * @since 3.1 |
| 945 | */ |
| 946 | public boolean isVarargs() { |
| 947 | // more efficient than just calling unsupportedIn2() to check |
| 948 | if (this.modifiers == null) { |
| 949 | unsupportedIn2(); |
| 950 | } |
| 951 | if (parameters().isEmpty()) { |
| 952 | return false; |
| 953 | } else { |
| 954 | SingleVariableDeclaration v = (SingleVariableDeclaration) parameters().get(parameters().size() - 1); |
| 955 | return v.isVarargs(); |
| 956 | } |
| 957 | } |
| 958 | |
| 959 | /** |
| 960 | * Returns the live ordered list of thrown exception names in this method |
| 961 | * declaration (below JLS8 API only). |
| 962 | * |
| 963 | * @return the live list of exception names |
| 964 | * (element type: {@link Name}) |
| 965 | * @exception UnsupportedOperationException if this operation is used in |
| 966 | * a JLS8 or later AST |
| 967 | * @deprecated In the JLS8 API, this method is replaced by |
| 968 | * {@link #thrownExceptionTypes()}. |
| 969 | */ |
| 970 | public List thrownExceptions() { |
| 971 | return internalThrownExceptions(); |
| 972 | } |
| 973 | |
| 974 | /** |
| 975 | * Internal synonym for deprecated method. Used to avoid |
| 976 | * deprecation warnings. |
| 977 | * |
| 978 | * @exception UnsupportedOperationException if this operation is used in |
| 979 | * a JLS8 or later AST |
| 980 | * @since 3.10 |
| 981 | */ |
| 982 | /* package */ List internalThrownExceptions() { |
| 983 | // more efficient than just calling supportedOnlyIn2_3_4() to check |
| 984 | if (this.thrownExceptions == null) { |
| 985 | supportedOnlyIn2_3_4(); |
| 986 | } |
| 987 | return this.thrownExceptions; |
| 988 | } |
| 989 | |
| 990 | /** |
| 991 | * Returns the live ordered list of thrown exception types in this method |
| 992 | * declaration. |
| 993 | * |
| 994 | * @return the live list of exception types |
| 995 | * (element type: {@link Type}) |
| 996 | * @exception UnsupportedOperationException if this operation is used |
| 997 | * in a JLS2, JLS3 or JLS4 AST |
| 998 | * @since 3.10 |
| 999 | */ |
| 1000 | public List thrownExceptionTypes() { |
| 1001 | if (this.thrownExceptionTypes == null) { |
| 1002 | unsupportedIn2_3_4(); |
| 1003 | } |
| 1004 | return this.thrownExceptionTypes; |
| 1005 | } |
| 1006 | |
| 1007 | /** |
| 1008 | * Returns the return type of the method declared in this method |
| 1009 | * declaration, exclusive of any extra array dimensions (JLS2 API only). |
| 1010 | * This is one of the few places where the void type is meaningful. |
| 1011 | * <p> |
| 1012 | * Note that this child is not relevant for constructor declarations |
| 1013 | * (although, it does still figure in subtree equality comparisons |
| 1014 | * and visits), and is devoid of the binding information ordinarily |
| 1015 | * available. |
| 1016 | * </p> |
| 1017 | * |
| 1018 | * @return the return type, possibly the void primitive type |
| 1019 | * @exception UnsupportedOperationException if this operation is used in |
| 1020 | * an AST later than JLS2 |
| 1021 | * @deprecated In the JLS3 API, this method is replaced by |
| 1022 | * {@link #getReturnType2()}, |
| 1023 | * which may return <code>null</code>. |
| 1024 | */ |
| 1025 | public Type getReturnType() { |
| 1026 | return internalGetReturnType(); |
| 1027 | } |
| 1028 | |
| 1029 | /** |
| 1030 | * Internal synonym for deprecated method. Used to avoid |
| 1031 | * deprecation warnings. |
| 1032 | * |
| 1033 | * @exception UnsupportedOperationException if this operation is used in |
| 1034 | * an AST later than JLS2 |
| 1035 | * @since 3.1 |
| 1036 | */ |
| 1037 | /* package */ final Type internalGetReturnType() { |
| 1038 | supportedOnlyIn2(); |
| 1039 | if (this.returnType == null) { |
| 1040 | // lazy init must be thread-safe for readers |
| 1041 | synchronized (this) { |
| 1042 | if (this.returnType == null) { |
| 1043 | preLazyInit(); |
| 1044 | this.returnType = this.ast.newPrimitiveType(PrimitiveType.VOID); |
| 1045 | postLazyInit(this.returnType, RETURN_TYPE_PROPERTY); |
| 1046 | } |
| 1047 | } |
| 1048 | } |
| 1049 | return this.returnType; |
| 1050 | } |
| 1051 | |
| 1052 | /** |
| 1053 | * Sets the return type of the method declared in this method declaration |
| 1054 | * to the given type, exclusive of any extra array dimensions (JLS2 API only). |
| 1055 | * This is one |
| 1056 | * of the few places where the void type is meaningful. |
| 1057 | * <p> |
| 1058 | * Note that this child is not relevant for constructor declarations |
| 1059 | * (although it does still figure in subtree equality comparisons and visits). |
| 1060 | * </p> |
| 1061 | * |
| 1062 | * @param type the new return type, possibly the void primitive type |
| 1063 | * @exception IllegalArgumentException if: |
| 1064 | * <ul> |
| 1065 | * <li>the node belongs to a different |
| 1066 | * AST</li> |
| 1067 | * <li>the node already has a |
| 1068 | * parent</li> |
| 1069 | * </ul> |
| 1070 | * @exception UnsupportedOperationException if this operation is used in |
| 1071 | * an AST later than JLS2 |
| 1072 | * @deprecated In the JLS3 API, this method is replaced by |
| 1073 | * {@link #setReturnType2(Type)}, which accepts <code>null</code>. |
| 1074 | */ |
| 1075 | public void setReturnType(Type type) { |
| 1076 | internalSetReturnType(type); |
| 1077 | } |
| 1078 | |
| 1079 | /** |
| 1080 | * Internal synonym for deprecated method. Used to avoid |
| 1081 | * deprecation warnings. |
| 1082 | * |
| 1083 | * @since 3.1 |
| 1084 | */ |
| 1085 | /* package */ void internalSetReturnType(Type type) { |
| 1086 | supportedOnlyIn2(); |
| 1087 | if (type == null) { |
| 1088 | throw new IllegalArgumentException(); |
| 1089 | } |
| 1090 | ASTNode oldChild = this.returnType; |
| 1091 | preReplaceChild(oldChild, type, RETURN_TYPE_PROPERTY); |
| 1092 | this.returnType = type; |
| 1093 | postReplaceChild(oldChild, type, RETURN_TYPE_PROPERTY); |
| 1094 | } |
| 1095 | |
| 1096 | /** |
| 1097 | * Returns the return type of the method declared in this method |
| 1098 | * declaration, exclusive of any extra array dimensions (added in JLS3 API). |
| 1099 | * This is one of the few places where the void type is meaningful. |
| 1100 | * <p> |
| 1101 | * Note that this child is not relevant for constructor declarations |
| 1102 | * (although, if present, it does still figure in subtree equality comparisons |
| 1103 | * and visits), and is devoid of the binding information ordinarily |
| 1104 | * available. In the JLS2 API, the return type is mandatory. |
| 1105 | * In the JLS3 API, the return type is optional. |
| 1106 | * </p> |
| 1107 | * |
| 1108 | * @return the return type, possibly the void primitive type, |
| 1109 | * or <code>null</code> if none |
| 1110 | * @exception UnsupportedOperationException if this operation is used in |
| 1111 | * a JLS2 AST |
| 1112 | * @since 3.1 |
| 1113 | */ |
| 1114 | public Type getReturnType2() { |
| 1115 | unsupportedIn2(); |
| 1116 | if (this.returnType == null && !this.returnType2Initialized) { |
| 1117 | // lazy init must be thread-safe for readers |
| 1118 | synchronized (this) { |
| 1119 | if (this.returnType == null && !this.returnType2Initialized) { |
| 1120 | preLazyInit(); |
| 1121 | this.returnType = this.ast.newPrimitiveType(PrimitiveType.VOID); |
| 1122 | this.returnType2Initialized = true; |
| 1123 | postLazyInit(this.returnType, RETURN_TYPE2_PROPERTY); |
| 1124 | } |
| 1125 | } |
| 1126 | } |
| 1127 | return this.returnType; |
| 1128 | } |
| 1129 | |
| 1130 | /** |
| 1131 | * Sets the return type of the method declared in this method declaration |
| 1132 | * to the given type, exclusive of any extra array dimensions (added in JLS3 |
| 1133 | * API). |
| 1134 | * This is one of the few places where the void type is meaningful. |
| 1135 | * <p> |
| 1136 | * Note that this child is not relevant for constructor declarations |
| 1137 | * (although it does still figure in subtree equality comparisons and visits). |
| 1138 | * In the JLS2 API, the return type is mandatory. |
| 1139 | * In the JLS3 API, the return type is optional. |
| 1140 | * </p> |
| 1141 | * |
| 1142 | * @param type the new return type, possibly the void primitive type, |
| 1143 | * or <code>null</code> if none |
| 1144 | * @exception UnsupportedOperationException if this operation is used in |
| 1145 | * a JLS2 AST |
| 1146 | * @exception IllegalArgumentException if: |
| 1147 | * <ul> |
| 1148 | * <li>the node belongs to a different |
| 1149 | * AST</li> |
| 1150 | * <li>the node already has a |
| 1151 | * parent</li> |
| 1152 | * </ul> |
| 1153 | * @since 3.1 |
| 1154 | */ |
| 1155 | public void setReturnType2(Type type) { |
| 1156 | unsupportedIn2(); |
| 1157 | this.returnType2Initialized = true; |
| 1158 | ASTNode oldChild = this.returnType; |
| 1159 | preReplaceChild(oldChild, type, RETURN_TYPE2_PROPERTY); |
| 1160 | this.returnType = type; |
| 1161 | postReplaceChild(oldChild, type, RETURN_TYPE2_PROPERTY); |
| 1162 | } |
| 1163 | |
| 1164 | /** |
| 1165 | * Returns the number of extra array dimensions over and above the |
| 1166 | * explicitly-specified return type. |
| 1167 | * <p> |
| 1168 | * For example, <code>int foo()[][]</code> has a return type of |
| 1169 | * <code>int</code> and two extra array dimensions; |
| 1170 | * <code>int[][] foo()</code> has a return type of <code>int[][]</code> |
| 1171 | * and zero extra array dimensions. The two constructs have different |
| 1172 | * ASTs, even though there are really syntactic variants of the same |
| 1173 | * method declaration. |
| 1174 | * </p> |
| 1175 | * <p> |
| 1176 | * In the JLS8 API, this method is a convenience method that |
| 1177 | * counts {@link #extraDimensions()}. |
| 1178 | * </p> |
| 1179 | * |
| 1180 | * @return the number of extra array dimensions |
| 1181 | * @since 2.1 |
| 1182 | */ |
| 1183 | public int getExtraDimensions() { |
| 1184 | // more efficient than checking getAST().API_LEVEL |
| 1185 | if (this.extraDimensions == null) { |
| 1186 | // JLS2,3,4 behavior - bona fide property |
| 1187 | return this.extraArrayDimensions; |
| 1188 | } else { |
| 1189 | return this.extraDimensions.size(); |
| 1190 | } |
| 1191 | } |
| 1192 | |
| 1193 | /** |
| 1194 | * Sets the number of extra array dimensions over and above the |
| 1195 | * explicitly-specified return type. |
| 1196 | * <p> |
| 1197 | * For example, <code>int foo()[][]</code> is rendered as a return |
| 1198 | * type of <code>int</code> with two extra array dimensions; |
| 1199 | * <code>int[][] foo()</code> is rendered as a return type of |
| 1200 | * <code>int[][]</code> with zero extra array dimensions. The two |
| 1201 | * constructs have different ASTs, even though there are really syntactic |
| 1202 | * variants of the same method declaration. |
| 1203 | * </p> |
| 1204 | * |
| 1205 | * @param dimensions the number of array dimensions |
| 1206 | * @exception IllegalArgumentException if the number of dimensions is |
| 1207 | * negative |
| 1208 | * @exception UnsupportedOperationException if this operation is used in |
| 1209 | * a JLS8 or later AST |
| 1210 | * @since 2.1 |
| 1211 | * @deprecated In the JLS8 API, this method is replaced by |
| 1212 | * {@link #extraDimensions()} which contains a list of |
| 1213 | * {@link Dimension} nodes. |
| 1214 | */ |
| 1215 | public void setExtraDimensions(int dimensions) { |
| 1216 | // more efficient than just calling supportedOnlyIn2_3_4() to check |
| 1217 | if (this.extraDimensions != null) { |
| 1218 | supportedOnlyIn2_3_4(); |
| 1219 | } |
| 1220 | if (dimensions < 0) { |
| 1221 | throw new IllegalArgumentException(); |
| 1222 | } |
| 1223 | preValueChange(EXTRA_DIMENSIONS_PROPERTY); |
| 1224 | this.extraArrayDimensions = dimensions; |
| 1225 | postValueChange(EXTRA_DIMENSIONS_PROPERTY); |
| 1226 | } |
| 1227 | |
| 1228 | /** |
| 1229 | * Returns the live ordered list of extra dimensions with optional annotations |
| 1230 | * (added in JLS8 API). |
| 1231 | * |
| 1232 | * @return the live list of extra dimensions with optional annotations (element |
| 1233 | * type: {@link Dimension}) |
| 1234 | * @exception UnsupportedOperationException if this operation is used below JLS8 |
| 1235 | * @since 3.10 |
| 1236 | */ |
| 1237 | public List extraDimensions() { |
| 1238 | // more efficient than just calling unsupportedIn2_3_4() to check |
| 1239 | if (this.extraDimensions == null) { |
| 1240 | unsupportedIn2_3_4(); |
| 1241 | } |
| 1242 | return this.extraDimensions; |
| 1243 | } |
| 1244 | |
| 1245 | /** |
| 1246 | * Returns the body of this method declaration, or <code>null</code> if |
| 1247 | * this method has <b>no</b> body. |
| 1248 | * <p> |
| 1249 | * Note that there is a subtle difference between having no body and having |
| 1250 | * an empty body ("{}"). |
| 1251 | * </p> |
| 1252 | * |
| 1253 | * @return the method body, or <code>null</code> if this method has no |
| 1254 | * body |
| 1255 | */ |
| 1256 | public Block getBody() { |
| 1257 | return this.optionalBody; |
| 1258 | } |
| 1259 | |
| 1260 | /** |
| 1261 | * Sets or clears the body of this method declaration. |
| 1262 | * <p> |
| 1263 | * Note that there is a subtle difference between having no body |
| 1264 | * (as in <code>"void foo();"</code>) and having an empty body (as in |
| 1265 | * "void foo() {}"). Abstract methods, and methods declared in interfaces, |
| 1266 | * have no body. Non-abstract methods, and all constructors, have a body. |
| 1267 | * </p> |
| 1268 | * |
| 1269 | * @param body the block node, or <code>null</code> if |
| 1270 | * there is none |
| 1271 | * @exception IllegalArgumentException if: |
| 1272 | * <ul> |
| 1273 | * <li>the node belongs to a different |
| 1274 | * AST</li> |
| 1275 | * <li>the node already has a parent</li> |
| 1276 | * <li>a cycle in would be created</li> |
| 1277 | * </ul> |
| 1278 | */ |
| 1279 | public void setBody(Block body) { |
| 1280 | // a MethodDeclaration may occur in a Block - must check cycles |
| 1281 | ASTNode oldChild = this.optionalBody; |
| 1282 | preReplaceChild(oldChild, body, BODY_PROPERTY); |
| 1283 | this.optionalBody = body; |
| 1284 | postReplaceChild(oldChild, body, BODY_PROPERTY); |
| 1285 | } |
| 1286 | |
| 1287 | /** |
| 1288 | * Resolves and returns the binding for the method or constructor declared |
| 1289 | * in this method or constructor declaration. |
| 1290 | * <p> |
| 1291 | * Note that bindings are generally unavailable unless requested when the |
| 1292 | * AST is being built. |
| 1293 | * </p> |
| 1294 | * |
| 1295 | * @return the binding, or <code>null</code> if the binding cannot be |
| 1296 | * resolved |
| 1297 | */ |
| 1298 | public IMethodBinding resolveBinding() { |
| 1299 | return this.ast.getBindingResolver().resolveMethod(this); |
| 1300 | } |
| 1301 | |
| 1302 | @Override |
| 1303 | int memSize() { |
| 1304 | return super.memSize() + 13 * 4; |
| 1305 | } |
| 1306 | |
| 1307 | @Override |
| 1308 | int treeSize() { |
| 1309 | return memSize() |
| 1310 | + (this.optionalDocComment == null ? 0 : getJavadoc().treeSize()) |
| 1311 | + (this.modifiers == null ? 0 : this.modifiers.listSize()) |
| 1312 | + (this.typeParameters == null ? 0 : this.typeParameters.listSize()) |
| 1313 | + (this.methodName == null ? 0 : getName().treeSize()) |
| 1314 | + (this.optionalReceiverType == null ? 0 : this.optionalReceiverType.treeSize()) |
| 1315 | + (this.optionalReceiverQualifier == null ? 0 : this.optionalReceiverQualifier.treeSize()) |
| 1316 | + (this.returnType == null ? 0 : this.returnType.treeSize()) |
| 1317 | + this.parameters.listSize() |
| 1318 | + (this.ast.apiLevel < AST.JLS8_INTERNAL |
| 1319 | ? this.thrownExceptions.listSize() |
| 1320 | : this.extraDimensions.listSize() + this.thrownExceptionTypes.listSize()) |
| 1321 | + (this.optionalBody == null ? 0 : getBody().treeSize()); |
| 1322 | } |
| 1323 | } |
| 1324 |
Members