| 1 | /******************************************************************************* |
|---|---|
| 2 | * Copyright (c) 2000, 2014 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.Iterator; |
| 19 | import java.util.List; |
| 20 | |
| 21 | /** |
| 22 | * Single variable declaration AST node type. Single variable |
| 23 | * declaration nodes are used in a limited number of places, including formal |
| 24 | * parameter lists and catch clauses. They are not used for field declarations |
| 25 | * and regular variable declaration statements. |
| 26 | * <pre> |
| 27 | * SingleVariableDeclaration: |
| 28 | * { ExtendedModifier } Type {Annotation} [ <b>...</b> ] Identifier { Dimension } [ <b>=</b> Expression ] |
| 29 | * </pre> |
| 30 | * <p> |
| 31 | * Note: There's currently no construct in the Java language that allows an initializer on a SingleVariableDeclaration. |
| 32 | * </p> |
| 33 | * |
| 34 | * @since 2.0 |
| 35 | * @noinstantiate This class is not intended to be instantiated by clients. |
| 36 | */ |
| 37 | @SuppressWarnings({"rawtypes", "unchecked"}) |
| 38 | public class SingleVariableDeclaration extends VariableDeclaration { |
| 39 | |
| 40 | /** |
| 41 | * The "modifiers" structural property of this node type (type: {@link Integer}) (JLS2 API only). |
| 42 | * @deprecated In the JLS3 API, this property is replaced by {@link #MODIFIERS2_PROPERTY}. |
| 43 | * @since 3.0 |
| 44 | */ |
| 45 | public static final SimplePropertyDescriptor MODIFIERS_PROPERTY = |
| 46 | new SimplePropertyDescriptor(SingleVariableDeclaration.class, "modifiers", int.class, MANDATORY); //$NON-NLS-1$ |
| 47 | |
| 48 | /** |
| 49 | * The "modifiers" structural property of this node type (element type: {@link IExtendedModifier}) (added in JLS3 API). |
| 50 | * @since 3.1 |
| 51 | */ |
| 52 | public static final ChildListPropertyDescriptor MODIFIERS2_PROPERTY = |
| 53 | new ChildListPropertyDescriptor(SingleVariableDeclaration.class, "modifiers", IExtendedModifier.class, CYCLE_RISK); //$NON-NLS-1$ |
| 54 | |
| 55 | /** |
| 56 | * The "type" structural property of this node type (child type: {@link Type}). |
| 57 | * @since 3.0 |
| 58 | */ |
| 59 | public static final ChildPropertyDescriptor TYPE_PROPERTY = |
| 60 | new ChildPropertyDescriptor(SingleVariableDeclaration.class, "type", Type.class, MANDATORY, NO_CYCLE_RISK); //$NON-NLS-1$ |
| 61 | |
| 62 | /** |
| 63 | * The "varargsAnnotations" structural property of variable arguments of this node type (element type: {@link Annotation}) |
| 64 | * (added in JLS8 API). |
| 65 | * @since 3.10 |
| 66 | */ |
| 67 | public static final ChildListPropertyDescriptor VARARGS_ANNOTATIONS_PROPERTY = |
| 68 | new ChildListPropertyDescriptor(SingleVariableDeclaration.class, "varargsAnnotations", Annotation.class, CYCLE_RISK); //$NON-NLS-1$ |
| 69 | |
| 70 | /** |
| 71 | * The "varargs" structural property of this node type (type: {@link Boolean}) (added in JLS3 API). |
| 72 | * @since 3.1 |
| 73 | */ |
| 74 | public static final SimplePropertyDescriptor VARARGS_PROPERTY = |
| 75 | new SimplePropertyDescriptor(SingleVariableDeclaration.class, "varargs", boolean.class, MANDATORY); //$NON-NLS-1$ |
| 76 | |
| 77 | /** |
| 78 | * The "name" structural property of this node type (child type: {@link SimpleName}). |
| 79 | * @since 3.0 |
| 80 | */ |
| 81 | public static final ChildPropertyDescriptor NAME_PROPERTY = |
| 82 | internalNamePropertyFactory(SingleVariableDeclaration.class); |
| 83 | |
| 84 | /** |
| 85 | * The "extraDimensions" structural property of this node type (type: {@link Integer}) |
| 86 | * (before JLS8 only). |
| 87 | * |
| 88 | * @since 3.0 |
| 89 | * @deprecated In JLS8 and later, use {@link SingleVariableDeclaration#EXTRA_DIMENSIONS2_PROPERTY} instead. |
| 90 | */ |
| 91 | public static final SimplePropertyDescriptor EXTRA_DIMENSIONS_PROPERTY = |
| 92 | internalExtraDimensionsPropertyFactory(SingleVariableDeclaration.class); |
| 93 | |
| 94 | /** |
| 95 | * The "extraDimensions2" structural property of this node type (element type: {@link Dimension}) (added in JLS8 API). |
| 96 | * @since 3.10 |
| 97 | */ |
| 98 | public static final ChildListPropertyDescriptor EXTRA_DIMENSIONS2_PROPERTY = |
| 99 | internalExtraDimensions2PropertyFactory(SingleVariableDeclaration.class); |
| 100 | |
| 101 | /** |
| 102 | * The "initializer" structural property of this node type (child type: {@link Expression}). |
| 103 | * @since 3.0 |
| 104 | */ |
| 105 | public static final ChildPropertyDescriptor INITIALIZER_PROPERTY = |
| 106 | internalInitializerPropertyFactory(SingleVariableDeclaration.class); |
| 107 | |
| 108 | /** |
| 109 | * A list of property descriptors (element type: |
| 110 | * {@link StructuralPropertyDescriptor}), |
| 111 | * or null if uninitialized. |
| 112 | * @since 3.0 |
| 113 | */ |
| 114 | private static final List PROPERTY_DESCRIPTORS_2_0; |
| 115 | |
| 116 | /** |
| 117 | * A list of property descriptors (element type: |
| 118 | * {@link StructuralPropertyDescriptor}), |
| 119 | * or null if uninitialized. |
| 120 | * @since 3.1 |
| 121 | */ |
| 122 | private static final List PROPERTY_DESCRIPTORS_3_0; |
| 123 | |
| 124 | /** |
| 125 | * A list of property descriptors (element type: |
| 126 | * {@link StructuralPropertyDescriptor}), |
| 127 | * or null if uninitialized. |
| 128 | * @since 3.10 |
| 129 | */ |
| 130 | private static final List PROPERTY_DESCRIPTORS_8_0; |
| 131 | |
| 132 | static { |
| 133 | List propertyList = new ArrayList(6); |
| 134 | createPropertyList(SingleVariableDeclaration.class, propertyList); |
| 135 | addProperty(MODIFIERS_PROPERTY, propertyList); |
| 136 | addProperty(TYPE_PROPERTY, propertyList); |
| 137 | addProperty(NAME_PROPERTY, propertyList); |
| 138 | addProperty(EXTRA_DIMENSIONS_PROPERTY, propertyList); |
| 139 | addProperty(INITIALIZER_PROPERTY, propertyList); |
| 140 | PROPERTY_DESCRIPTORS_2_0 = reapPropertyList(propertyList); |
| 141 | |
| 142 | propertyList = new ArrayList(7); |
| 143 | createPropertyList(SingleVariableDeclaration.class, propertyList); |
| 144 | addProperty(MODIFIERS2_PROPERTY, propertyList); |
| 145 | addProperty(TYPE_PROPERTY, propertyList); |
| 146 | addProperty(VARARGS_PROPERTY, propertyList); |
| 147 | addProperty(NAME_PROPERTY, propertyList); |
| 148 | addProperty(EXTRA_DIMENSIONS_PROPERTY, propertyList); |
| 149 | addProperty(INITIALIZER_PROPERTY, propertyList); |
| 150 | PROPERTY_DESCRIPTORS_3_0 = reapPropertyList(propertyList); |
| 151 | |
| 152 | propertyList = new ArrayList(8); |
| 153 | createPropertyList(SingleVariableDeclaration.class, propertyList); |
| 154 | addProperty(MODIFIERS2_PROPERTY, propertyList); |
| 155 | addProperty(TYPE_PROPERTY, propertyList); |
| 156 | addProperty(VARARGS_ANNOTATIONS_PROPERTY, propertyList); |
| 157 | addProperty(VARARGS_PROPERTY, propertyList); |
| 158 | addProperty(NAME_PROPERTY, propertyList); |
| 159 | addProperty(EXTRA_DIMENSIONS2_PROPERTY, propertyList); |
| 160 | addProperty(INITIALIZER_PROPERTY, propertyList); |
| 161 | PROPERTY_DESCRIPTORS_8_0 = reapPropertyList(propertyList); |
| 162 | } |
| 163 | |
| 164 | /** |
| 165 | * Returns a list of structural property descriptors for this node type. |
| 166 | * Clients must not modify the result. |
| 167 | * |
| 168 | * @param apiLevel the API level; one of the |
| 169 | * <code>AST.JLS*</code> constants |
| 170 | * @return a list of property descriptors (element type: |
| 171 | * {@link StructuralPropertyDescriptor}) |
| 172 | * @since 3.0 |
| 173 | */ |
| 174 | public static List propertyDescriptors(int apiLevel) { |
| 175 | if (apiLevel == AST.JLS2_INTERNAL) { |
| 176 | return PROPERTY_DESCRIPTORS_2_0; |
| 177 | } else if (apiLevel < AST.JLS8_INTERNAL) { |
| 178 | return PROPERTY_DESCRIPTORS_3_0; |
| 179 | } else { |
| 180 | return PROPERTY_DESCRIPTORS_8_0; |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | /** |
| 185 | * The extended modifiers (element type: {@link IExtendedModifier}). |
| 186 | * Null in JLS2. Added in JLS3; defaults to an empty list |
| 187 | * (see constructor). |
| 188 | * |
| 189 | * @since 3.1 |
| 190 | */ |
| 191 | private ASTNode.NodeList modifiers = null; |
| 192 | |
| 193 | /** |
| 194 | * The modifiers; bit-wise or of Modifier flags. |
| 195 | * Defaults to none. Not used in 3.0. |
| 196 | */ |
| 197 | private int modifierFlags = Modifier.NONE; |
| 198 | |
| 199 | /** |
| 200 | * The type; lazily initialized; defaults to an unspecified, |
| 201 | * legal type. |
| 202 | */ |
| 203 | private Type type = null; |
| 204 | |
| 205 | /** |
| 206 | * The type annotations on the varargs token (element type: {@link Annotation}). |
| 207 | * Null before JLS8. Added in JLS8; defaults to an empty list |
| 208 | * (see constructor). |
| 209 | * |
| 210 | * @since 3.10 |
| 211 | */ |
| 212 | private ASTNode.NodeList varargsAnnotations = null; |
| 213 | |
| 214 | /** |
| 215 | * Indicates the last parameter of a variable arity method; |
| 216 | * defaults to false. |
| 217 | * |
| 218 | * @since 3.1 |
| 219 | */ |
| 220 | private boolean variableArity = false; |
| 221 | |
| 222 | /** |
| 223 | * Creates a new AST node for a variable declaration owned by the given |
| 224 | * AST. By default, the variable declaration has: no modifiers, an |
| 225 | * unspecified (but legal) type, an unspecified (but legal) variable name, |
| 226 | * 0 dimensions after the variable; no initializer; not variable arity. |
| 227 | * <p> |
| 228 | * N.B. This constructor is package-private. |
| 229 | * </p> |
| 230 | * |
| 231 | * @param ast the AST that is to own this node |
| 232 | */ |
| 233 | SingleVariableDeclaration(AST ast) { |
| 234 | super(ast); |
| 235 | if (ast.apiLevel >= AST.JLS3_INTERNAL) { |
| 236 | this.modifiers = new ASTNode.NodeList(MODIFIERS2_PROPERTY); |
| 237 | if (ast.apiLevel >= AST.JLS8_INTERNAL) { |
| 238 | this.varargsAnnotations = new ASTNode.NodeList(VARARGS_ANNOTATIONS_PROPERTY); |
| 239 | } |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | @Override |
| 244 | final ChildPropertyDescriptor internalNameProperty() { |
| 245 | return NAME_PROPERTY; |
| 246 | } |
| 247 | |
| 248 | @Override |
| 249 | final SimplePropertyDescriptor internalExtraDimensionsProperty() { |
| 250 | return EXTRA_DIMENSIONS_PROPERTY; |
| 251 | } |
| 252 | |
| 253 | @Override |
| 254 | final ChildListPropertyDescriptor internalExtraDimensions2Property() { |
| 255 | return EXTRA_DIMENSIONS2_PROPERTY; |
| 256 | } |
| 257 | |
| 258 | @Override |
| 259 | final ChildPropertyDescriptor internalInitializerProperty() { |
| 260 | return INITIALIZER_PROPERTY; |
| 261 | } |
| 262 | |
| 263 | @Override |
| 264 | final List internalStructuralPropertiesForType(int apiLevel) { |
| 265 | return propertyDescriptors(apiLevel); |
| 266 | } |
| 267 | |
| 268 | @Override |
| 269 | final int internalGetSetIntProperty(SimplePropertyDescriptor property, boolean get, int value) { |
| 270 | if (property == MODIFIERS_PROPERTY) { |
| 271 | if (get) { |
| 272 | return getModifiers(); |
| 273 | } else { |
| 274 | setModifiers(value); |
| 275 | return 0; |
| 276 | } |
| 277 | } |
| 278 | if (property == EXTRA_DIMENSIONS_PROPERTY) { |
| 279 | if (get) { |
| 280 | return getExtraDimensions(); |
| 281 | } else { |
| 282 | internalSetExtraDimensions(value); |
| 283 | return 0; |
| 284 | } |
| 285 | } |
| 286 | // allow default implementation to flag the error |
| 287 | return super.internalGetSetIntProperty(property, get, value); |
| 288 | } |
| 289 | |
| 290 | @Override |
| 291 | final boolean internalGetSetBooleanProperty(SimplePropertyDescriptor property, boolean get, boolean value) { |
| 292 | if (property == VARARGS_PROPERTY) { |
| 293 | if (get) { |
| 294 | return isVarargs(); |
| 295 | } else { |
| 296 | setVarargs(value); |
| 297 | return false; |
| 298 | } |
| 299 | } |
| 300 | // allow default implementation to flag the error |
| 301 | return super.internalGetSetBooleanProperty(property, get, value); |
| 302 | } |
| 303 | |
| 304 | @Override |
| 305 | final ASTNode internalGetSetChildProperty(ChildPropertyDescriptor property, boolean get, ASTNode child) { |
| 306 | if (property == TYPE_PROPERTY) { |
| 307 | if (get) { |
| 308 | return getType(); |
| 309 | } else { |
| 310 | setType((Type) child); |
| 311 | return null; |
| 312 | } |
| 313 | } |
| 314 | if (property == NAME_PROPERTY) { |
| 315 | if (get) { |
| 316 | return getName(); |
| 317 | } else { |
| 318 | setName((SimpleName) child); |
| 319 | return null; |
| 320 | } |
| 321 | } |
| 322 | if (property == INITIALIZER_PROPERTY) { |
| 323 | if (get) { |
| 324 | return getInitializer(); |
| 325 | } else { |
| 326 | setInitializer((Expression) child); |
| 327 | return null; |
| 328 | } |
| 329 | } |
| 330 | // allow default implementation to flag the error |
| 331 | return super.internalGetSetChildProperty(property, get, child); |
| 332 | } |
| 333 | |
| 334 | @Override |
| 335 | final List internalGetChildListProperty(ChildListPropertyDescriptor property) { |
| 336 | if (property == MODIFIERS2_PROPERTY) { |
| 337 | return modifiers(); |
| 338 | } |
| 339 | if (property == VARARGS_ANNOTATIONS_PROPERTY) { |
| 340 | return varargsAnnotations(); |
| 341 | } |
| 342 | if (property == EXTRA_DIMENSIONS2_PROPERTY) { |
| 343 | return extraDimensions(); |
| 344 | } |
| 345 | // allow default implementation to flag the error |
| 346 | return super.internalGetChildListProperty(property); |
| 347 | } |
| 348 | |
| 349 | @Override |
| 350 | final int getNodeType0() { |
| 351 | return SINGLE_VARIABLE_DECLARATION; |
| 352 | } |
| 353 | |
| 354 | @Override |
| 355 | ASTNode clone0(AST target) { |
| 356 | SingleVariableDeclaration result = new SingleVariableDeclaration(target); |
| 357 | result.setSourceRange(getStartPosition(), getLength()); |
| 358 | if (this.ast.apiLevel == AST.JLS2_INTERNAL) { |
| 359 | result.setModifiers(getModifiers()); |
| 360 | } else { |
| 361 | result.modifiers().addAll(ASTNode.copySubtrees(target, modifiers())); |
| 362 | result.setVarargs(isVarargs()); |
| 363 | } |
| 364 | result.setType((Type) getType().clone(target)); |
| 365 | if (this.ast.apiLevel >= AST.JLS8_INTERNAL) { |
| 366 | result.varargsAnnotations().addAll( |
| 367 | ASTNode.copySubtrees(target, varargsAnnotations())); |
| 368 | } |
| 369 | result.setName((SimpleName) getName().clone(target)); |
| 370 | if (this.ast.apiLevel >= AST.JLS8_INTERNAL) { |
| 371 | result.extraDimensions().addAll( |
| 372 | ASTNode.copySubtrees(target, this.extraDimensions())); |
| 373 | } else { |
| 374 | result.internalSetExtraDimensions(getExtraDimensions()); |
| 375 | } |
| 376 | result.setInitializer( |
| 377 | (Expression) ASTNode.copySubtree(target, getInitializer())); |
| 378 | return result; |
| 379 | } |
| 380 | |
| 381 | @Override |
| 382 | final boolean subtreeMatch0(ASTMatcher matcher, Object other) { |
| 383 | // dispatch to correct overloaded match method |
| 384 | return matcher.match(this, other); |
| 385 | } |
| 386 | |
| 387 | @Override |
| 388 | void accept0(ASTVisitor visitor) { |
| 389 | boolean visitChildren = visitor.visit(this); |
| 390 | if (visitChildren) { |
| 391 | // visit children in normal left to right reading order |
| 392 | if (this.ast.apiLevel >= AST.JLS3_INTERNAL) { |
| 393 | acceptChildren(visitor, this.modifiers); |
| 394 | } |
| 395 | acceptChild(visitor, getType()); |
| 396 | if (this.ast.apiLevel >= AST.JLS8_INTERNAL && isVarargs()) { |
| 397 | acceptChildren(visitor, this.varargsAnnotations); |
| 398 | } |
| 399 | acceptChild(visitor, getName()); |
| 400 | if (this.ast.apiLevel >= AST.JLS8_INTERNAL){ |
| 401 | acceptChildren(visitor, this.extraDimensions); |
| 402 | } |
| 403 | acceptChild(visitor, getInitializer()); |
| 404 | } |
| 405 | visitor.endVisit(this); |
| 406 | } |
| 407 | |
| 408 | /** |
| 409 | * Returns the live ordered list of modifiers and annotations |
| 410 | * of this declaration (added in JLS3 API). |
| 411 | * <p> |
| 412 | * Note that the final modifier is the only meaningful modifier for local |
| 413 | * variable and formal parameter declarations. |
| 414 | * </p> |
| 415 | * |
| 416 | * @return the live list of modifiers and annotations |
| 417 | * (element type: {@link IExtendedModifier}) |
| 418 | * @exception UnsupportedOperationException if this operation is used in |
| 419 | * a JLS2 AST |
| 420 | * @since 3.1 |
| 421 | */ |
| 422 | public List modifiers() { |
| 423 | // more efficient than just calling unsupportedIn2() to check |
| 424 | if (this.modifiers == null) { |
| 425 | unsupportedIn2(); |
| 426 | } |
| 427 | return this.modifiers; |
| 428 | } |
| 429 | |
| 430 | /** |
| 431 | * Returns the modifiers explicitly specified on this declaration. |
| 432 | * <p> |
| 433 | * In the JLS3 API, this method is a convenience method that |
| 434 | * computes these flags from <code>modifiers()</code>. |
| 435 | * </p> |
| 436 | * |
| 437 | * @return the bit-wise or of <code>Modifier</code> constants |
| 438 | * @see Modifier |
| 439 | */ |
| 440 | public int getModifiers() { |
| 441 | // more efficient than checking getAST().API_LEVEL |
| 442 | if (this.modifiers == null) { |
| 443 | // JLS2 behavior - bona fide property |
| 444 | return this.modifierFlags; |
| 445 | } else { |
| 446 | // JLS3 behavior - convenient method |
| 447 | // performance could be improved by caching computed flags |
| 448 | // but this would require tracking changes to this.modifiers |
| 449 | int computedModifierFlags = Modifier.NONE; |
| 450 | for (Iterator it = modifiers().iterator(); it.hasNext(); ) { |
| 451 | Object x = it.next(); |
| 452 | if (x instanceof Modifier) { |
| 453 | computedModifierFlags |= ((Modifier) x).getKeyword().toFlagValue(); |
| 454 | } |
| 455 | } |
| 456 | return computedModifierFlags; |
| 457 | } |
| 458 | } |
| 459 | |
| 460 | /** |
| 461 | * Sets the modifiers explicitly specified on this declaration (JLS2 API only). |
| 462 | * <p> |
| 463 | * The following modifiers are meaningful for fields: public, private, protected, |
| 464 | * static, final, volatile, and transient. For local variable and formal |
| 465 | * parameter declarations, the only meaningful modifier is final. |
| 466 | * </p> |
| 467 | * |
| 468 | * @param modifiers the given modifiers (bit-wise or of <code>Modifier</code> constants) |
| 469 | * @exception UnsupportedOperationException if this operation is used in |
| 470 | * an AST later than JLS2 |
| 471 | * @see Modifier |
| 472 | * @deprecated In the JLS3 API, this method is replaced by |
| 473 | * {@link #modifiers()} which contains a list of a <code>Modifier</code> nodes. |
| 474 | */ |
| 475 | public void setModifiers(int modifiers) { |
| 476 | internalSetModifiers(modifiers); |
| 477 | } |
| 478 | |
| 479 | /** |
| 480 | * Internal synonym for deprecated method. Used to avoid |
| 481 | * deprecation warnings. |
| 482 | * @since 3.1 |
| 483 | */ |
| 484 | /*package*/ final void internalSetModifiers(int pmodifiers) { |
| 485 | supportedOnlyIn2(); |
| 486 | preValueChange(MODIFIERS_PROPERTY); |
| 487 | this.modifierFlags = pmodifiers; |
| 488 | postValueChange(MODIFIERS_PROPERTY); |
| 489 | } |
| 490 | |
| 491 | /** |
| 492 | * Returns the type of the variable declared in this variable declaration, |
| 493 | * exclusive of any extra array dimensions or the varargs dimension. |
| 494 | * <p> |
| 495 | * WARNING: For array-typed varargs, the {@link Type#resolveBinding() binding} |
| 496 | * of the returned <code>Type</code> is not useful, since it represents |
| 497 | * an unused type. It misses the last (innermost) dimension that carries the |
| 498 | * {@link #varargsAnnotations()}. |
| 499 | * </p> |
| 500 | * |
| 501 | * @return the type |
| 502 | */ |
| 503 | public Type getType() { |
| 504 | if (this.type == null) { |
| 505 | // lazy init must be thread-safe for readers |
| 506 | synchronized (this) { |
| 507 | if (this.type == null) { |
| 508 | preLazyInit(); |
| 509 | this.type = this.ast.newPrimitiveType(PrimitiveType.INT); |
| 510 | postLazyInit(this.type, TYPE_PROPERTY); |
| 511 | } |
| 512 | } |
| 513 | } |
| 514 | return this.type; |
| 515 | } |
| 516 | |
| 517 | /** |
| 518 | * Sets the type of the variable declared in this variable declaration to |
| 519 | * the given type, exclusive of any extra array dimensions. |
| 520 | * |
| 521 | * @param type the new type |
| 522 | * @exception IllegalArgumentException if: |
| 523 | * <ul> |
| 524 | * <li>the node belongs to a different AST</li> |
| 525 | * <li>the node already has a parent</li> |
| 526 | * </ul> |
| 527 | */ |
| 528 | public void setType(Type type) { |
| 529 | if (type == null) { |
| 530 | throw new IllegalArgumentException(); |
| 531 | } |
| 532 | ASTNode oldChild = this.type; |
| 533 | preReplaceChild(oldChild, type, TYPE_PROPERTY); |
| 534 | this.type = type; |
| 535 | postReplaceChild(oldChild, type, TYPE_PROPERTY); |
| 536 | } |
| 537 | |
| 538 | /** |
| 539 | * Returns whether this declaration declares the last parameter of |
| 540 | * a variable arity method (added in JLS3 API). |
| 541 | * <p> |
| 542 | * Note that the binding for the type <code>Foo</code> in the vararg method |
| 543 | * declaration <code>void fun(Foo... args)</code> is always for the type as |
| 544 | * written; i.e., the type binding for <code>Foo</code>. However, if you |
| 545 | * navigate from the method declaration to its method binding to the |
| 546 | * type binding for its last parameter, the type binding for the vararg |
| 547 | * parameter is always an array type (i.e., <code>Foo[]</code>) reflecting |
| 548 | * the way vararg methods get compiled. |
| 549 | * </p> |
| 550 | * <p> |
| 551 | * WARNING: For array-typed varargs, the {@link Type#resolveBinding() binding} |
| 552 | * of the variable's {@link #getType() type} is not useful, since it represents |
| 553 | * an unused type. It misses the last (innermost) dimension that carries the |
| 554 | * {@link #varargsAnnotations()}. |
| 555 | * </p> |
| 556 | * |
| 557 | * @return <code>true</code> if this is a variable arity parameter declaration, |
| 558 | * and <code>false</code> otherwise |
| 559 | * @exception UnsupportedOperationException if this operation is used in |
| 560 | * a JLS2 AST |
| 561 | * @since 3.1 |
| 562 | */ |
| 563 | public boolean isVarargs() { |
| 564 | // more efficient than just calling unsupportedIn2() to check |
| 565 | if (this.modifiers == null) { |
| 566 | unsupportedIn2(); |
| 567 | } |
| 568 | return this.variableArity; |
| 569 | } |
| 570 | |
| 571 | /** |
| 572 | * Sets whether this declaration declares the last parameter of |
| 573 | * a variable arity method (added in JLS3 API). |
| 574 | * |
| 575 | * @param variableArity <code>true</code> if this is a variable arity |
| 576 | * parameter declaration, and <code>false</code> otherwise |
| 577 | * @since 3.1 |
| 578 | */ |
| 579 | public void setVarargs(boolean variableArity) { |
| 580 | // more efficient than just calling unsupportedIn2() to check |
| 581 | if (this.modifiers == null) { |
| 582 | unsupportedIn2(); |
| 583 | } |
| 584 | preValueChange(VARARGS_PROPERTY); |
| 585 | this.variableArity = variableArity; |
| 586 | postValueChange(VARARGS_PROPERTY); |
| 587 | } |
| 588 | |
| 589 | /** |
| 590 | * Returns the ordered list of annotations on the varargs token (added in JLS8 API). |
| 591 | * <p> |
| 592 | * WARNING: For array-typed varargs, the {@link Type#resolveBinding() binding} |
| 593 | * of the variable's {@link #getType() type} is not useful, since it represents |
| 594 | * an unused type. It misses the last (innermost) dimension that carries the |
| 595 | * returned {@code varargsAnnotations}. |
| 596 | * </p> |
| 597 | * |
| 598 | * @return the list of annotations on the varargs token (element type: {@link Annotation}) |
| 599 | * @exception UnsupportedOperationException if this operation is used |
| 600 | * in a JLS2, JLS3 or JLS4 AST |
| 601 | * @see #isVarargs() |
| 602 | * @since 3.10 |
| 603 | */ |
| 604 | public List varargsAnnotations() { |
| 605 | if (this.varargsAnnotations == null) { |
| 606 | unsupportedIn2_3_4(); |
| 607 | } |
| 608 | return this.varargsAnnotations; |
| 609 | } |
| 610 | |
| 611 | @Override |
| 612 | int memSize() { |
| 613 | // treat Operator as free |
| 614 | return BASE_NODE_SIZE + 9 * 4; |
| 615 | } |
| 616 | |
| 617 | @Override |
| 618 | int treeSize() { |
| 619 | return |
| 620 | memSize() |
| 621 | + (this.modifiers == null ? 0 : this.modifiers.listSize()) |
| 622 | + (this.type == null ? 0 : getType().treeSize()) |
| 623 | + (this.varargsAnnotations == null ? 0 : this.varargsAnnotations.listSize()) |
| 624 | + (this.variableName == null ? 0 : getName().treeSize()) |
| 625 | + (this.extraDimensions == null ? 0 : this.extraDimensions.listSize()) |
| 626 | + (this.optionalInitializer == null ? 0 : getInitializer().treeSize()); |
| 627 | } |
| 628 | } |
| 629 |
Members