1 11 package org.eclipse.jdt.internal.core.jdom; 12 13 import org.eclipse.jdt.core.Flags; 14 import org.eclipse.jdt.core.IJavaElement; 15 import org.eclipse.jdt.core.IType; 16 import org.eclipse.jdt.core.Signature; 17 import org.eclipse.jdt.core.compiler.CharOperation; 18 import org.eclipse.jdt.core.jdom.*; 19 import org.eclipse.jdt.internal.core.util.Messages; 20 import org.eclipse.jdt.internal.core.util.CharArrayBuffer; 21 import org.eclipse.jdt.internal.core.util.Util; 22 31 class DOMMethod extends DOMMember implements IDOMMethod { 33 34 39 protected String fReturnType; 40 41 51 protected int[] fReturnTypeRange; 52 53 59 protected char[] fParameterList; 60 61 65 protected int[] fParameterRange; 66 67 75 protected char[] fExceptionList; 76 77 81 protected int[] fExceptionRange; 82 83 90 protected String fBody; 91 92 96 protected int[] fBodyRange; 97 98 99 103 protected String [] fParameterNames; 104 105 109 protected String [] fParameterTypes; 110 111 115 protected String [] fExceptions; 116 117 121 protected String [] fTypeParameters = CharOperation.NO_STRINGS; 122 123 128 protected String fDefaultValue = null; 129 130 133 DOMMethod() { 134 } 136 197 DOMMethod(char[] document, int[] sourceRange, String name, int[] nameRange, int[] commentRange, int flags, int[] modifierRange, boolean isConstructor, String returnType, int[] returnTypeRange, String [] parameterTypes, String [] parameterNames, int[] parameterRange, String [] exceptions, int[] exceptionRange, int[] bodyRange) { 198 super(document, sourceRange, name, nameRange, commentRange, flags, modifierRange); 199 200 setMask(MASK_IS_CONSTRUCTOR, isConstructor); 201 fReturnType= returnType; 202 fReturnTypeRange= returnTypeRange; 203 fParameterTypes= parameterTypes; 204 fParameterNames= parameterNames; 205 fParameterRange= parameterRange; 206 fExceptionRange= exceptionRange; 207 fExceptions= exceptions; 208 setHasBody(true); 209 fBodyRange= bodyRange; 210 setMask(MASK_DETAILED_SOURCE_INDEXES, true); 211 212 } 213 240 DOMMethod(char[] document, int[] sourceRange, String name, int[] nameRange, int flags, boolean isConstructor, String returnType, String [] parameterTypes, String [] parameterNames, String [] exceptions) { 241 this(document, sourceRange, name, nameRange, new int[] {-1, -1}, flags, new int[] {-1, -1}, isConstructor, returnType, new int[] {-1, -1}, parameterTypes, parameterNames, new int[] {-1, -1}, exceptions, new int[] {-1, -1}, new int[] {-1, -1}); 242 setMask(MASK_DETAILED_SOURCE_INDEXES, false); 243 } 244 247 public void addException(String name) throws IllegalArgumentException { 248 if (name == null) { 249 throw new IllegalArgumentException (Messages.dom_nullExceptionType); 250 } 251 if (fExceptions == null) { 252 fExceptions= new String [1]; 253 fExceptions[0]= name; 254 } else { 255 fExceptions= appendString(fExceptions, name); 256 } 257 setExceptions(fExceptions); 258 } 259 262 public void addParameter(String type, String name) throws IllegalArgumentException { 263 if (type == null) { 264 throw new IllegalArgumentException (Messages.dom_nullTypeParameter); 265 } 266 if (name == null) { 267 throw new IllegalArgumentException (Messages.dom_nullNameParameter); 268 } 269 if (fParameterNames == null) { 270 fParameterNames= new String [1]; 271 fParameterNames[0]= name; 272 } else { 273 fParameterNames= appendString(fParameterNames, name); 274 } 275 if (fParameterTypes == null) { 276 fParameterTypes= new String [1]; 277 fParameterTypes[0]= type; 278 } else { 279 fParameterTypes= appendString(fParameterTypes, type); 280 } 281 setParameters(fParameterTypes, fParameterNames); 282 } 283 286 protected void appendMemberBodyContents(CharArrayBuffer buffer) { 287 if (fBody != null) { 288 buffer.append(fBody); 289 } else { 290 buffer.append(fDocument, fBodyRange[0], fBodyRange[1] + 1 - fBodyRange[0]); 291 } 292 } 293 296 protected void appendMemberDeclarationContents(CharArrayBuffer buffer) { 297 298 if (isConstructor()) { 299 buffer 300 .append(getConstructorName()) 301 .append(fDocument, fNameRange[1] + 1, fParameterRange[0] - fNameRange[1] - 1); 302 } else { 303 buffer.append(getReturnTypeContents()); 304 if (fReturnTypeRange[0] >= 0) { 305 buffer.append(fDocument, fReturnTypeRange[1] + 1, fNameRange[0] - fReturnTypeRange[1] - 1); 306 } else { 307 buffer.append(' '); 308 } 309 buffer 310 .append(getNameContents()) 311 .append(fDocument, fNameRange[1] + 1, fParameterRange[0] - fNameRange[1] - 1); 312 } 313 if (fParameterList != null) { 314 buffer.append(fParameterList); 315 } else { 316 buffer.append(fDocument, fParameterRange[0], fParameterRange[1] + 1 - fParameterRange[0]); 317 } 318 int start; 319 if (hasTrailingArrayQualifier() && isReturnTypeAltered()) { 320 start= fReturnTypeRange[3] + 1; 321 } else { 322 start= fParameterRange[1] + 1; 323 } 324 if (fExceptions != null) { 325 if (fExceptionRange[0] >= 0) { 327 buffer.append(fDocument, start, fExceptionRange[0] - start); 328 } else { 329 buffer.append(" throws "); } 331 if (fExceptionList != null) { 333 buffer.append(fExceptionList); 334 if (fExceptionRange[0] >= 0) { 336 buffer.append(fDocument, fExceptionRange[1] + 1, fBodyRange[0] - fExceptionRange[1] - 1); 337 } else { 338 buffer.append(fDocument, fParameterRange[1] + 1, fBodyRange[0] - fParameterRange[1] - 1); 339 } 340 } else { 341 buffer.append(fDocument, fExceptionRange[0], fBodyRange[0] - fExceptionRange[0]); 343 } 344 } else { 345 if (fExceptionRange[0] >= 0) { 347 buffer.append(fDocument, fExceptionRange[1] + 1, fBodyRange[0] - fExceptionRange[1] - 1); 348 } else { 349 buffer.append(fDocument, start, fBodyRange[0] - start); 350 } 351 } 352 353 } 354 357 protected void appendSimpleContents(CharArrayBuffer buffer) { 358 buffer.append(fDocument, fSourceRange[0], fNameRange[0] - fSourceRange[0]); 360 if (isConstructor()) { 362 buffer.append(getConstructorName()); 363 } else { 364 buffer.append(fName); 365 } 366 buffer.append(fDocument, fNameRange[1] + 1, fSourceRange[1] - fNameRange[1]); 368 } 369 372 public String getBody() { 373 becomeDetailed(); 374 if (hasBody()) { 375 if (fBody != null) { 376 return fBody; 377 } else { 378 return new String (fDocument, fBodyRange[0], fBodyRange[1] + 1 - fBodyRange[0]); 379 } 380 } else { 381 return null; 382 } 383 } 384 389 protected String getConstructorName() { 390 391 if (isConstructor()) { 392 if (getParent() != null) { 393 return getParent().getName(); 394 } else { 395 return new String (getNameContents()); 397 } 398 } else { 399 return null; 400 } 401 402 } 403 406 protected DOMNode getDetailedNode() { 407 return (DOMNode)getFactory().createMethod(getContents()); 408 } 409 412 public String [] getExceptions() { 413 return fExceptions; 414 } 415 protected char[] generateFlags() { 416 char[] flags= Flags.toString(getFlags() & ~Flags.AccVarargs).toCharArray(); 417 if (flags.length == 0) { 418 return flags; 419 } else { 420 return CharOperation.concat(flags, new char[] {' '}); 421 } 422 } 425 public IJavaElement getJavaElement(IJavaElement parent) throws IllegalArgumentException { 426 if (parent.getElementType() == IJavaElement.TYPE) { 427 String [] sigs= null; 429 if (fParameterTypes != null) { 430 sigs= new String [fParameterTypes.length]; 431 int i; 432 for (i= 0; i < fParameterTypes.length; i++) { 433 sigs[i]= Signature.createTypeSignature(fParameterTypes[i].toCharArray(), false); 434 } 435 } 436 String name= null; 437 if (isConstructor()) { 438 name= getConstructorName(); 439 } else { 440 name= getName(); 441 } 442 return ((IType)parent).getMethod(name, sigs); 443 } else { 444 throw new IllegalArgumentException (Messages.element_illegalParent); 445 } 446 } 447 450 protected int getMemberDeclarationStartPosition() { 451 if (fReturnTypeRange[0] >= 0) { 452 return fReturnTypeRange[0]; 453 } else { 454 return fNameRange[0]; 455 } 456 } 457 460 public String getName() { 461 if (isConstructor()) { 462 return null; 463 } else { 464 return super.getName(); 465 } 466 } 467 470 public int getNodeType() { 471 return IDOMNode.METHOD; 472 } 473 476 public String [] getParameterNames() { 477 return fParameterNames; 478 } 479 482 public String [] getParameterTypes() { 483 return fParameterTypes; 484 } 485 488 public String getReturnType() { 489 if (isConstructor()) { 490 return null; 491 } else { 492 return fReturnType; 493 } 494 } 495 498 protected char[] getReturnTypeContents() { 499 if (isConstructor()) { 500 return null; 501 } else { 502 if (isReturnTypeAltered()) { 503 return fReturnType.toCharArray(); 504 } else { 505 return CharOperation.subarray(fDocument, fReturnTypeRange[0], fReturnTypeRange[1] + 1); 506 } 507 508 } 509 } 510 514 protected boolean hasTrailingArrayQualifier() { 515 return fReturnTypeRange.length > 2; 516 } 517 520 public boolean isConstructor() { 521 return getMask(MASK_IS_CONSTRUCTOR); 522 } 523 527 protected boolean isReturnTypeAltered() { 528 return getMask(MASK_RETURN_TYPE_ALTERED); 529 } 530 536 public boolean isSignatureEqual(IDOMNode node) { 537 boolean ok= node.getNodeType() == getNodeType(); 538 if (ok) { 539 IDOMMethod method= (IDOMMethod)node; 540 ok = (isConstructor() && method.isConstructor()) || 541 (!isConstructor() && !method.isConstructor()); 542 if (ok && !isConstructor()) { 543 ok= getName().equals(method.getName()); 544 } 545 if (!ok) { 546 return false; 547 } 548 549 String [] types= method.getParameterTypes(); 550 if (fParameterTypes == null || fParameterTypes.length == 0) { 551 if (types == null || types.length == 0) { 553 return true; 555 } 556 } else { 557 if (types == null || types.length == 0) { 559 return false; 561 } 562 if (fParameterTypes.length != types.length) { 563 return false; 565 } 566 int i; 567 for (i= 0; i < types.length; i++) { 568 if (!fParameterTypes[i].equals(types[i])) { 569 return false; 570 } 571 } 572 return true; 573 } 574 } 575 return false; 576 577 } 578 581 protected DOMNode newDOMNode() { 582 return new DOMMethod(); 583 } 584 587 protected void offset(int offset) { 588 super.offset(offset); 589 offsetRange(fBodyRange, offset); 590 offsetRange(fExceptionRange, offset); 591 offsetRange(fParameterRange, offset); 592 offsetRange(fReturnTypeRange, offset); 593 } 594 597 public void setBody(String body) { 598 becomeDetailed(); 599 fragment(); 600 fBody= body; 601 setHasBody(body != null); 602 if (!hasBody()) { 603 fBody= ";" + Util.getLineSeparator(body, null); } 605 } 606 609 void setBodyRangeEnd(int end) { 610 fBodyRange[1] = end; 611 } 612 615 public void setConstructor(boolean b) { 616 becomeDetailed(); 617 setMask(MASK_IS_CONSTRUCTOR, b); 618 fragment(); 619 } 620 623 public void setExceptions(String [] names) { 624 becomeDetailed(); 625 if (names == null || names.length == 0) { 626 fExceptions= null; 627 } else { 628 fExceptions= names; 629 CharArrayBuffer buffer = new CharArrayBuffer(); 630 char[] comma = new char[] {',', ' '}; 631 for (int i = 0, length = names.length; i < length; i++) { 632 if (i > 0) 633 buffer.append(comma); 634 buffer.append(names[i]); 635 } 636 fExceptionList= buffer.getContents(); 637 } 638 fragment(); 639 } 640 643 public void setName(String name) { 644 if (name == null) { 645 throw new IllegalArgumentException (Messages.element_nullName); 646 } else { 647 super.setName(name); 648 } 649 } 650 653 public void setParameters(String [] types, String [] names) throws IllegalArgumentException { 654 becomeDetailed(); 655 if (types== null || names == null) { 656 if (types == null && names == null) { 657 fParameterTypes= null; 658 fParameterNames= null; 659 fParameterList= new char[] {'(',')'}; 660 } else { 661 throw new IllegalArgumentException (Messages.dom_mismatchArgNamesAndTypes); 662 } 663 } else if (names.length != types.length) { 664 throw new IllegalArgumentException (Messages.dom_mismatchArgNamesAndTypes); 665 } else if (names.length == 0) { 666 setParameters(null, null); 667 } else { 668 fParameterNames= names; 669 fParameterTypes= types; 670 CharArrayBuffer parametersBuffer = new CharArrayBuffer(); 671 parametersBuffer.append("("); char[] comma = new char[] {',', ' '}; 673 for (int i = 0; i < names.length; i++) { 674 if (i > 0) { 675 parametersBuffer.append(comma); 676 } 677 parametersBuffer 678 .append(types[i]) 679 .append(' ') 680 .append(names[i]); 681 } 682 parametersBuffer.append(')'); 683 fParameterList= parametersBuffer.getContents(); 684 } 685 fragment(); 686 } 687 690 public void setReturnType(String name) throws IllegalArgumentException { 691 if (name == null) { 692 throw new IllegalArgumentException (Messages.dom_nullReturnType); 693 } 694 becomeDetailed(); 695 fragment(); 696 setReturnTypeAltered(true); 697 fReturnType= name; 698 } 699 703 protected void setReturnTypeAltered(boolean typeAltered) { 704 setMask(MASK_RETURN_TYPE_ALTERED, typeAltered); 705 } 706 708 protected void setSourceRangeEnd(int end) { 709 super.setSourceRangeEnd(end); 710 fBodyRange[1]= end; 711 } 712 715 protected void shareContents(DOMNode node) { 716 super.shareContents(node); 717 DOMMethod method= (DOMMethod)node; 718 fBody= method.fBody; 719 fBodyRange= rangeCopy(method.fBodyRange); 720 fExceptionList= method.fExceptionList; 721 fExceptionRange= rangeCopy(method.fExceptionRange); 722 fExceptions= method.fExceptions; 723 fParameterList= method.fParameterList; 724 fParameterNames= method.fParameterNames; 725 fParameterRange= rangeCopy(method.fParameterRange); 726 fParameterTypes= method.fParameterTypes; 727 fReturnType= method.fReturnType; 728 fReturnTypeRange= rangeCopy(method.fReturnTypeRange); 729 } 730 733 public String toString() { 734 if (isConstructor()) { 735 return "CONSTRUCTOR"; } else { 737 return "METHOD: " + getName(); } 739 } 740 741 745 public void setDefault(String defaultValue) { 746 this.fDefaultValue = defaultValue; 747 } 748 749 753 public String getDefault() { 754 return this.fDefaultValue; 755 } 756 757 761 public String [] getTypeParameters() { 762 return this.fTypeParameters; 763 } 764 765 769 public void setTypeParameters(String [] typeParameters) { 770 this.fTypeParameters = typeParameters; 771 } 772 } 773 | Popular Tags |