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 68 public class TypeDeclaration extends AbstractTypeDeclaration { 69 70 74 public static final ChildPropertyDescriptor JAVADOC_PROPERTY = 75 internalJavadocPropertyFactory(TypeDeclaration.class); 76 77 81 public static final SimplePropertyDescriptor MODIFIERS_PROPERTY = 82 internalModifiersPropertyFactory(TypeDeclaration.class); 83 84 88 public static final ChildListPropertyDescriptor MODIFIERS2_PROPERTY = 89 internalModifiers2PropertyFactory(TypeDeclaration.class); 90 91 95 public static final SimplePropertyDescriptor INTERFACE_PROPERTY = 96 new SimplePropertyDescriptor(TypeDeclaration.class, "interface", boolean.class, MANDATORY); 98 102 public static final ChildPropertyDescriptor NAME_PROPERTY = 103 new ChildPropertyDescriptor(TypeDeclaration.class, "name", SimpleName.class, MANDATORY, NO_CYCLE_RISK); 105 109 public static final ChildPropertyDescriptor SUPERCLASS_PROPERTY = 110 new ChildPropertyDescriptor(TypeDeclaration.class, "superclass", Name.class, OPTIONAL, NO_CYCLE_RISK); 112 116 public static final ChildListPropertyDescriptor SUPER_INTERFACES_PROPERTY = 117 new ChildListPropertyDescriptor(TypeDeclaration.class, "superInterfaces", Name.class, NO_CYCLE_RISK); 119 123 public static final ChildPropertyDescriptor SUPERCLASS_TYPE_PROPERTY = 124 new ChildPropertyDescriptor(TypeDeclaration.class, "superclassType", Type.class, OPTIONAL, NO_CYCLE_RISK); 126 130 public static final ChildListPropertyDescriptor SUPER_INTERFACE_TYPES_PROPERTY = 131 new ChildListPropertyDescriptor(TypeDeclaration.class, "superInterfaceTypes", Type.class, NO_CYCLE_RISK); 133 137 public static final ChildListPropertyDescriptor TYPE_PARAMETERS_PROPERTY = 138 new ChildListPropertyDescriptor(TypeDeclaration.class, "typeParameters", TypeParameter.class, NO_CYCLE_RISK); 140 144 public static final ChildListPropertyDescriptor BODY_DECLARATIONS_PROPERTY = 145 internalBodyDeclarationPropertyFactory(TypeDeclaration.class); 146 147 153 private static final List PROPERTY_DESCRIPTORS_2_0; 154 155 161 private static final List PROPERTY_DESCRIPTORS_3_0; 162 163 static { 164 List propertyList = new ArrayList (8); 165 createPropertyList(TypeDeclaration.class, propertyList); 166 addProperty(JAVADOC_PROPERTY, propertyList); 167 addProperty(MODIFIERS_PROPERTY, propertyList); 168 addProperty(INTERFACE_PROPERTY, propertyList); 169 addProperty(NAME_PROPERTY, propertyList); 170 addProperty(SUPERCLASS_PROPERTY, propertyList); 171 addProperty(SUPER_INTERFACES_PROPERTY, propertyList); 172 addProperty(BODY_DECLARATIONS_PROPERTY, propertyList); 173 PROPERTY_DESCRIPTORS_2_0 = reapPropertyList(propertyList); 174 175 propertyList = new ArrayList (9); 176 createPropertyList(TypeDeclaration.class, propertyList); 177 addProperty(JAVADOC_PROPERTY, propertyList); 178 addProperty(MODIFIERS2_PROPERTY, propertyList); 179 addProperty(INTERFACE_PROPERTY, propertyList); 180 addProperty(NAME_PROPERTY, propertyList); 181 addProperty(TYPE_PARAMETERS_PROPERTY, propertyList); 182 addProperty(SUPERCLASS_TYPE_PROPERTY, propertyList); 183 addProperty(SUPER_INTERFACE_TYPES_PROPERTY, propertyList); 184 addProperty(BODY_DECLARATIONS_PROPERTY, propertyList); 185 PROPERTY_DESCRIPTORS_3_0 = reapPropertyList(propertyList); 186 } 187 188 199 public static List propertyDescriptors(int apiLevel) { 200 if (apiLevel == AST.JLS2_INTERNAL) { 201 return PROPERTY_DESCRIPTORS_2_0; 202 } else { 203 return PROPERTY_DESCRIPTORS_3_0; 204 } 205 } 206 207 211 private boolean isInterface = false; 212 213 219 private ASTNode.NodeList typeParameters = null; 220 221 226 private Name optionalSuperclassName = null; 227 228 234 private ASTNode.NodeList superInterfaceNames = null; 235 236 242 private Type optionalSuperclassType = null; 243 244 250 private ASTNode.NodeList superInterfaceTypes = null; 251 252 266 TypeDeclaration(AST ast) { 267 super(ast); 268 if (ast.apiLevel == AST.JLS2_INTERNAL) { 269 this.superInterfaceNames = new ASTNode.NodeList(SUPER_INTERFACES_PROPERTY); 270 } 271 if (ast.apiLevel >= AST.JLS3) { 272 this.typeParameters = new ASTNode.NodeList(TYPE_PARAMETERS_PROPERTY); 273 this.superInterfaceTypes = new ASTNode.NodeList(SUPER_INTERFACE_TYPES_PROPERTY); 274 } 275 } 276 277 281 final List internalStructuralPropertiesForType(int apiLevel) { 282 return propertyDescriptors(apiLevel); 283 } 284 285 288 final int internalGetSetIntProperty(SimplePropertyDescriptor property, boolean get, int value) { 289 if (property == MODIFIERS_PROPERTY) { 290 if (get) { 291 return getModifiers(); 292 } else { 293 internalSetModifiers(value); 294 return 0; 295 } 296 } 297 return super.internalGetSetIntProperty(property, get, value); 299 } 300 301 304 final boolean internalGetSetBooleanProperty(SimplePropertyDescriptor property, boolean get, boolean value) { 305 if (property == INTERFACE_PROPERTY) { 306 if (get) { 307 return isInterface(); 308 } else { 309 setInterface(value); 310 return false; 311 } 312 } 313 return super.internalGetSetBooleanProperty(property, get, value); 315 } 316 317 320 final ASTNode internalGetSetChildProperty(ChildPropertyDescriptor property, boolean get, ASTNode child) { 321 if (property == JAVADOC_PROPERTY) { 322 if (get) { 323 return getJavadoc(); 324 } else { 325 setJavadoc((Javadoc) child); 326 return null; 327 } 328 } 329 if (property == NAME_PROPERTY) { 330 if (get) { 331 return getName(); 332 } else { 333 setName((SimpleName) child); 334 return null; 335 } 336 } 337 if (property == SUPERCLASS_PROPERTY) { 338 if (get) { 339 return getSuperclass(); 340 } else { 341 setSuperclass((Name) child); 342 return null; 343 } 344 } 345 if (property == SUPERCLASS_TYPE_PROPERTY) { 346 if (get) { 347 return getSuperclassType(); 348 } else { 349 setSuperclassType((Type) child); 350 return null; 351 } 352 } 353 return super.internalGetSetChildProperty(property, get, child); 355 } 356 357 360 final List internalGetChildListProperty(ChildListPropertyDescriptor property) { 361 if (property == MODIFIERS2_PROPERTY) { 362 return modifiers(); 363 } 364 if (property == TYPE_PARAMETERS_PROPERTY) { 365 return typeParameters(); 366 } 367 if (property == SUPER_INTERFACES_PROPERTY) { 368 return superInterfaces(); 369 } 370 if (property == SUPER_INTERFACE_TYPES_PROPERTY) { 371 return superInterfaceTypes(); 372 } 373 if (property == BODY_DECLARATIONS_PROPERTY) { 374 return bodyDeclarations(); 375 } 376 return super.internalGetChildListProperty(property); 378 } 379 380 383 final ChildPropertyDescriptor internalJavadocProperty() { 384 return JAVADOC_PROPERTY; 385 } 386 387 390 final ChildListPropertyDescriptor internalModifiers2Property() { 391 return MODIFIERS2_PROPERTY; 392 } 393 394 397 final SimplePropertyDescriptor internalModifiersProperty() { 398 return MODIFIERS_PROPERTY; 399 } 400 401 404 final ChildPropertyDescriptor internalNameProperty() { 405 return NAME_PROPERTY; 406 } 407 408 411 final ChildListPropertyDescriptor internalBodyDeclarationsProperty() { 412 return BODY_DECLARATIONS_PROPERTY; 413 } 414 415 416 419 final int getNodeType0() { 420 return TYPE_DECLARATION; 421 } 422 423 426 ASTNode clone0(AST target) { 427 TypeDeclaration result = new TypeDeclaration(target); 428 result.setSourceRange(this.getStartPosition(), this.getLength()); 429 result.setJavadoc( 430 (Javadoc) ASTNode.copySubtree(target, getJavadoc())); 431 if (this.ast.apiLevel == AST.JLS2_INTERNAL) { 432 result.internalSetModifiers(getModifiers()); 433 result.setSuperclass( 434 (Name) ASTNode.copySubtree(target, getSuperclass())); 435 result.superInterfaces().addAll( 436 ASTNode.copySubtrees(target, superInterfaces())); 437 } 438 result.setInterface(isInterface()); 439 result.setName((SimpleName) getName().clone(target)); 440 if (this.ast.apiLevel >= AST.JLS3) { 441 result.modifiers().addAll(ASTNode.copySubtrees(target, modifiers())); 442 result.typeParameters().addAll( 443 ASTNode.copySubtrees(target, typeParameters())); 444 result.setSuperclassType( 445 (Type) ASTNode.copySubtree(target, getSuperclassType())); 446 result.superInterfaceTypes().addAll( 447 ASTNode.copySubtrees(target, superInterfaceTypes())); 448 } 449 result.bodyDeclarations().addAll( 450 ASTNode.copySubtrees(target, bodyDeclarations())); 451 return result; 452 } 453 454 457 final boolean subtreeMatch0(ASTMatcher matcher, Object other) { 458 return matcher.match(this, other); 460 } 461 462 465 void accept0(ASTVisitor visitor) { 466 boolean visitChildren = visitor.visit(this); 467 if (visitChildren) { 468 if (this.ast.apiLevel == AST.JLS2_INTERNAL) { 470 acceptChild(visitor, getJavadoc()); 471 acceptChild(visitor, getName()); 472 acceptChild(visitor, getSuperclass()); 473 acceptChildren(visitor, this.superInterfaceNames); 474 acceptChildren(visitor, this.bodyDeclarations); 475 } 476 if (this.ast.apiLevel >= AST.JLS3) { 477 acceptChild(visitor, getJavadoc()); 478 acceptChildren(visitor, this.modifiers); 479 acceptChild(visitor, getName()); 480 acceptChildren(visitor, this.typeParameters); 481 acceptChild(visitor, getSuperclassType()); 482 acceptChildren(visitor, this.superInterfaceTypes); 483 acceptChildren(visitor, this.bodyDeclarations); 484 } 485 } 486 visitor.endVisit(this); 487 } 488 489 496 public boolean isInterface() { 497 return this.isInterface; 498 } 499 500 508 public void setInterface(boolean isInterface) { 509 preValueChange(INTERFACE_PROPERTY); 510 this.isInterface = isInterface; 511 postValueChange(INTERFACE_PROPERTY); 512 } 513 514 524 public List typeParameters() { 525 if (this.typeParameters == null) { 527 unsupportedIn2(); 528 } 529 return this.typeParameters; 530 } 531 532 549 public Name getSuperclass() { 550 return internalGetSuperclass(); 551 } 552 553 558 final Name internalGetSuperclass() { 559 supportedOnlyIn2(); 560 return this.optionalSuperclassName; 561 } 562 563 578 public Type getSuperclassType() { 579 unsupportedIn2(); 580 return this.optionalSuperclassType; 581 } 582 583 605 public void setSuperclass(Name superclassName) { 606 internalSetSuperclass(superclassName); 607 } 608 609 614 final void internalSetSuperclass(Name superclassName) { 615 supportedOnlyIn2(); 616 ASTNode oldChild = this.optionalSuperclassName; 617 preReplaceChild(oldChild, superclassName, SUPERCLASS_PROPERTY); 618 this.optionalSuperclassName = superclassName; 619 postReplaceChild(oldChild, superclassName, SUPERCLASS_PROPERTY); 620 } 621 622 641 public void setSuperclassType(Type superclassType) { 642 unsupportedIn2(); 643 ASTNode oldChild = this.optionalSuperclassType; 644 preReplaceChild(oldChild, superclassType, SUPERCLASS_TYPE_PROPERTY); 645 this.optionalSuperclassType = superclassType; 646 postReplaceChild(oldChild, superclassType, SUPERCLASS_TYPE_PROPERTY); 647 } 648 649 663 public List superInterfaces() { 664 return internalSuperInterfaces(); 665 } 666 667 672 final List internalSuperInterfaces() { 673 if (this.superInterfaceNames == null) { 675 supportedOnlyIn2(); 676 } 677 return this.superInterfaceNames; 678 } 679 680 692 public List superInterfaceTypes() { 693 if (this.superInterfaceTypes == null) { 695 unsupportedIn2(); 696 } 697 return this.superInterfaceTypes; 698 } 699 700 713 public FieldDeclaration[] getFields() { 714 List bd = bodyDeclarations(); 715 int fieldCount = 0; 716 for (Iterator it = bd.listIterator(); it.hasNext(); ) { 717 if (it.next() instanceof FieldDeclaration) { 718 fieldCount++; 719 } 720 } 721 FieldDeclaration[] fields = new FieldDeclaration[fieldCount]; 722 int next = 0; 723 for (Iterator it = bd.listIterator(); it.hasNext(); ) { 724 Object decl = it.next(); 725 if (decl instanceof FieldDeclaration) { 726 fields[next++] = (FieldDeclaration) decl; 727 } 728 } 729 return fields; 730 } 731 732 744 public MethodDeclaration[] getMethods() { 745 List bd = bodyDeclarations(); 746 int methodCount = 0; 747 for (Iterator it = bd.listIterator(); it.hasNext(); ) { 748 if (it.next() instanceof MethodDeclaration) { 749 methodCount++; 750 } 751 } 752 MethodDeclaration[] methods = new MethodDeclaration[methodCount]; 753 int next = 0; 754 for (Iterator it = bd.listIterator(); it.hasNext(); ) { 755 Object decl = it.next(); 756 if (decl instanceof MethodDeclaration) { 757 methods[next++] = (MethodDeclaration) decl; 758 } 759 } 760 return methods; 761 } 762 763 774 public TypeDeclaration[] getTypes() { 775 List bd = bodyDeclarations(); 776 int typeCount = 0; 777 for (Iterator it = bd.listIterator(); it.hasNext(); ) { 778 if (it.next() instanceof TypeDeclaration) { 779 typeCount++; 780 } 781 } 782 TypeDeclaration[] memberTypes = new TypeDeclaration[typeCount]; 783 int next = 0; 784 for (Iterator it = bd.listIterator(); it.hasNext(); ) { 785 Object decl = it.next(); 786 if (decl instanceof TypeDeclaration) { 787 memberTypes[next++] = (TypeDeclaration) decl; 788 } 789 } 790 return memberTypes; 791 } 792 793 796 ITypeBinding internalResolveBinding() { 797 return this.ast.getBindingResolver().resolveType(this); 798 } 799 800 803 int memSize() { 804 return super.memSize() + 6 * 4; 805 } 806 807 810 int treeSize() { 811 return memSize() 812 + (this.optionalDocComment == null ? 0 : getJavadoc().treeSize()) 813 + (this.modifiers == null ? 0 : this.modifiers.listSize()) 814 + (this.typeName == null ? 0 : getName().treeSize()) 815 + (this.typeParameters == null ? 0 : this.typeParameters.listSize()) 816 + (this.optionalSuperclassName == null ? 0 : getSuperclass().treeSize()) 817 + (this.optionalSuperclassType == null ? 0 : getSuperclassType().treeSize()) 818 + (this.superInterfaceNames == null ? 0 : this.superInterfaceNames.listSize()) 819 + (this.superInterfaceTypes == null ? 0 : this.superInterfaceTypes.listSize()) 820 + this.bodyDeclarations.listSize(); 821 } 822 } 823 824 | Popular Tags |