1 5 package xdoclet.tagshandler; 6 7 8 import java.util.*; 9 10 import org.apache.commons.logging.Log; 11 12 import xjavadoc.*; 13 14 import xdoclet.XDocletException; 15 import xdoclet.XDocletMessages; 16 import xdoclet.util.DocletUtil; 17 import xdoclet.util.LogUtil; 18 import xdoclet.util.Translator; 19 20 26 public class MethodTagsHandler extends AbstractProgramElementTagsHandler 27 { 28 public static String getMethodTypeFor(XMethod method) 29 { 30 return method.getReturnType().getType().getQualifiedName() + method.getReturnType().getDimensionAsString(); 31 } 32 33 public static String getTransformedMethodTypeFor(XMethod method) 34 { 35 return method.getReturnType().getType().getTransformedQualifiedName() + method.getReturnType().getDimensionAsString(); 36 } 37 38 44 public static String getMethodNameWithoutPrefixFor(XMethod currentMethod) 45 { 46 String propertyName = currentMethod.getPropertyName(); 47 48 if (Character.isLowerCase(propertyName.charAt(0))) 49 return Character.toUpperCase(propertyName.charAt(0)) + propertyName.substring(1); 50 else 51 return propertyName; 52 } 53 54 60 public static String getPropertyNameFor(XMethod method) 61 { 62 return method.getPropertyName(); 63 } 64 65 71 public static boolean isGetter(String str) 72 { 73 return str.startsWith("get") || str.startsWith("is"); 74 } 75 76 82 public static boolean isSetter(String str) 83 { 84 return str.startsWith("set"); 85 } 86 87 public static boolean isGetterMethod(XMethod method) 88 { 89 return method.isPropertyAccessor(); 90 } 91 92 public static boolean isSetterMethod(XMethod method) 93 { 94 return method.isPropertyMutator(); 95 } 96 97 110 public static boolean hasMethod(XClass clazz, String methodName, String [] parameters, boolean setCurrentMethod) 111 throws XDocletException 112 { 113 return hasExecutableMember(clazz, methodName, parameters, setCurrentMethod, FOR_METHOD); 114 } 115 116 124 public String getterPrefix() throws XDocletException 125 { 126 if (getCurrentMethod().getName().startsWith("get")) { 127 return "get"; 128 } 129 else if (getCurrentMethod().getName().startsWith("is")) { 130 return "is"; 131 } 132 else if (getCurrentMethod().getName().startsWith("set")) { 133 String [] params = {getCurrentMethod().getReturnType().getType().getQualifiedName()}; 135 136 if (hasMethod(getCurrentClass(), "is" + methodNameWithoutPrefix(), params, false)) { 137 return "is"; 138 } 139 else { 140 return "get"; 141 } 142 } 143 144 return ""; 145 } 146 147 157 public String getterMethod() throws XDocletException 158 { 159 return getterPrefix() + methodNameWithoutPrefix(); 160 } 161 162 172 public String setterMethod() throws XDocletException 173 { 174 return "set" + methodNameWithoutPrefix(); 175 } 176 177 195 public void setCurrentMethod(String template, Properties attributes) throws XDocletException 196 { 197 String methodName = attributes.getProperty("name"); 198 String parametersStr = attributes.getProperty("parameters"); 199 String delimiter = attributes.getProperty("delimiter"); 200 201 String [] parameters = null; 202 203 if (parametersStr != null) { 204 if (delimiter == null) { 205 delimiter = PARAMETER_DELIMITER; 206 } 207 208 parameters = DocletUtil.tokenizeDelimitedToArray(parametersStr, delimiter); 209 } 210 211 XMethod oldMethod = getCurrentMethod(); 212 213 if (hasMethod(getCurrentClass(), methodName, parameters, true)) { 214 generate(template); 215 } 216 217 setCurrentMethod(oldMethod); 218 } 219 220 227 public String modifiers() throws XDocletException 228 { 229 return modifiers(FOR_METHOD); 230 } 231 232 245 public String methodComment(Properties attributes) throws XDocletException 246 { 247 return memberComment(attributes, FOR_METHOD); 248 } 249 250 257 public void ifHasMethodComment(String template) throws XDocletException 258 { 259 Properties attributes = new Properties(); 260 261 attributes.setProperty("no-comment-signs", "true"); 262 263 String comment = methodComment(attributes); 264 265 if (!comment.trim().equals("")) { 266 generate(template); 267 } 268 } 269 270 285 public String exceptionList(Properties attributes) throws XDocletException 286 { 287 return exceptionList(attributes, FOR_METHOD); 288 } 289 290 301 public void ifIsAbstract(String template, Properties attributes) throws XDocletException 302 { 303 if (isAbstract(attributes)) { 304 generate(template); 305 } 306 } 307 308 319 public void ifIsNotAbstract(String template, Properties attributes) throws XDocletException 320 { 321 if (!isAbstract(attributes)) { 322 generate(template); 323 } 324 } 325 326 337 public void ifReturnsVoid(String template, Properties attributes) throws XDocletException 338 { 339 if (returnsVoid(attributes)) { 340 generate(template); 341 } 342 } 343 344 355 public void ifDoesntReturnVoid(String template, Properties attributes) throws XDocletException 356 { 357 if (!returnsVoid(attributes)) { 358 generate(template); 359 } 360 } 361 362 375 public void forAllClassMethods(String template, Properties attributes) throws XDocletException 376 { 377 String typeName = attributes.getProperty("type"); 378 int extent = TypeTagsHandler.extractExtentType(attributes.getProperty("extent")); 379 380 Collection classes = getAllClasses(); 381 SortedSet methods = new TreeSet(); 382 383 for (Iterator i = classes.iterator(); i.hasNext(); ) { 384 XClass clazz = (XClass) i.next(); 385 386 if (typeName == null || TypeTagsHandler.isOfType(clazz, typeName, extent)) { 387 Collection classMethods = clazz.getMethods(); 388 389 methods.addAll(classMethods); 390 391 } 392 } 393 394 Iterator methodIterator = methods.iterator(); 395 396 while (methodIterator.hasNext()) { 397 XMethod current = (XMethod) methodIterator.next(); 398 399 setCurrentClass(current.getContainingClass()); 400 setCurrentMethod(current); 401 402 generate(template); 403 } 404 } 405 406 418 public void forAllMethods(String template, Properties attributes) throws XDocletException 419 { 420 forAllMembers(template, attributes, FOR_METHOD); 421 } 422 423 437 public void ifDoesntHaveMethodTag(String template, Properties attributes) throws XDocletException 438 { 439 if (!hasTag(attributes, FOR_METHOD)) { 440 generate(template); 441 } 442 else { 443 String error = attributes.getProperty("error"); 444 445 if (error != null) { 446 getEngine().print(error); 447 } 448 } 449 } 450 451 465 public void ifHasMethodTag(String template, Properties attributes) throws XDocletException 466 { 467 if (hasTag(attributes, FOR_METHOD)) { 468 generate(template); 469 } 470 else { 471 String error = attributes.getProperty("error"); 472 473 if (error != null) { 474 getEngine().print(error); 475 } 476 } 477 } 478 479 487 public void executeAndRestoreMethod(String template, Properties attributes) throws XDocletException 488 { 489 XMethod method = getCurrentMethod(); 490 491 generate(template); 492 setCurrentMethod(method); 493 } 494 495 508 public void ifMethodTagValueEquals(String template, Properties attributes) throws XDocletException 509 { 510 if (isTagValueEqual(attributes, FOR_METHOD)) { 511 generate(template); 512 } 513 } 514 515 524 public void ifMethodNameEquals(String template, Properties attributes) throws XDocletException 525 { 526 ifMethodNameEquals_Impl(template, attributes, true); 527 } 528 529 538 public void ifMethodNameNotEquals(String template, Properties attributes) throws XDocletException 539 { 540 ifMethodNameEquals_Impl(template, attributes, false); 541 } 542 543 556 public void ifMethodTagValueNotEquals(String template, Properties attributes) throws XDocletException 557 { 558 if (!isTagValueEqual(attributes, FOR_METHOD)) { 559 generate(template); 560 } 561 } 562 563 581 public String methodTagValue(Properties attributes) throws XDocletException 582 { 583 return getExpandedDelimitedTagValue(attributes, FOR_METHOD); 584 } 585 586 595 public void forAllMethodTags(String template, Properties attributes) throws XDocletException 596 { 597 forAllMemberTags(template, attributes, FOR_METHOD, XDocletTagshandlerMessages.ONLY_CALL_METHOD_NOT_NULL, new String []{"forAllMethodTags"}); 598 } 599 600 612 public void forAllMethodTagTokens(String template, Properties attributes) throws XDocletException 613 { 614 forAllMemberTagTokens(template, attributes, FOR_METHOD); 615 } 616 617 624 public String firstSentenceDescriptionOfCurrentMethod() throws XDocletException 625 { 626 return firstSentenceDescriptionOfCurrentMember(getCurrentMethod()); 627 } 628 629 637 public String methodType(Properties attributes) throws XDocletException 638 { 639 return getMethodTypeFor(getCurrentMethod()); 640 } 641 642 650 public String transformedMethodType(Properties attributes) throws XDocletException 651 { 652 return getTransformedMethodTypeFor(getCurrentMethod()); 653 } 654 655 664 public void ifIsOfType(String template, Properties attributes) throws XDocletException 665 { 666 if (ifIsOfTypeImpl(template, attributes)) 667 generate(template); 668 } 669 670 679 public void ifIsNotOfType(String template, Properties attributes) throws XDocletException 680 { 681 if (!ifIsOfTypeImpl(template, attributes)) 682 generate(template); 683 } 684 685 public boolean ifIsOfTypeImpl(String template, Properties attributes) throws XDocletException 686 { 687 return methodType(attributes).equals(attributes.getProperty("type")); 688 } 689 690 698 public String methodName(Properties attributes) throws XDocletException 699 { 700 if (attributes != null) { 701 String value = (String ) attributes.get("value"); 702 703 if (value != null) { 704 String m = getCurrentMethod().getName().substring(Integer.parseInt(value)); 705 char firstU = m.charAt(0); 707 char firstL = Character.toLowerCase(firstU); 708 709 return firstL + m.substring(1); 710 } 711 } 712 713 return getCurrentMethod() != null ? getCurrentMethod().getName() : ""; 714 } 715 716 724 public String methodNameWithoutPrefix() throws XDocletException 725 { 726 return getMethodNameWithoutPrefixFor(getCurrentMethod()); 727 } 728 729 735 public String currentMethodName() throws XDocletException 736 { 737 return getCurrentMethod().getName(); 738 } 739 740 748 public String propertyName() throws XDocletException 749 { 750 return getPropertyNameFor(getCurrentMethod()); 751 } 752 753 771 public void ifHasMethod(String template, Properties attributes) throws XDocletException 772 { 773 ifHasMethod_Impl(template, attributes, true); 774 } 775 776 793 public void ifDoesntHaveMethod(String template, Properties attributes) throws XDocletException 794 { 795 ifHasMethod_Impl(template, attributes, false); 796 } 797 798 807 public void ifIsGetter(String template, Properties attributes) throws XDocletException 808 { 809 String method_name = attributes.getProperty("method"); 810 811 if (method_name != null) { 812 if (isGetter(method_name)) { 813 generate(template); 814 } 815 } 816 else { 817 if (isGetterMethod(getCurrentMethod())) { 818 generate(template); 819 } 820 } 821 } 822 823 832 public void ifIsSetter(String template, Properties attributes) throws XDocletException 833 { 834 String method_name = attributes.getProperty("method"); 835 836 if (method_name != null) { 837 if (isSetter(method_name)) { 838 generate(template); 839 } 840 } 841 else { 842 if (isSetterMethod(getCurrentMethod())) { 843 generate(template); 844 } 845 } 846 } 847 848 855 public void ifIsPublic(String template) throws XDocletException 856 { 857 if (getCurrentMethod().isPublic()) { 858 generate(template); 859 } 860 } 861 862 876 public void ifThrowsException(String template, Properties attributes) throws XDocletException 877 { 878 if (throwsException(attributes)) { 879 generate(template); 880 } 881 } 882 883 897 public void ifDoesntThrowException(String template, Properties attributes) throws XDocletException 898 { 899 if (!throwsException(attributes)) { 900 generate(template); 901 } 902 } 903 904 private boolean isAbstract(Properties attributes) throws XDocletException 905 { 906 String methodName = attributes.getProperty("method"); 907 908 if (methodName == null) { 909 return getCurrentMethod().isAbstract(); 910 } 911 else { 912 XMethod method = (XMethod) getXExecutableMemberForMemberName(methodName, true, FOR_METHOD); 913 914 if (method == null) { 916 throw new XDocletException(Translator.getString(XDocletMessages.class, XDocletMessages.METHOD_NOT_FOUND, new String []{methodName})); 917 } 918 919 return method.isAbstract(); 920 } 921 } 922 923 930 private boolean throwsException(Properties attributes) throws XDocletException 931 { 932 String checkExceptions = attributes.getProperty("exceptions"); 933 String memberName = null; 934 Collection exceptions = null; 935 936 XExecutableMember executableMember = null; 937 938 executableMember = getCurrentMethod(); 939 memberName = attributes.getProperty("method"); 940 941 if (executableMember == null && memberName == null) { 942 exceptions = new ArrayList(); 943 } 944 945 if (memberName == null) { 946 exceptions = executableMember.getThrownExceptions(); 947 } 948 else { 949 executableMember = getXExecutableMemberForMemberName(memberName, true, FOR_METHOD); 950 951 if (executableMember != null) { 953 exceptions = executableMember.getThrownExceptions(); 954 } 955 else { 956 exceptions = new ArrayList(); 957 } 958 } 959 960 String type = null; 961 962 for (Iterator i = exceptions.iterator(); i.hasNext(); ) { 963 type = ((XClass) i.next()).getQualifiedName(); 964 965 if (checkExceptions != null) { 966 if (checkExceptions.indexOf(type) != -1) { 967 return true; 968 } 969 } 970 } 971 return false; 972 } 973 974 private boolean returnsVoid(Properties attributes) throws XDocletException 975 { 976 String methodName = attributes.getProperty("method"); 977 978 if (methodName == null) { 979 return ("void".equals(getMethodTypeFor(getCurrentMethod()))); 980 } 981 else { 982 XMethod method = (XMethod) getXExecutableMemberForMemberName(methodName, true, FOR_METHOD); 983 984 if (method == null) { 986 throw new XDocletException(Translator.getString(XDocletMessages.class, XDocletMessages.METHOD_NOT_FOUND, new String []{methodName})); 987 } 988 989 return ("void".equals(getMethodTypeFor(method))); 990 } 991 } 992 993 private void ifMethodNameEquals_Impl(String template, Properties attributes, boolean condition) throws XDocletException 994 { 995 String method_name = attributes.getProperty("name"); 996 997 if (getCurrentMethod().getName().equals(method_name) == condition) { 998 generate(template); 999 } 1000 } 1001 1002 1014 private void ifHasMethod_Impl(String template, Properties attributes, boolean hasMethod) throws XDocletException 1015 { 1016 Log log = LogUtil.getLog(MethodTagsHandler.class, "ifHasMethod_Impl"); 1017 1018 String methodName = attributes.getProperty("name"); 1019 String parametersStr = attributes.getProperty("parameters"); 1020 String delimiter = attributes.getProperty("delimiter"); 1021 1022 String [] parameters = null; 1023 1024 if (log.isDebugEnabled()) { 1025 log.debug("methodName=" + methodName); 1026 log.debug("parametersStr=" + parametersStr); 1027 log.debug("delimiter=" + delimiter); 1028 log.debug("hasMethod=" + hasMethod); 1029 log.debug("getCurrentClass()=" + getCurrentClass()); 1030 } 1031 1032 if (parametersStr != null) { 1033 if (delimiter == null) { 1034 delimiter = PARAMETER_DELIMITER; 1035 } 1036 1037 parameters = DocletUtil.tokenizeDelimitedToArray(parametersStr, delimiter); 1038 1039 if (log.isDebugEnabled()) { 1040 log.debug("parameters.length=" + parameters.length); 1041 if (parameters.length > 0) 1042 log.debug("parameters[0]=" + parameters[0]); 1043 } 1044 } 1045 if (hasMethod(getCurrentClass(), methodName, parameters, false) == hasMethod) { 1046 log.debug("method found."); 1047 generate(template); 1048 } 1049 else { 1050 log.debug("method not found."); 1051 } 1052 } 1053} 1054 | Popular Tags |