1 11 12 package org.eclipse.jdt.core.dom; 13 14 import java.util.ArrayList ; 15 import java.util.List ; 16 17 65 public class MethodDeclaration extends BodyDeclaration { 66 67 71 public static final ChildPropertyDescriptor JAVADOC_PROPERTY = 72 internalJavadocPropertyFactory(MethodDeclaration.class); 73 74 78 public static final SimplePropertyDescriptor MODIFIERS_PROPERTY = 79 internalModifiersPropertyFactory(MethodDeclaration.class); 80 81 85 public static final ChildListPropertyDescriptor MODIFIERS2_PROPERTY = 86 internalModifiers2PropertyFactory(MethodDeclaration.class); 87 88 92 public static final SimplePropertyDescriptor CONSTRUCTOR_PROPERTY = 93 new SimplePropertyDescriptor(MethodDeclaration.class, "constructor", boolean.class, MANDATORY); 95 99 public static final ChildPropertyDescriptor NAME_PROPERTY = 100 new ChildPropertyDescriptor(MethodDeclaration.class, "name", SimpleName.class, MANDATORY, NO_CYCLE_RISK); 102 106 public static final ChildPropertyDescriptor RETURN_TYPE_PROPERTY = 107 new ChildPropertyDescriptor(MethodDeclaration.class, "returnType", Type.class, MANDATORY, NO_CYCLE_RISK); 109 113 public static final ChildPropertyDescriptor RETURN_TYPE2_PROPERTY = 114 new ChildPropertyDescriptor(MethodDeclaration.class, "returnType2", Type.class, OPTIONAL, NO_CYCLE_RISK); 116 120 public static final SimplePropertyDescriptor EXTRA_DIMENSIONS_PROPERTY = 121 new SimplePropertyDescriptor(MethodDeclaration.class, "extraDimensions", int.class, MANDATORY); 123 127 public static final ChildListPropertyDescriptor TYPE_PARAMETERS_PROPERTY = 128 new ChildListPropertyDescriptor(MethodDeclaration.class, "typeParameters", TypeParameter.class, NO_CYCLE_RISK); 130 134 public static final ChildListPropertyDescriptor PARAMETERS_PROPERTY = 135 new ChildListPropertyDescriptor(MethodDeclaration.class, "parameters", SingleVariableDeclaration.class, CYCLE_RISK); 137 141 public static final ChildListPropertyDescriptor THROWN_EXCEPTIONS_PROPERTY = 142 new ChildListPropertyDescriptor(MethodDeclaration.class, "thrownExceptions", Name.class, NO_CYCLE_RISK); 144 148 public static final ChildPropertyDescriptor BODY_PROPERTY = 149 new ChildPropertyDescriptor(MethodDeclaration.class, "body", Block.class, OPTIONAL, CYCLE_RISK); 151 157 private static final List PROPERTY_DESCRIPTORS_2_0; 158 159 165 private static final List PROPERTY_DESCRIPTORS_3_0; 166 167 static { 168 List propertyList = new ArrayList (10); 169 createPropertyList(MethodDeclaration.class, propertyList); 170 addProperty(JAVADOC_PROPERTY, propertyList); 171 addProperty(MODIFIERS_PROPERTY, propertyList); 172 addProperty(CONSTRUCTOR_PROPERTY, propertyList); 173 addProperty(RETURN_TYPE_PROPERTY, propertyList); 174 addProperty(NAME_PROPERTY, propertyList); 175 addProperty(PARAMETERS_PROPERTY, propertyList); 176 addProperty(EXTRA_DIMENSIONS_PROPERTY, propertyList); 177 addProperty(THROWN_EXCEPTIONS_PROPERTY, propertyList); 178 addProperty(BODY_PROPERTY, propertyList); 179 PROPERTY_DESCRIPTORS_2_0 = reapPropertyList(propertyList); 180 181 propertyList = new ArrayList (11); 182 createPropertyList(MethodDeclaration.class, propertyList); 183 addProperty(JAVADOC_PROPERTY, propertyList); 184 addProperty(MODIFIERS2_PROPERTY, propertyList); 185 addProperty(CONSTRUCTOR_PROPERTY, propertyList); 186 addProperty(TYPE_PARAMETERS_PROPERTY, propertyList); 187 addProperty(RETURN_TYPE2_PROPERTY, propertyList); 188 addProperty(NAME_PROPERTY, propertyList); 189 addProperty(PARAMETERS_PROPERTY, propertyList); 190 addProperty(EXTRA_DIMENSIONS_PROPERTY, propertyList); 191 addProperty(THROWN_EXCEPTIONS_PROPERTY, propertyList); 192 addProperty(BODY_PROPERTY, propertyList); 193 PROPERTY_DESCRIPTORS_3_0 = reapPropertyList(propertyList); 194 } 195 196 205 public static List propertyDescriptors(int apiLevel) { 206 if (apiLevel == AST.JLS2_INTERNAL) { 207 return PROPERTY_DESCRIPTORS_2_0; 208 } else { 209 return PROPERTY_DESCRIPTORS_3_0; 210 } 211 } 212 213 217 private boolean isConstructor = false; 218 219 223 private SimpleName methodName = null; 224 225 230 private ASTNode.NodeList parameters = 231 new ASTNode.NodeList(PARAMETERS_PROPERTY); 232 233 239 private Type returnType = null; 240 241 245 private boolean returnType2Initialized = false; 246 247 253 private ASTNode.NodeList typeParameters = null; 254 255 261 private int extraArrayDimensions = 0; 262 263 267 private ASTNode.NodeList thrownExceptions = 268 new ASTNode.NodeList(THROWN_EXCEPTIONS_PROPERTY); 269 270 274 private Block optionalBody = null; 275 276 291 MethodDeclaration(AST ast) { 292 super(ast); 293 if (ast.apiLevel >= AST.JLS3) { 294 this.typeParameters = new ASTNode.NodeList(TYPE_PARAMETERS_PROPERTY); 295 } 296 } 297 298 302 final List internalStructuralPropertiesForType(int apiLevel) { 303 return propertyDescriptors(apiLevel); 304 } 305 306 309 final int internalGetSetIntProperty(SimplePropertyDescriptor property, boolean get, int value) { 310 if (property == MODIFIERS_PROPERTY) { 311 if (get) { 312 return getModifiers(); 313 } else { 314 internalSetModifiers(value); 315 return 0; 316 } 317 } 318 if (property == EXTRA_DIMENSIONS_PROPERTY) { 319 if (get) { 320 return getExtraDimensions(); 321 } else { 322 setExtraDimensions(value); 323 return 0; 324 } 325 } 326 return super.internalGetSetIntProperty(property, get, value); 328 } 329 330 333 final boolean internalGetSetBooleanProperty(SimplePropertyDescriptor property, boolean get, boolean value) { 334 if (property == CONSTRUCTOR_PROPERTY) { 335 if (get) { 336 return isConstructor(); 337 } else { 338 setConstructor(value); 339 return false; 340 } 341 } 342 return super.internalGetSetBooleanProperty(property, get, value); 344 } 345 346 349 final ASTNode internalGetSetChildProperty(ChildPropertyDescriptor property, boolean get, ASTNode child) { 350 if (property == JAVADOC_PROPERTY) { 351 if (get) { 352 return getJavadoc(); 353 } else { 354 setJavadoc((Javadoc) child); 355 return null; 356 } 357 } 358 if (property == NAME_PROPERTY) { 359 if (get) { 360 return getName(); 361 } else { 362 setName((SimpleName) child); 363 return null; 364 } 365 } 366 if (property == RETURN_TYPE_PROPERTY) { 367 if (get) { 368 return getReturnType(); 369 } else { 370 setReturnType((Type) child); 371 return null; 372 } 373 } 374 if (property == RETURN_TYPE2_PROPERTY) { 375 if (get) { 376 return getReturnType2(); 377 } else { 378 setReturnType2((Type) child); 379 return null; 380 } 381 } 382 if (property == BODY_PROPERTY) { 383 if (get) { 384 return getBody(); 385 } else { 386 setBody((Block) child); 387 return null; 388 } 389 } 390 return super.internalGetSetChildProperty(property, get, child); 392 } 393 394 397 final List internalGetChildListProperty(ChildListPropertyDescriptor property) { 398 if (property == MODIFIERS2_PROPERTY) { 399 return modifiers(); 400 } 401 if (property == TYPE_PARAMETERS_PROPERTY) { 402 return typeParameters(); 403 } 404 if (property == PARAMETERS_PROPERTY) { 405 return parameters(); 406 } 407 if (property == THROWN_EXCEPTIONS_PROPERTY) { 408 return thrownExceptions(); 409 } 410 return super.internalGetChildListProperty(property); 412 } 413 414 417 final ChildPropertyDescriptor internalJavadocProperty() { 418 return JAVADOC_PROPERTY; 419 } 420 421 424 final ChildListPropertyDescriptor internalModifiers2Property() { 425 return MODIFIERS2_PROPERTY; 426 } 427 428 431 final SimplePropertyDescriptor internalModifiersProperty() { 432 return MODIFIERS_PROPERTY; 433 } 434 435 438 final int getNodeType0() { 439 return METHOD_DECLARATION; 440 } 441 442 445 ASTNode clone0(AST target) { 446 MethodDeclaration result = new MethodDeclaration(target); 447 result.setSourceRange(this.getStartPosition(), this.getLength()); 448 result.setJavadoc( 449 (Javadoc) ASTNode.copySubtree(target, getJavadoc())); 450 if (this.ast.apiLevel == AST.JLS2_INTERNAL) { 451 result.internalSetModifiers(getModifiers()); 452 result.setReturnType( 453 (Type) ASTNode.copySubtree(target, getReturnType())); 454 } 455 if (this.ast.apiLevel >= AST.JLS3) { 456 result.modifiers().addAll(ASTNode.copySubtrees(target, modifiers())); 457 result.typeParameters().addAll( 458 ASTNode.copySubtrees(target, typeParameters())); 459 result.setReturnType2( 460 (Type) ASTNode.copySubtree(target, getReturnType2())); 461 } 462 result.setConstructor(isConstructor()); 463 result.setExtraDimensions(getExtraDimensions()); 464 result.setName((SimpleName) getName().clone(target)); 465 result.parameters().addAll( 466 ASTNode.copySubtrees(target, parameters())); 467 result.thrownExceptions().addAll( 468 ASTNode.copySubtrees(target, thrownExceptions())); 469 result.setBody( 470 (Block) ASTNode.copySubtree(target, getBody())); 471 return result; 472 } 473 474 477 final boolean subtreeMatch0(ASTMatcher matcher, Object other) { 478 return matcher.match(this, other); 480 } 481 482 485 void accept0(ASTVisitor visitor) { 486 boolean visitChildren = visitor.visit(this); 487 if (visitChildren) { 488 acceptChild(visitor, getJavadoc()); 490 if (this.ast.apiLevel == AST.JLS2_INTERNAL) { 491 acceptChild(visitor, getReturnType()); 492 } else { 493 acceptChildren(visitor, this.modifiers); 494 acceptChildren(visitor, this.typeParameters); 495 acceptChild(visitor, getReturnType2()); 496 } 497 acceptChild(visitor, getName()); 499 acceptChildren(visitor, this.parameters); 500 acceptChildren(visitor, this.thrownExceptions); 501 acceptChild(visitor, getBody()); 502 } 503 visitor.endVisit(this); 504 } 505 506 512 public boolean isConstructor() { 513 return this.isConstructor; 514 } 515 516 522 public void setConstructor(boolean isConstructor) { 523 preValueChange(CONSTRUCTOR_PROPERTY); 524 this.isConstructor = isConstructor; 525 postValueChange(CONSTRUCTOR_PROPERTY); 526 } 527 528 538 public List typeParameters() { 539 if (this.typeParameters == null) { 541 unsupportedIn2(); 542 } 543 return this.typeParameters; 544 } 545 546 553 public SimpleName getName() { 554 if (this.methodName == null) { 555 synchronized (this) { 557 if (this.methodName == null) { 558 preLazyInit(); 559 this.methodName = new SimpleName(this.ast); 560 postLazyInit(this.methodName, NAME_PROPERTY); 561 } 562 } 563 } 564 return this.methodName; 565 } 566 567 579 public void setName(SimpleName methodName) { 580 if (methodName == null) { 581 throw new IllegalArgumentException (); 582 } 583 ASTNode oldChild = this.methodName; 584 preReplaceChild(oldChild, methodName, NAME_PROPERTY); 585 this.methodName = methodName; 586 postReplaceChild(oldChild, methodName, NAME_PROPERTY); 587 } 588 589 596 public List parameters() { 597 return this.parameters; 598 } 599 600 612 public boolean isVarargs() { 613 if (this.modifiers == null) { 615 unsupportedIn2(); 616 } 617 if (parameters().isEmpty()) { 618 return false; 619 } else { 620 SingleVariableDeclaration v = (SingleVariableDeclaration) parameters().get(parameters().size() - 1); 621 return v.isVarargs(); 622 } 623 } 624 625 632 public List thrownExceptions() { 633 return this.thrownExceptions; 634 } 635 636 653 public Type getReturnType() { 654 return internalGetReturnType(); 655 } 656 657 662 final Type internalGetReturnType() { 663 supportedOnlyIn2(); 664 if (this.returnType == null) { 665 synchronized (this) { 667 if (this.returnType == null) { 668 preLazyInit(); 669 this.returnType = this.ast.newPrimitiveType(PrimitiveType.VOID); 670 postLazyInit(this.returnType, RETURN_TYPE_PROPERTY); 671 } 672 } 673 } 674 return this.returnType; 675 } 676 677 697 public void setReturnType(Type type) { 698 internalSetReturnType(type); 699 } 700 701 706 void internalSetReturnType(Type type) { 707 supportedOnlyIn2(); 708 if (type == null) { 709 throw new IllegalArgumentException (); 710 } 711 ASTNode oldChild = this.returnType; 712 preReplaceChild(oldChild, type, RETURN_TYPE_PROPERTY); 713 this.returnType = type; 714 postReplaceChild(oldChild, type, RETURN_TYPE_PROPERTY); 715 } 716 717 735 public Type getReturnType2() { 736 unsupportedIn2(); 737 if (this.returnType == null && !this.returnType2Initialized) { 738 synchronized (this) { 740 if (this.returnType == null && !this.returnType2Initialized) { 741 preLazyInit(); 742 this.returnType = this.ast.newPrimitiveType(PrimitiveType.VOID); 743 this.returnType2Initialized = true; 744 postLazyInit(this.returnType, RETURN_TYPE2_PROPERTY); 745 } 746 } 747 } 748 return this.returnType; 749 } 750 751 773 public void setReturnType2(Type type) { 774 unsupportedIn2(); 775 this.returnType2Initialized = true; 776 ASTNode oldChild = this.returnType; 777 preReplaceChild(oldChild, type, RETURN_TYPE2_PROPERTY); 778 this.returnType = type; 779 postReplaceChild(oldChild, type, RETURN_TYPE2_PROPERTY); 780 } 781 782 797 public int getExtraDimensions() { 798 return this.extraArrayDimensions; 799 } 800 801 818 public void setExtraDimensions(int dimensions) { 819 if (dimensions < 0) { 820 throw new IllegalArgumentException (); 821 } 822 preValueChange(EXTRA_DIMENSIONS_PROPERTY); 823 this.extraArrayDimensions = dimensions; 824 postValueChange(EXTRA_DIMENSIONS_PROPERTY); 825 } 826 827 838 public Block getBody() { 839 return this.optionalBody; 840 } 841 842 860 public void setBody(Block body) { 861 ASTNode oldChild = this.optionalBody; 863 preReplaceChild(oldChild, body, BODY_PROPERTY); 864 this.optionalBody = body; 865 postReplaceChild(oldChild, body, BODY_PROPERTY); 866 } 867 868 879 public IMethodBinding resolveBinding() { 880 return this.ast.getBindingResolver().resolveMethod(this); 881 } 882 883 886 int memSize() { 887 return super.memSize() + 9 * 4; 888 } 889 890 893 int treeSize() { 894 return 895 memSize() 896 + (this.optionalDocComment == null ? 0 : getJavadoc().treeSize()) 897 + (this.modifiers == null ? 0 : this.modifiers.listSize()) 898 + (this.typeParameters == null ? 0 : this.typeParameters.listSize()) 899 + (this.methodName == null ? 0 : getName().treeSize()) 900 + (this.returnType == null ? 0 : this.returnType.treeSize()) 901 + this.parameters.listSize() 902 + this.thrownExceptions.listSize() 903 + (this.optionalBody == null ? 0 : getBody().treeSize()); 904 } 905 } 906 907 | Popular Tags |