1 16 package org.apache.ws.jaxme.js; 17 18 import java.io.IOException ; 19 import java.io.Serializable ; 20 import java.io.StringWriter ; 21 import java.io.Writer ; 22 import java.lang.reflect.Method ; 23 import java.lang.reflect.Modifier ; 24 import java.util.ArrayList ; 25 import java.util.Arrays ; 26 import java.util.Iterator ; 27 import java.util.List ; 28 import java.util.NoSuchElementException ; 29 import java.util.StringTokenizer ; 30 31 32 34 public class JavaSource extends IndentationEngineImpl { 35 37 public static class Type implements Serializable { 38 private String name; 39 Type(String pName) { 40 name = pName; 41 } 42 public String toString() { return name; } 43 45 public static Type valueOf(String pType) { 46 if ("class".equals(pType)) { 47 return CLASS; 48 } else if ("interface".equals(pType)) { 49 return INTERFACE; 50 } else { 51 throw new IllegalArgumentException ("Type must be either 'class' or 'interface'."); 52 } 53 } 54 } 55 56 59 public static class Protection implements Serializable { 60 private String name; 61 Protection(String pName) { 62 name = pName; 63 } 64 public String toString() { return name; } 65 67 public static Protection valueOf(String pProtection) { 68 if ("public".equals(pProtection)) { 69 return PUBLIC; 70 } else if ("protected".equals(pProtection)) { 71 return PROTECTED; 72 } else if ("private".equals(pProtection)) { 73 return PRIVATE; 74 } else if (pProtection == null || "".equals(pProtection)) { 75 return DEFAULT_PROTECTION; 76 } else { 77 throw new IllegalArgumentException ("Protection must be either 'public', 'protected', 'private', null or '' (default protection)."); 78 } 79 } 80 85 public static Protection valueOf(int pModifiers) { 86 if (Modifier.isPublic(pModifiers)) { 87 return PUBLIC; 88 } else if (Modifier.isProtected(pModifiers)) { 89 return PROTECTED; 90 } else if (Modifier.isPrivate(pModifiers)) { 91 return PRIVATE; 92 } else { 93 return DEFAULT_PROTECTION; 94 } 95 } 96 } 97 98 103 public static final Type CLASS = new Type("class"); 104 105 110 public static final Type INTERFACE = new Type("interface"); 111 112 114 public static final Protection PUBLIC = new Protection("public"); 115 116 118 public static final Protection PROTECTED = new Protection("protected"); 119 120 122 public static final Protection PRIVATE = new Protection("private"); 123 124 126 public static final Protection DEFAULT_PROTECTION = new Protection(""); 127 128 134 JavaSource(JavaSourceFactory pFactory, JavaQName pName, Protection pProtection) { 135 factory = pFactory; 136 setQName(pName); 137 setProtection(pProtection); 138 } 139 140 List myObjects = new ArrayList (); 141 142 144 public JavaClassInitializer[] getClassInitializers() { 145 List result = new ArrayList (myObjects); 146 for (Iterator iter = myObjects.iterator(); iter.hasNext(); ) { 147 ConditionalIndentationJavaSourceObject object = 148 (ConditionalIndentationJavaSourceObject) iter.next(); 149 if (object instanceof JavaClassInitializer) { 150 result.add(object); 151 } 152 } 153 return (JavaClassInitializer[]) result.toArray(new JavaClassInitializer[result.size()]); 154 } 155 156 private JavaSourceFactory factory; 157 158 161 public JavaSourceFactory getFactory() { 162 return factory; 163 } 164 165 private JavaQName myQName; 166 168 public JavaQName getQName() { 169 return myQName; 170 } 171 173 public void setQName(JavaQName pQName) { 174 myQName = pQName; 175 } 176 179 public String getClassName() { return myQName.getClassName(); } 180 183 public String getPackageName() { return myQName.getPackageName(); } 184 185 private Protection myProtection; 186 189 public Protection getProtection() { return myProtection; } 190 193 public void setProtection(Protection protection) { 194 myProtection = (protection == null) ? DEFAULT_PROTECTION : protection; 195 } 196 197 private Type type = CLASS; 198 202 public Type getType() { return type; } 203 207 public void setType(Type pType) { 208 this.type = (pType == null) ? CLASS : pType; 209 } 210 211 private JavaComment comment; 212 215 public JavaComment getComment() { return comment; } 216 219 public JavaComment newComment() { 220 if (comment == null) { 221 comment = new JavaComment(); 222 return comment; 223 } else { 224 throw new IllegalStateException ("A Javadoc comment has already been created for this object."); 225 } 226 } 227 228 private List extendedClasses; 229 231 public void clearExtends() { 232 extendedClasses = null; 233 } 234 237 public JavaQName[] getExtends() { 238 if (extendedClasses == null) { 239 return new JavaQName[0]; 240 } else { 241 return (JavaQName[]) extendedClasses.toArray(new JavaQName[extendedClasses.size()]); 242 } 243 } 244 248 public void addExtends(JavaQName pExtends) { 249 if (extendedClasses == null) { 250 extendedClasses = new ArrayList (); 251 } else if ("class".equals(getType())) { 252 throw new IllegalStateException ("Only interfaces may extend multiple classes."); 253 } 254 extendedClasses.add(pExtends); 255 } 256 260 public void addExtends(Class pExtends) { 261 addExtends(JavaQNameImpl.getInstance(pExtends)); 262 } 263 264 266 public boolean isExtending(JavaQName pClass) { 267 for (Iterator iter = extendedClasses.iterator(); iter.hasNext(); ) { 268 if (iter.next().equals(pClass)) { 269 return true; 270 } 271 } 272 return false; 273 } 274 275 277 public boolean isExtending(Class pClass) { 278 return isExtending(JavaQNameImpl.getInstance(pClass)); 279 } 280 281 private ArrayList imports = new ArrayList (); 282 285 public JavaQName[] getImports() { 286 return (JavaQName[]) imports.toArray(new JavaQName[imports.size()]); 287 } 288 289 292 public void addImport(JavaQName s) { 293 if (s.isArray()) { 294 throw new IllegalArgumentException ("Arrays cannot be imported"); 295 } 296 imports.add(s); 297 } 298 299 302 public void addImport(Class s) { imports.add(JavaQNameImpl.getInstance(s)); } 303 304 306 public void clearImports() { imports.clear(); } 307 308 private ArrayList myImplements = new ArrayList (); 309 313 public JavaQName[] getImplements() { 314 return (JavaQName[]) myImplements.toArray(new JavaQName[myImplements.size()]); 315 } 316 317 321 public void addImplements(JavaQName s) { myImplements.add(s); } 322 323 327 public void addImplements(Class s) { myImplements.add(JavaQNameImpl.getInstance(s)); } 328 329 331 public void clearImplements() { myImplements.clear(); } 332 333 335 public boolean isImplementing(JavaQName pClass) { 336 for (Iterator iter = myImplements.iterator(); iter.hasNext(); ) { 337 if (iter.next().equals(pClass)) { 338 return true; 339 } 340 } 341 return false; 342 } 343 344 346 public boolean isImplementing(Class pClass) { 347 return isImplementing(JavaQNameImpl.getInstance(pClass)); 348 } 349 350 private ArrayList myFields = new ArrayList (); 351 353 public JavaField getField(String pName) { 354 if (pName == null) { 355 throw new NullPointerException ("A field name must not be null."); 356 } 357 JavaField[] fields = getFields(); 358 for (int i = 0; i < fields.length; i++) { 359 if (pName.equals(fields[i].getName())) { 360 return fields[i]; 361 } 362 } 363 return null; 364 } 365 368 public JavaField[] getFields() { 369 return (JavaField[]) myFields.toArray(new JavaField[myFields.size()]); 370 } 371 374 void addField(JavaField f) { 375 String s = f.getName(); 376 for (int i = 0; i < myFields.size(); i++) { 377 if (s.equals(((JavaField) myFields.get(i)).getName())) { 378 throw new IllegalStateException ("The class " + getQName() + " already has a field " + s + "."); 379 } 380 } 381 myFields.add(f); 382 } 383 384 387 public JavaConstructor[] getConstructors() { 388 List result = new ArrayList (); 389 for (Iterator iter = myObjects.iterator(); iter.hasNext(); ) { 390 ConditionalIndentationJavaSourceObject object = 391 (ConditionalIndentationJavaSourceObject) iter.next(); 392 if (object instanceof JavaConstructor) { 393 result.add(object); 394 } 395 } 396 return (JavaConstructor[]) result.toArray(new JavaConstructor[result.size()]); 397 } 398 401 void addConstructor(JavaConstructor c) { myObjects.add(c); } 402 405 public Iterator getConstructorIterator() { 406 return new MyObjectIterator(JavaConstructor.class); 407 } 408 409 412 public JavaMethod[] getMethods() { 413 List result = new ArrayList (); 414 for (Iterator iter = myObjects.iterator(); iter.hasNext(); ) { 415 ConditionalIndentationJavaSourceObject object = (ConditionalIndentationJavaSourceObject) iter.next(); 416 if (object instanceof JavaMethod) { 417 result.add(object); 418 } 419 } 420 return (JavaMethod[]) result.toArray(new JavaMethod[result.size()]); 421 } 422 423 426 public JavaMethod getMethod(String pMethodName, JavaQName[] pParams) { 427 for (Iterator iter = myObjects.iterator(); iter.hasNext(); ) { 428 ConditionalIndentationJavaSourceObject object = (ConditionalIndentationJavaSourceObject) iter.next(); 429 if (object instanceof JavaMethod) { 430 JavaMethod jm = (JavaMethod) object; 431 if (jm.getName().equals(pMethodName)) { 432 Parameter[] parameters = jm.getParams(); 433 if (parameters.length == pParams.length) { 434 boolean equals = true; 435 for (int i = 0; i < parameters.length; i++) { 436 if (!parameters[i].getType().equals(pParams[i])) { 437 equals = false; 438 break; 439 } 440 } 441 if (equals) { 442 return jm; 443 } 444 } 445 } 446 } 447 } 448 return null; 449 } 450 451 private class MyObjectIterator implements Iterator { 452 private final Class instanceClass; 453 MyObjectIterator(Class pInstanceClass) { 454 instanceClass = pInstanceClass; 455 } 456 457 private Object result = null; 458 private boolean mayRemove; 459 private Iterator inner = myObjects.iterator(); 460 public void remove() { 461 if (!mayRemove) { 462 throw new IllegalStateException ("remove() is only allowed immediately after next()"); 463 } 464 inner.remove(); 465 mayRemove = false; 466 } 467 public boolean hasNext() { 468 mayRemove = false; 469 if (result != null) { 470 return true; 471 } 472 while (inner.hasNext()) { 473 Object o = inner.next(); 474 if (instanceClass.isAssignableFrom(o.getClass())) { 475 result = o; 476 return true; 477 } 478 } 479 result = null; 480 return false; 481 } 482 public Object next() { 483 if (!hasNext()) { 484 throw new NoSuchElementException (); 485 } 486 Object myResult = result; 487 result = null; 488 mayRemove = true; 489 return myResult; 490 } 491 } 492 493 496 public Iterator getMethodIterator() { 497 return new MyObjectIterator(JavaMethod.class); 498 } 499 500 501 504 void addMethod(JavaMethod m) { myObjects.add(m); } 505 506 507 public void write(IndentationTarget pTarget) throws IOException { 508 if (!isInnerClass()) { 509 String packageName = getPackageName(); 510 if (packageName.length() > 0) { 511 pTarget.indent(0); 512 pTarget.write("package "); 513 pTarget.write(packageName); 514 pTarget.write(";"); 515 pTarget.write(); 516 pTarget.indent(0); 517 pTarget.write(); 518 } 519 } 520 521 JavaQName[] myExtendedClasses = getExtends(); 522 JavaQName[] implementedInterfaces = getImplements(); 523 JavaInnerClass[] myInnerClasses = getInnerClasses(); 524 JavaField[] fields = getFields(); 525 getConstructors(); 526 getMethods(); 527 528 JavaQName[] myImports = getImports(); 529 Arrays.sort(myImports); 530 if (myImports.length > 0) { 531 for (int i = 0; i < myImports.length; i++) { 532 pTarget.indent(0); 533 pTarget.write("import "); 534 pTarget.write(myImports[i].toString()); 535 pTarget.write(";"); 536 pTarget.write(); 537 } 538 pTarget.indent(0); 539 pTarget.write(); 540 pTarget.indent(0); 541 pTarget.write(); 542 } 543 if (comment != null) { 544 comment.write(pTarget); 545 pTarget.indent(0); 546 pTarget.write(); 547 } 548 549 pTarget.indent(0); 550 if (myProtection != null && !myProtection.equals(DEFAULT_PROTECTION)) { 551 pTarget.write(myProtection.toString()); 552 pTarget.write(" "); 553 } 554 if (isStatic) { 555 pTarget.write("static "); 556 } 557 if (isAbstract()) { 558 pTarget.write("abstract "); 559 } 560 pTarget.write(getType().toString()); 561 pTarget.write(" "); 562 String s = getClassName(); 563 int offset = s.lastIndexOf('.'); 564 if (offset > -1) { 565 s = s.substring(offset+1); 566 } 567 offset = s.lastIndexOf('$'); 568 if (offset > -1) { 569 s = s.substring(offset+1); 570 } 571 pTarget.write(s); 572 pTarget.write(" "); 573 for (int i = 0; i < myExtendedClasses.length; i++) { 574 if (i > 0) { 575 pTarget.write(", "); 576 } else { 577 pTarget.write("extends "); 578 } 579 pTarget.write(pTarget.asString(myExtendedClasses[i])); 580 pTarget.write(" "); 581 } 582 if (implementedInterfaces.length > 0) { 583 for (int i = 0; i < implementedInterfaces.length; i++) { 584 if (i == 0) { 585 pTarget.write("implements "); 586 } else { 587 pTarget.write(", "); 588 } 589 pTarget.write(pTarget.asString(implementedInterfaces[i])); 590 pTarget.write(" "); 591 } 592 } 593 pTarget.write("{"); 594 pTarget.write(); 595 596 IncreasingTarget increasingTarget = new IncreasingTarget(pTarget); 597 for (int i = 0; i < myInnerClasses.length; i++) { 598 increasingTarget.setInterface(myInnerClasses[i].isInterface() ? 599 Boolean.TRUE : Boolean.FALSE); 600 myInnerClasses[i].write(increasingTarget); 601 increasingTarget.setInterface(null); 602 pTarget.indent(0); 603 pTarget.write(); 604 } 605 606 if (fields != null && fields.length > 0) { 607 for (int i = 0; i < fields.length; i++) { 608 fields[i].write(increasingTarget); 609 pTarget.indent(0); 610 pTarget.write(); 611 } 612 pTarget.indent(0); 613 pTarget.write(); 614 } 615 616 for (Iterator iter = myObjects.iterator(); iter.hasNext(); ) { 617 ConditionalIndentationJavaSourceObject object = 618 (ConditionalIndentationJavaSourceObject) iter.next(); 619 object.write(increasingTarget); 620 pTarget.indent(0); 621 pTarget.write(); 622 } 623 624 String [] myRawJavaSources = getRawJavaSources(); 625 626 for (int i = 0; i < myRawJavaSources.length; i++) { 627 for (StringTokenizer st = new StringTokenizer (myRawJavaSources[i], "\r\n"); 628 st.hasMoreTokens(); ) { 629 pTarget.indent(0); 630 String tok = st.nextToken(); 631 if (tok.length() > 0) { 632 pTarget.write(tok); 633 } 634 pTarget.write(); 635 } 636 pTarget.indent(0); 637 pTarget.write(); 638 } 639 pTarget.indent(0); 640 pTarget.write("}"); 641 pTarget.write(); 642 } 643 644 647 public static String getQuotedNoQuotes(String s) { 648 StringBuffer sb = new StringBuffer (); 649 for (int i = 0; i < s.length(); i++) { 650 char c = s.charAt(i); 651 if (c == '\n') { 652 sb.append("\\n"); 653 } else if (c == '\\') { 654 sb.append("\\\\"); 655 } else if (c == '\t') { 656 sb.append("\\t"); 657 } else if (c == '\r') { 658 sb.append("\\r"); 659 } else if (c == '\f') { 660 sb.append("\\f"); 661 } else if (c == '\"') { 662 sb.append("\\\""); 663 } else { 664 sb.append(c); 665 } 666 } 667 return sb.toString(); 668 } 669 670 673 public static String getQuoted(String s) { 674 return "\"" + getQuotedNoQuotes(s) + "\""; 675 } 676 677 private java.util.List innerClasses; 678 680 public void addInnerClass(JavaInnerClass pClass) { 681 if (innerClasses == null) { 682 innerClasses = new java.util.ArrayList (); 683 } 684 innerClasses.add(pClass); 685 } 686 688 public void clearInnerClasses() { 689 innerClasses = null; 690 } 691 693 public JavaInnerClass[] getInnerClasses() { 694 if (innerClasses == null) { 695 return new JavaInnerClass[0]; 696 } 697 return (JavaInnerClass[]) 698 innerClasses.toArray(new JavaInnerClass[innerClasses.size()]); 699 } 700 703 public JavaInnerClass getInnerClass(String pName) { 704 if (innerClasses == null) { 705 return null; 706 } 707 for (int i = 0; i < innerClasses.size(); i++) { 708 JavaInnerClass jic = (JavaInnerClass) innerClasses.get(i); 709 if (jic.getQName().getInnerClassName().equals(pName)) { 710 return jic; 711 } 712 } 713 return null; 714 } 715 716 private boolean isStatic; 717 719 protected boolean getStatic() { 720 return isStatic; 721 } 722 723 725 public void setStatic(boolean pStatic) { 726 isStatic = pStatic; 727 } 728 729 private java.util.List rawJavaSources; 730 732 public void addRawJavaSource(String pSource) { 733 if (pSource != null && pSource.length() > 0) { 734 if (rawJavaSources == null) { 735 rawJavaSources = new java.util.ArrayList (); 736 } 737 rawJavaSources.add(pSource); 738 } 739 } 740 741 743 public void clearRawJavaSources() { 744 rawJavaSources = null; 745 } 746 747 749 public String [] getRawJavaSources() { 750 if (rawJavaSources == null) { 751 return new String [0]; 752 } 753 return (String []) rawJavaSources.toArray(new String [rawJavaSources.size()]); 754 } 755 756 private boolean bAbstract; 757 759 public boolean isAbstract() { 760 return bAbstract; 761 } 762 764 public void setAbstract(boolean isAbstract) { 765 this.bAbstract = isAbstract; 766 } 767 768 770 public boolean isInterface() { 771 return INTERFACE.equals(getType()); 772 } 773 774 778 public String asString(JavaQName pQName, boolean pAddIfPossible) { 779 return _asString(pQName, pAddIfPossible).replace('$', '.'); 780 } 781 782 private String _asString(JavaQName pQName, boolean pAddIfPossible) { 783 if (isForcingFullyQualifiedName()) { 784 return pQName.toString(); 785 } 786 if (pQName.isArray()) { 787 return asString(pQName.getInstanceClass(), pAddIfPossible) + "[]"; 788 } 789 if (!pQName.isImportable()) { 790 return pQName.toString(); 791 } 792 if ("".equals(pQName.getPackageName())) { 793 return pQName.getClassName(); 794 } 795 796 JavaQName outerQName = pQName; 797 int offset = outerQName.getClassName().indexOf('$'); 798 if (offset >= 0) { 799 String className = outerQName.getClassName().substring(0, offset); 800 outerQName = JavaQNameImpl.getInstance(outerQName.getPackageName(), className); 801 } 802 803 offset = outerQName.getClassName().indexOf('.'); 804 if (offset >= 0) { 805 String className = pQName.getClassName().substring(0, offset); 806 outerQName = JavaQNameImpl.getInstance(outerQName.getPackageName(), className); 807 } 808 809 if (getQName().equals(outerQName)) { 810 return pQName.getClassName(); 811 } else if (getQName().getClassName().equals(outerQName.getClassName())) { 812 return pQName.toString(); 813 } 814 815 boolean done = false; 816 boolean imported = false; 817 818 for (Iterator iter = imports.iterator(); !done && iter.hasNext(); ) { 819 JavaQName jqName = (JavaQName) iter.next(); 820 if (jqName.equals(outerQName)) { 821 done = true; 822 imported = true; 823 } else if (outerQName.getClassName().equals(jqName.getClassName())) { 824 done = true; 825 imported = false; 826 } 827 } 828 829 if (!done) { 830 String packageName = pQName.getPackageName(); 831 if (packageName.equals(getPackageName()) || 832 packageName.equals("java.lang")) { 833 imported = true; 834 done = true; 835 } 836 if (!done) { 837 if (pAddIfPossible) { 838 addImport(outerQName); 839 done = true; 840 imported = true; 841 } else { 842 done = true; 843 imported = false; 844 } 845 } 846 } 847 848 if (imported) { 849 return pQName.getClassName(); 850 } else { 851 return pQName.toString(); 852 } 853 } 854 855 private boolean forcingFullyQualifiedName = false; 856 859 public boolean isForcingFullyQualifiedName() { 860 return forcingFullyQualifiedName; 861 } 862 865 public void setForcingFullyQualifiedName(boolean pForcingFullyQualifiedName) { 866 forcingFullyQualifiedName = pForcingFullyQualifiedName; 867 } 868 869 private boolean hasDynamicImports = true; 870 872 public boolean hasDynamicImports() { 873 return hasDynamicImports; 874 } 875 877 public void setDynamicImports(boolean pDynamicImports) { 878 hasDynamicImports = pDynamicImports; 879 } 880 881 883 public void write(Writer pTarget) throws IOException { 884 if (hasDynamicImports()) { 885 IndentationTarget devNullTarget = new IndentationTarget(){ 886 public boolean isInterface() { return JavaSource.this.isInterface(); } 887 public String asString(JavaQName pQName) { 888 return JavaSource.this.asString(pQName, true); 889 } 890 public void write(String pValue) {} 891 public void write() {} 892 public void indent(int i) {} 893 }; 894 write(devNullTarget); 895 } 896 WriterTarget wt = new WriterTarget(){ 897 public boolean isInterface() { return JavaSource.this.isInterface(); } 898 public String asString(JavaQName pQName) { 899 return JavaSource.this.asString(pQName, false); 900 } 901 }; 902 wt.setTarget(pTarget); 903 write(wt); 904 } 905 906 908 public String toString() { 909 StringWriter sw = new StringWriter (); 910 try { 911 write(sw); 912 return sw.toString(); 913 } catch (IOException e) { 914 throw new IllegalStateException ("Unexcpected IOException while writing into a StringWriter: " + e.getMessage()); 915 } 916 } 917 918 920 public JavaClassInitializer newJavaClassInitializer() { 921 JavaClassInitializer result = new JavaClassInitializer(); 922 result.setJavaSource(this); 923 myObjects.add(result); 924 return result; 925 } 926 927 929 public JavaConstructor newJavaConstructor() { 930 return newJavaConstructor(DEFAULT_PROTECTION); 931 } 932 933 935 public JavaConstructor newJavaConstructor(JavaSource.Protection pProtection) { 936 JavaConstructor result = new JavaConstructor(getClassName(), pProtection); 937 result.setJavaSource(this); 938 this.addConstructor(result); 939 return result; 940 } 941 942 945 public JavaConstructor newJavaConstructor(String pProtection) { 946 return newJavaConstructor(Protection.valueOf(pProtection)); 947 } 948 949 953 public JavaConstructor newJavaConstructor(JavaConstructor pConstructor) { 954 return newJavaConstructor(pConstructor, false); 955 } 956 957 964 public JavaConstructor newJavaConstructor(JavaConstructor pConstructor, boolean pSuper) { 965 JavaConstructor result = newJavaConstructor(pConstructor.getProtection()); 966 List superParams = pSuper ? new ArrayList () : null; 967 Parameter[] params = pConstructor.getParams(); 968 for (int i = 0; i < params.length; i++) { 969 DirectAccessible p = result.addParam(params[i]); 970 if (pSuper) { 971 if (!superParams.isEmpty()) { 972 superParams.add(", "); 973 } 974 superParams.add(p); 975 } 976 } 977 JavaQName[] exceptions = pConstructor.getExceptions(); 978 for (int i = 0; i < exceptions.length; i++) { 979 result.addThrows(exceptions[i]); 980 } 981 if (pSuper) { 982 result.addLine("super(", superParams, ");"); 983 } 984 return result; 985 } 986 987 990 public JavaMethod newJavaMethod(String pName, String pType) { 991 return newJavaMethod(pName, JavaQNameImpl.getInstance(pType, true), (Protection) null); 992 } 993 994 997 public JavaMethod newJavaMethod(String pName, String pType, Protection pProtection) { 998 return newJavaMethod(pName, JavaQNameImpl.getInstance(pType, true), pProtection); 999 } 1000 1001 1004 public JavaMethod newJavaMethod(String pName, String pType, String pProtection) { 1005 return newJavaMethod(pName, JavaQNameImpl.getInstance(pType, true), 1006 Protection.valueOf(pProtection)); 1007 } 1008 1009 1012 public JavaMethod newJavaMethod(String pName, JavaQName pType) { 1013 return newJavaMethod(pName, pType, DEFAULT_PROTECTION); 1014 } 1015 1016 1019 public JavaMethod newJavaMethod(String pName, JavaQName pType, Protection pProtection) { 1020 JavaMethod result = new JavaMethod(pName, pType, pProtection); 1021 result.setJavaSource(this); 1022 addMethod(result); 1023 return result; 1024 } 1025 1026 1029 public JavaMethod newJavaMethod(String pName, JavaQName pType, String pProtection) { 1030 JavaMethod result = new JavaMethod(pName, pType, Protection.valueOf(pProtection)); 1031 result.setJavaSource(this); 1032 addMethod(result); 1033 return result; 1034 } 1035 1036 1039 public JavaMethod newJavaMethod(String pName, Class pType) { 1040 return newJavaMethod(pName, JavaQNameImpl.getInstance(pType), DEFAULT_PROTECTION); 1041 } 1042 1043 1046 public JavaMethod newJavaMethod(String pName, Class pType, Protection pProtection) { 1047 return newJavaMethod(pName, JavaQNameImpl.getInstance(pType), pProtection); 1048 } 1049 1050 1053 public JavaMethod newJavaMethod(String pName, Class pType, String pProtection) { 1054 return newJavaMethod(pName, JavaQNameImpl.getInstance(pType), 1055 Protection.valueOf(pProtection)); 1056 } 1057 1058 1071 public JavaMethod newJavaMethod(Method pMethod) { 1072 JavaMethod result = newJavaMethod(pMethod.getName(), 1073 JavaQNameImpl.getInstance(pMethod.getReturnType()), 1074 JavaSource.Protection.valueOf(pMethod.getModifiers())); 1075 Class [] parameters = pMethod.getParameterTypes(); 1076 if (parameters != null) { 1077 for (int i = 0; i < parameters.length; i++) { 1078 String parameterName = "p" + i; 1079 result.addParam(parameters[i], parameterName); 1080 } 1081 } 1082 Class [] exceptions = pMethod.getExceptionTypes(); 1083 if (exceptions != null) { 1084 for (int i = 0; i < exceptions.length; i++) { 1085 result.addThrows(exceptions[i]); 1086 } 1087 } 1088 return result; 1089 } 1090 1091 1097 public JavaMethod newJavaMethod(JavaMethod pMethod) { 1098 JavaMethod jm = newJavaMethod(pMethod.getName(), pMethod.getType(), pMethod.getProtection()); 1099 Parameter[] params = pMethod.getParams(); 1100 for (int i = 0; i < params.length; i++) { 1101 jm.addParam(params[i].getType(), params[i].getName()); 1102 } 1103 JavaQName[] exceptions = pMethod.getExceptions(); 1104 for (int i = 0; i < exceptions.length; i++) { 1105 jm.addThrows(exceptions[i]); 1106 } 1107 return jm; 1108 } 1109 1110 1113 public JavaField newJavaField(String pName, JavaQName pType, Protection pProtection) { 1114 JavaField result = new JavaField(pName, pType, pProtection); 1115 result.setJavaSource(this); 1116 addField(result); 1117 return result; 1118 } 1119 1120 1123 public JavaField newJavaField(String pName, JavaQName pType, String pProtection) { 1124 JavaField result = new JavaField(pName, pType, Protection.valueOf(pProtection)); 1125 result.setJavaSource(this); 1126 addField(result); 1127 return result; 1128 } 1129 1130 1133 public JavaField newJavaField(String pName, JavaQName pType) { 1134 return newJavaField(pName, pType, DEFAULT_PROTECTION); 1135 } 1136 1137 1140 public JavaField newJavaField(String pName, String pType, Protection pProtection) { 1141 return newJavaField(pName, JavaQNameImpl.getInstance(pType, true), pProtection); 1142 } 1143 1144 1147 public JavaField newJavaField(String pName, String pType, String pProtection) { 1148 return newJavaField(pName, JavaQNameImpl.getInstance(pType, true), 1149 Protection.valueOf(pProtection)); 1150 } 1151 1152 1155 public JavaField newJavaField(String pName, String pType) { 1156 return newJavaField(pName, JavaQNameImpl.getInstance(pType, true), (Protection) null); 1157 } 1158 1159 1162 public JavaField newJavaField(String pName, Class pType, JavaSource.Protection pProtection) { 1163 return newJavaField(pName, JavaQNameImpl.getInstance(pType), pProtection); 1164 } 1165 1166 1170 public JavaField newJavaField(String pName, Class pType, String pProtection) { 1171 return newJavaField(pName, JavaQNameImpl.getInstance(pType), 1172 Protection.valueOf(pProtection)); 1173 } 1174 1175 1178 public JavaField newJavaField(String pName, Class pType) { 1179 return newJavaField(pName, JavaQNameImpl.getInstance(pType)); 1180 } 1181 1182 1185 public JavaInnerClass newJavaInnerClass(String pName) { 1186 return newJavaInnerClass(pName, DEFAULT_PROTECTION); 1187 } 1188 1189 1192 public JavaInnerClass newJavaInnerClass(String pName, Protection pProtection) { 1193 JavaQName innerClassName = JavaQNameImpl.getInnerInstance(getQName(), pName); 1194 JavaInnerClass result = new JavaInnerClass(this, innerClassName, pProtection); 1195 addInnerClass(result); 1196 return result; 1197 } 1198 1199 1202 public JavaInnerClass newJavaInnerClass(String pName, String pProtection) { 1203 return newJavaInnerClass(pName, Protection.valueOf(pProtection)); 1204 } 1205 1206 1208 public boolean isInnerClass() { 1209 return false; 1210 } 1211 1223 public void newBeanProperty(JavaQName pType, String pName) { 1224 String upperCaseName = Character.toUpperCase(pName.charAt(0)) + pName.substring(1); 1225 if (JavaQNameImpl.VOID.equals(pType)) { 1226 newBeanProperty(pType, pName, "is" + upperCaseName, "set" + upperCaseName); 1227 } else { 1228 newBeanProperty(pType, pName, "get" + upperCaseName, "set" + upperCaseName); 1229 } 1230 } 1231 1232 1235 public void newBeanProperty(Class pClass, String pName) { 1236 newBeanProperty(JavaQNameImpl.getInstance(pClass), pName); 1237 } 1238 1239 1256 public void newBeanProperty(JavaQName pType, String pFieldName, 1257 String pGetMethodName, String pSetMethodName) { 1258 newJavaField(pFieldName, pType, JavaSource.PRIVATE); 1259 if (pGetMethodName != null) { 1260 JavaMethod getMethod = newJavaMethod(pGetMethodName, pType, JavaSource.PUBLIC); 1261 getMethod.addLine("return this.", pFieldName, ";"); 1262 } 1263 if (pSetMethodName != null) { 1264 JavaMethod setMethod = newJavaMethod(pSetMethodName, JavaQNameImpl.VOID, JavaSource.PUBLIC); 1265 setMethod.addParam(pType, pFieldName); 1266 setMethod.addLine("this.", pFieldName, " = ", pFieldName, ";"); 1267 } 1268 } 1269 1270 1274 public void newBeanProperty(Class pClass, String pFieldName, 1275 String pGetMethodName, String pSetMethodName) { 1276 newBeanProperty(JavaQNameImpl.getInstance(pClass), pFieldName, pGetMethodName, 1277 pSetMethodName); 1278 } 1279} 1280 | Popular Tags |