1 11 12 package org.eclipse.jdt.core.dom; 13 14 import java.util.ArrayList ; 15 import java.util.Iterator ; 16 import java.util.List ; 17 18 38 public class SingleVariableDeclaration extends VariableDeclaration { 39 40 44 public static final SimplePropertyDescriptor MODIFIERS_PROPERTY = 45 new SimplePropertyDescriptor(SingleVariableDeclaration.class, "modifiers", int.class, MANDATORY); 47 51 public static final ChildListPropertyDescriptor MODIFIERS2_PROPERTY = 52 new ChildListPropertyDescriptor(SingleVariableDeclaration.class, "modifiers", IExtendedModifier.class, CYCLE_RISK); 54 58 public static final ChildPropertyDescriptor NAME_PROPERTY = 59 new ChildPropertyDescriptor(SingleVariableDeclaration.class, "name", SimpleName.class, MANDATORY, NO_CYCLE_RISK); 61 65 public static final ChildPropertyDescriptor TYPE_PROPERTY = 66 new ChildPropertyDescriptor(SingleVariableDeclaration.class, "type", Type.class, MANDATORY, NO_CYCLE_RISK); 68 72 public static final SimplePropertyDescriptor VARARGS_PROPERTY = 73 new SimplePropertyDescriptor(SingleVariableDeclaration.class, "varargs", boolean.class, MANDATORY); 75 79 public static final SimplePropertyDescriptor EXTRA_DIMENSIONS_PROPERTY = 80 new SimplePropertyDescriptor(SingleVariableDeclaration.class, "extraDimensions", int.class, MANDATORY); 82 86 public static final ChildPropertyDescriptor INITIALIZER_PROPERTY = 87 new ChildPropertyDescriptor(SingleVariableDeclaration.class, "initializer", Expression.class, OPTIONAL, CYCLE_RISK); 89 95 private static final List PROPERTY_DESCRIPTORS_2_0; 96 97 103 private static final List PROPERTY_DESCRIPTORS_3_0; 104 105 static { 106 List propertyList = new ArrayList (6); 107 createPropertyList(SingleVariableDeclaration.class, propertyList); 108 addProperty(MODIFIERS_PROPERTY, propertyList); 109 addProperty(TYPE_PROPERTY, propertyList); 110 addProperty(NAME_PROPERTY, propertyList); 111 addProperty(EXTRA_DIMENSIONS_PROPERTY, propertyList); 112 addProperty(INITIALIZER_PROPERTY, propertyList); 113 PROPERTY_DESCRIPTORS_2_0 = reapPropertyList(propertyList); 114 115 propertyList = new ArrayList (7); 116 createPropertyList(SingleVariableDeclaration.class, propertyList); 117 addProperty(MODIFIERS2_PROPERTY, propertyList); 118 addProperty(TYPE_PROPERTY, propertyList); 119 addProperty(VARARGS_PROPERTY, propertyList); 120 addProperty(NAME_PROPERTY, propertyList); 121 addProperty(EXTRA_DIMENSIONS_PROPERTY, propertyList); 122 addProperty(INITIALIZER_PROPERTY, propertyList); 123 PROPERTY_DESCRIPTORS_3_0 = reapPropertyList(propertyList); 124 } 125 126 136 public static List propertyDescriptors(int apiLevel) { 137 if (apiLevel == AST.JLS2_INTERNAL) { 138 return PROPERTY_DESCRIPTORS_2_0; 139 } else { 140 return PROPERTY_DESCRIPTORS_3_0; 141 } 142 } 143 144 151 private ASTNode.NodeList modifiers = null; 152 153 157 private int modifierFlags = Modifier.NONE; 158 159 163 private SimpleName variableName = null; 164 165 169 private Type type = null; 170 171 177 private boolean variableArity = false; 178 179 185 private int extraArrayDimensions = 0; 186 187 191 private Expression optionalInitializer = null; 192 193 204 SingleVariableDeclaration(AST ast) { 205 super(ast); 206 if (ast.apiLevel >= AST.JLS3) { 207 this.modifiers = new ASTNode.NodeList(MODIFIERS2_PROPERTY); 208 } 209 } 210 211 215 final SimplePropertyDescriptor internalExtraDimensionsProperty() { 216 return EXTRA_DIMENSIONS_PROPERTY; 217 } 218 219 223 final ChildPropertyDescriptor internalInitializerProperty() { 224 return INITIALIZER_PROPERTY; 225 } 226 227 231 final ChildPropertyDescriptor internalNameProperty() { 232 return NAME_PROPERTY; 233 } 234 235 238 final List internalStructuralPropertiesForType(int apiLevel) { 239 return propertyDescriptors(apiLevel); 240 } 241 242 245 final int internalGetSetIntProperty(SimplePropertyDescriptor property, boolean get, int value) { 246 if (property == MODIFIERS_PROPERTY) { 247 if (get) { 248 return getModifiers(); 249 } else { 250 setModifiers(value); 251 return 0; 252 } 253 } 254 if (property == EXTRA_DIMENSIONS_PROPERTY) { 255 if (get) { 256 return getExtraDimensions(); 257 } else { 258 setExtraDimensions(value); 259 return 0; 260 } 261 } 262 return super.internalGetSetIntProperty(property, get, value); 264 } 265 266 269 final boolean internalGetSetBooleanProperty(SimplePropertyDescriptor property, boolean get, boolean value) { 270 if (property == VARARGS_PROPERTY) { 271 if (get) { 272 return isVarargs(); 273 } else { 274 setVarargs(value); 275 return false; 276 } 277 } 278 return super.internalGetSetBooleanProperty(property, get, value); 280 } 281 282 285 final ASTNode internalGetSetChildProperty(ChildPropertyDescriptor property, boolean get, ASTNode child) { 286 if (property == NAME_PROPERTY) { 287 if (get) { 288 return getName(); 289 } else { 290 setName((SimpleName) child); 291 return null; 292 } 293 } 294 if (property == TYPE_PROPERTY) { 295 if (get) { 296 return getType(); 297 } else { 298 setType((Type) child); 299 return null; 300 } 301 } 302 if (property == INITIALIZER_PROPERTY) { 303 if (get) { 304 return getInitializer(); 305 } else { 306 setInitializer((Expression) child); 307 return null; 308 } 309 } 310 return super.internalGetSetChildProperty(property, get, child); 312 } 313 314 317 final List internalGetChildListProperty(ChildListPropertyDescriptor property) { 318 if (property == MODIFIERS2_PROPERTY) { 319 return modifiers(); 320 } 321 return super.internalGetChildListProperty(property); 323 } 324 325 328 final int getNodeType0() { 329 return SINGLE_VARIABLE_DECLARATION; 330 } 331 332 335 ASTNode clone0(AST target) { 336 SingleVariableDeclaration result = new SingleVariableDeclaration(target); 337 result.setSourceRange(this.getStartPosition(), this.getLength()); 338 if (this.ast.apiLevel == AST.JLS2_INTERNAL) { 339 result.setModifiers(getModifiers()); 340 } else { 341 result.modifiers().addAll(ASTNode.copySubtrees(target, modifiers())); 342 result.setVarargs(isVarargs()); 343 } 344 result.setType((Type) getType().clone(target)); 345 result.setExtraDimensions(getExtraDimensions()); 346 result.setName((SimpleName) getName().clone(target)); 347 result.setInitializer( 348 (Expression) ASTNode.copySubtree(target, getInitializer())); 349 return result; 350 } 351 352 355 final boolean subtreeMatch0(ASTMatcher matcher, Object other) { 356 return matcher.match(this, other); 358 } 359 360 363 void accept0(ASTVisitor visitor) { 364 boolean visitChildren = visitor.visit(this); 365 if (visitChildren) { 366 if (this.ast.apiLevel >= AST.JLS3) { 368 acceptChildren(visitor, this.modifiers); 369 } 370 acceptChild(visitor, getType()); 371 acceptChild(visitor, getName()); 372 acceptChild(visitor, getInitializer()); 373 } 374 visitor.endVisit(this); 375 } 376 377 391 public List modifiers() { 392 if (this.modifiers == null) { 394 unsupportedIn2(); 395 } 396 return this.modifiers; 397 } 398 399 409 public int getModifiers() { 410 if (this.modifiers == null) { 412 return this.modifierFlags; 414 } else { 415 int computedModifierFlags = Modifier.NONE; 419 for (Iterator it = modifiers().iterator(); it.hasNext(); ) { 420 Object x = it.next(); 421 if (x instanceof Modifier) { 422 computedModifierFlags |= ((Modifier) x).getKeyword().toFlagValue(); 423 } 424 } 425 return computedModifierFlags; 426 } 427 } 428 429 444 public void setModifiers(int modifiers) { 445 internalSetModifiers(modifiers); 446 } 447 448 453 final void internalSetModifiers(int pmodifiers) { 454 supportedOnlyIn2(); 455 preValueChange(MODIFIERS_PROPERTY); 456 this.modifierFlags = pmodifiers; 457 postValueChange(MODIFIERS_PROPERTY); 458 } 459 460 463 public SimpleName getName() { 464 if (this.variableName == null) { 465 synchronized (this) { 467 if (this.variableName == null) { 468 preLazyInit(); 469 this.variableName = new SimpleName(this.ast); 470 postLazyInit(this.variableName, NAME_PROPERTY); 471 } 472 } 473 } 474 return this.variableName; 475 } 476 477 480 public void setName(SimpleName variableName) { 481 if (variableName == null) { 482 throw new IllegalArgumentException (); 483 } 484 ASTNode oldChild = this.variableName; 485 preReplaceChild(oldChild, variableName, NAME_PROPERTY); 486 this.variableName = variableName; 487 postReplaceChild(oldChild, variableName, NAME_PROPERTY); 488 } 489 490 496 public Type getType() { 497 if (this.type == null) { 498 synchronized (this) { 500 if (this.type == null) { 501 preLazyInit(); 502 this.type = this.ast.newPrimitiveType(PrimitiveType.INT); 503 postLazyInit(this.type, TYPE_PROPERTY); 504 } 505 } 506 } 507 return this.type; 508 } 509 510 521 public void setType(Type type) { 522 if (type == null) { 523 throw new IllegalArgumentException (); 524 } 525 ASTNode oldChild = this.type; 526 preReplaceChild(oldChild, type, TYPE_PROPERTY); 527 this.type = type; 528 postReplaceChild(oldChild, type, TYPE_PROPERTY); 529 } 530 531 550 public boolean isVarargs() { 551 if (this.modifiers == null) { 553 unsupportedIn2(); 554 } 555 return this.variableArity; 556 } 557 558 566 public void setVarargs(boolean variableArity) { 567 if (this.modifiers == null) { 569 unsupportedIn2(); 570 } 571 preValueChange(VARARGS_PROPERTY); 572 this.variableArity = variableArity; 573 postValueChange(VARARGS_PROPERTY); 574 } 575 576 580 public int getExtraDimensions() { 581 return this.extraArrayDimensions; 582 } 583 584 588 public void setExtraDimensions(int dimensions) { 589 if (dimensions < 0) { 590 throw new IllegalArgumentException (); 591 } 592 preValueChange(EXTRA_DIMENSIONS_PROPERTY); 593 this.extraArrayDimensions = dimensions; 594 postValueChange(EXTRA_DIMENSIONS_PROPERTY); 595 } 596 597 600 public Expression getInitializer() { 601 return this.optionalInitializer; 602 } 603 604 607 public void setInitializer(Expression initializer) { 608 ASTNode oldChild = this.optionalInitializer; 611 preReplaceChild(oldChild, initializer,INITIALIZER_PROPERTY); 612 this.optionalInitializer = initializer; 613 postReplaceChild(oldChild, initializer,INITIALIZER_PROPERTY); 614 } 615 616 619 int memSize() { 620 return BASE_NODE_SIZE + 7 * 4; 622 } 623 624 627 int treeSize() { 628 return 629 memSize() 630 + (this.modifiers == null ? 0 : this.modifiers.listSize()) 631 + (this.type == null ? 0 : getType().treeSize()) 632 + (this.variableName == null ? 0 : getName().treeSize()) 633 + (this.optionalInitializer == null ? 0 : getInitializer().treeSize()); 634 } 635 } 636 | Popular Tags |