KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > xdoclet > tagshandler > MethodTagsHandler


1 /*
2  * Copyright (c) 2001, 2002 The XDoclet team
3  * All rights reserved.
4  */

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 /**
21  * @author Ara Abrahamian (ara_e@email.com)
22  * @created Oct 15, 2001
23  * @xdoclet.taghandler namespace="Method"
24  * @version $Revision: 1.34 $
25  */

26 public class MethodTagsHandler extends AbstractProgramElementTagsHandler
27 {
28     public static String JavaDoc getMethodTypeFor(XMethod method)
29     {
30         return method.getReturnType().getType().getQualifiedName() + method.getReturnType().getDimensionAsString();
31     }
32
33     public static String JavaDoc getTransformedMethodTypeFor(XMethod method)
34     {
35         return method.getReturnType().getType().getTransformedQualifiedName() + method.getReturnType().getDimensionAsString();
36     }
37
38     /**
39      * Merge with modified SubTask.methodNameWithoutPrefix
40      *
41      * @param currentMethod Description of Parameter
42      * @return Description of the Returned Value
43      */

44     public static String JavaDoc getMethodNameWithoutPrefixFor(XMethod currentMethod)
45     {
46         String JavaDoc 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     /**
55      * Gets the PropertyNameFor attribute of the MethodTagsHandler class
56      *
57      * @param method Describe what the parameter does
58      * @return The PropertyNameFor value
59      */

60     public static String JavaDoc getPropertyNameFor(XMethod method)
61     {
62         return method.getPropertyName();
63     }
64
65     /**
66      * Returns true if the str string starts with a getter prefix ("get" or "is").
67      *
68      * @param str Description of Parameter
69      * @return The Getter value
70      */

71     public static boolean isGetter(String JavaDoc str)
72     {
73         return str.startsWith("get") || str.startsWith("is");
74     }
75
76     /**
77      * Returns true if the str string starts with "set" prefix.
78      *
79      * @param str
80      * @return
81      */

82     public static boolean isSetter(String JavaDoc 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     /**
98      * Returns true if a method with the specified methodName+parameters is found in the class clazz. The parameters
99      * array can be empty, if so any method with any set of parameters is considered equal to the method we're searching
100      * for. if not empty all parameters of the method must be equal to the ones specified in parameters array to have
101      * "method equality".
102      *
103      * @param clazz Description of Parameter
104      * @param methodName Description of Parameter
105      * @param parameters Description of Parameter
106      * @param setCurrentMethod
107      * @return Description of the Returned Value
108      * @exception XDocletException
109      */

110     public static boolean hasMethod(XClass clazz, String JavaDoc methodName, String JavaDoc[] parameters, boolean setCurrentMethod)
111          throws XDocletException
112     {
113         return hasExecutableMember(clazz, methodName, parameters, setCurrentMethod, FOR_METHOD);
114     }
115
116     /**
117      * Returns 'get' or 'is' getter prefix part of the current method. Returns empty string if the method doesn't start
118      * with either of the two getter prefixes.
119      *
120      * @return Description of the Returned Value
121      * @exception XDocletException Description of Exception
122      * @doc.tag type="content"
123      */

124     public String JavaDoc 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             // for boolean here we don't know if it is get or is, lets find it
134
String JavaDoc[] 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     /**
148      * Returns the getter method name for the current method by prefixing the method name with the proper getter prefix.
149      *
150      * @return Description of the Returned Value
151      * @exception XDocletException Description of Exception
152      * @see #methodNameWithoutPrefix()
153      * @see #setterMethod()
154      * @see #getterPrefix()
155      * @doc.tag type="content"
156      */

157     public String JavaDoc getterMethod() throws XDocletException
158     {
159         return getterPrefix() + methodNameWithoutPrefix();
160     }
161
162     /**
163      * Returns the setter method name for the current method by prefixing the method name with a 'set' and removing the
164      * getter method's 'get' or 'is' prefixes, if any.
165      *
166      * @return Description of the Returned Value
167      * @exception XDocletException Description of Exception
168      * @see #methodNameWithoutPrefix()
169      * @see #getterMethod()
170      * @doc.tag type="content"
171      */

172     public String JavaDoc setterMethod() throws XDocletException
173     {
174         return "set" + methodNameWithoutPrefix();
175     }
176
177     /**
178      * Evaluate the body if current class has a method with the specified name+parameters. If parameters not specified
179      * then any method with the given name and any set of parameters is considered equal to the given method name and so
180      * the test result is positive and the body is evaluated. This method change the current method to the one
181      * specified.
182      *
183      * @param template The body of the block tag
184      * @param attributes The attributes of the template tag
185      * @exception XDocletException Description of Exception
186      * @see #ifHasMethod(java.lang.String,java.util.Properties)
187      * @doc.tag type="block"
188      * @doc.param name="name" optional="false" description="The name of the method we're searching for
189      * its existence in current class."
190      * @doc.param name="parameters" optional="true" description="We're searching for a method that has
191      * the exact set of parameters specified in parameters param."
192      * @doc.param name="delimiter" optional="true" description="The parameters param is delimited by
193      * the string specified in delimiter parameter."
194      */

195     public void setCurrentMethod(String JavaDoc template, Properties attributes) throws XDocletException
196     {
197         String JavaDoc methodName = attributes.getProperty("name");
198         String JavaDoc parametersStr = attributes.getProperty("parameters");
199         String JavaDoc delimiter = attributes.getProperty("delimiter");
200
201         String JavaDoc[] 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     /**
221      * Any modifiers (static, volatile, etc.) for the current method.
222      *
223      * @return modifiers
224      * @exception XDocletException Describe the exception
225      * @doc.tag type="content"
226      */

227     public String JavaDoc modifiers() throws XDocletException
228     {
229         return modifiers(FOR_METHOD);
230     }
231
232     /**
233      * The comment for the current method.
234      *
235      * @param attributes The attributes of the template tag
236      * @return javadoc comment
237      * @exception XDocletException Description of Exception
238      * @see ClassTagsHandler#classComment(java.util.Properties)
239      * @doc.tag type="content"
240      * @doc.param name="no-comment-signs" optional="true" values="true,false" description="If true
241      * then don't decorate the comment with comment signs."
242      * @doc.param name="indent" optional="true" description="Number of spaces to indent the comment.
243      * Default is 0."
244      */

245     public String JavaDoc methodComment(Properties attributes) throws XDocletException
246     {
247         return memberComment(attributes, FOR_METHOD);
248     }
249
250     /**
251      * Evaluates the body block if current method has a javadoc comment.
252      *
253      * @param template The body of the block tag
254      * @exception XDocletException Description of Exception
255      * @doc.tag type="block"
256      */

257     public void ifHasMethodComment(String JavaDoc template) throws XDocletException
258     {
259         Properties attributes = new Properties();
260
261         attributes.setProperty("no-comment-signs", "true");
262
263         String JavaDoc comment = methodComment(attributes);
264
265         if (!comment.trim().equals("")) {
266             generate(template);
267         }
268     }
269
270     /**
271      * Iterates over all exceptions thrown by the current or specified method and returns a string containing definition
272      * of all those exceptions.
273      *
274      * @param attributes The attributes of the template tag
275      * @return throws clause for the method
276      * @exception XDocletException Description of Exception
277      * @doc.tag type="block"
278      * @doc.param name="method" optional="true" description="The method name of which exceptions list
279      * is extracted. If not specified then current method is used."
280      * @doc.param name="skip" optional="true" description="A comma-separated list of exceptions that
281      * should be skipped and not put into the list."
282      * @doc.param name="append" optional="true" description="A comma-separated list of exceptions that
283      * should be always appended regardless if current method has that exception defined or not."
284      */

285     public String JavaDoc exceptionList(Properties attributes) throws XDocletException
286     {
287         return exceptionList(attributes, FOR_METHOD);
288     }
289
290     /**
291      * Evaluate the body block if current method is abstract.
292      *
293      * @param template The body of the block tag
294      * @param attributes The attributes of the template tag
295      * @exception XDocletException Description of Exception
296      * @see #ifIsNotAbstract(java.lang.String,java.util.Properties)
297      * @doc.tag type="block"
298      * @doc.param name="method" optional="true" description="The method name of which abstractness is
299      * evaluated. If not specified then current method is used."
300      */

301     public void ifIsAbstract(String JavaDoc template, Properties attributes) throws XDocletException
302     {
303         if (isAbstract(attributes)) {
304             generate(template);
305         }
306     }
307
308     /**
309      * Evaluates the body block if current method is not abstract.
310      *
311      * @param template The body of the block tag
312      * @param attributes The attributes of the template tag
313      * @exception XDocletException Description of Exception
314      * @see #ifIsAbstract(java.lang.String,java.util.Properties)
315      * @doc.tag type="block"
316      * @doc.param name="method" optional="true" description="The method name of which exceptions list
317      * is extracted. If not specified then current method is used."
318      */

319     public void ifIsNotAbstract(String JavaDoc template, Properties attributes) throws XDocletException
320     {
321         if (!isAbstract(attributes)) {
322             generate(template);
323         }
324     }
325
326     /**
327      * Evaluate the body block if current method returns void.
328      *
329      * @param template The body of the block tag
330      * @param attributes The attributes of the template tag
331      * @exception XDocletException Description of Exception
332      * @see #ifDoesntReturnVoid(java.lang.String,java.util.Properties)
333      * @doc.tag type="block"
334      * @doc.param name="method" optional="true" description="The method name whose return type is
335      * checked. If not specified then current method is used."
336      */

337     public void ifReturnsVoid(String JavaDoc template, Properties attributes) throws XDocletException
338     {
339         if (returnsVoid(attributes)) {
340             generate(template);
341         }
342     }
343
344     /**
345      * Evaluates the body block if current method doesn't return void.
346      *
347      * @param template The body of the block tag
348      * @param attributes The attributes of the template tag
349      * @exception XDocletException Description of Exception
350      * @see #ifReturnsVoid(java.lang.String,java.util.Properties)
351      * @doc.tag type="block"
352      * @doc.param name="method" optional="true" description="The method name whose return type is
353      * checked. If not specified then current method is used."
354      */

355     public void ifDoesntReturnVoid(String JavaDoc template, Properties attributes) throws XDocletException
356     {
357         if (!returnsVoid(attributes)) {
358             generate(template);
359         }
360     }
361
362     /**
363      * Loops through all methods for all classes after first sorting all the methods.
364      *
365      * @param template The body of the block tag
366      * @param attributes The attributes of the template tag
367      * @exception XDocletException Description of Exception
368      * @doc.tag type="block"
369      * @doc.param name="type" optional="true" description="For all classes by the type."
370      * @doc.param name="extent" optional="true" values="concrete-type,superclass,hierarchy"
371      * description="Specifies the extent of the type search. If concrete-type then only check the concrete type, if
372      * superclass then check also superclass, if hierarchy then search the whole hierarchy and find if the class is
373      * of the specified type. Default is hierarchy."
374      */

375     public void forAllClassMethods(String JavaDoc template, Properties attributes) throws XDocletException
376     {
377         String JavaDoc 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     /**
407      * Iterates over all methods of current class and evaluates the body of the tag for each method.
408      *
409      * @param template The body of the block tag
410      * @param attributes The attributes of the template tag
411      * @exception XDocletException Description of Exception
412      * @doc.tag type="block"
413      * @doc.param name="superclasses" optional="true" values="true,false" description="If true then
414      * traverse superclasses also, otherwise look up the tag in current concrete class only."
415      * @doc.param name="sort" optional="true" values="true,false" description="If true then sort the
416      * methods list."
417      */

418     public void forAllMethods(String JavaDoc template, Properties attributes) throws XDocletException
419     {
420         forAllMembers(template, attributes, FOR_METHOD);
421     }
422
423     /**
424      * Evaluates the body if current method doesn't have at least one tag with the specified name.
425      *
426      * @param template The body of the block tag
427      * @param attributes The attributes of the template tag
428      * @exception XDocletException Description of Exception
429      * @doc.tag type="block"
430      * @doc.param name="tagName" optional="false" description="The tag name."
431      * @doc.param name="paramName" description="The parameter name. If not specified, then the raw
432      * content of the tag is returned."
433      * @doc.param name="paramNum" description="The zero-based parameter number. It's used if the user
434      * used the space-separated format for specifying parameters."
435      * @doc.param name="error" description="Show this error message if no tag found."
436      */

437     public void ifDoesntHaveMethodTag(String JavaDoc template, Properties attributes) throws XDocletException
438     {
439         if (!hasTag(attributes, FOR_METHOD)) {
440             generate(template);
441         }
442         else {
443             String JavaDoc error = attributes.getProperty("error");
444
445             if (error != null) {
446                 getEngine().print(error);
447             }
448         }
449     }
450
451     /**
452      * Evaluates the body if current method has at least one tag with the specified name.
453      *
454      * @param template The body of the block tag
455      * @param attributes The attributes of the template tag
456      * @exception XDocletException Description of Exception
457      * @doc.tag type="block"
458      * @doc.param name="tagName" optional="false" description="The tag name."
459      * @doc.param name="paramName" description="The parameter name. If not specified, then the raw
460      * content of the tag is returned."
461      * @doc.param name="paramNum" description="The zero-based parameter number. It's used if the user
462      * used the space-separated format for specifying parameters."
463      * @doc.param name="error" description="Show this error message if no tag found."
464      */

465     public void ifHasMethodTag(String JavaDoc template, Properties attributes) throws XDocletException
466     {
467         if (hasTag(attributes, FOR_METHOD)) {
468             generate(template);
469         }
470         else {
471             String JavaDoc error = attributes.getProperty("error");
472
473             if (error != null) {
474                 getEngine().print(error);
475             }
476         }
477     }
478
479     /**
480      * Evaluate the current block, and then restore the current method before continuing.
481      *
482      * @param template The body of the block tag
483      * @param attributes The attributes of the template tag
484      * @exception XDocletException Description of Exception
485      * @doc.tag type="block"
486      */

487     public void executeAndRestoreMethod(String JavaDoc template, Properties attributes) throws XDocletException
488     {
489         XMethod method = getCurrentMethod();
490
491         generate(template);
492         setCurrentMethod(method);
493     }
494
495     /**
496      * Evaluates the body if value for the method tag equals the specified value.
497      *
498      * @param template The body of the block tag
499      * @param attributes The attributes of the template tag
500      * @exception XDocletException Description of Exception
501      * @doc.tag type="block"
502      * @doc.param name="tagName" optional="false" description="The tag name."
503      * @doc.param name="paramName" description="The parameter name. If not specified, then the raw
504      * content of the tag is returned."
505      * @doc.param name="paramNum" description="The zero-based parameter number. It's used if the user
506      * used the space-separated format for specifying parameters."
507      */

508     public void ifMethodTagValueEquals(String JavaDoc template, Properties attributes) throws XDocletException
509     {
510         if (isTagValueEqual(attributes, FOR_METHOD)) {
511             generate(template);
512         }
513     }
514
515     /**
516      * Evaluates the body if method name equals to the specified value.
517      *
518      * @param template The body of the block tag
519      * @param attributes The attributes of the template tag
520      * @exception XDocletException Description of Exception
521      * @doc.tag type="block"
522      * @doc.param name="name" optional="false" description="The method name."
523      */

524     public void ifMethodNameEquals(String JavaDoc template, Properties attributes) throws XDocletException
525     {
526         ifMethodNameEquals_Impl(template, attributes, true);
527     }
528
529     /**
530      * Evaluates the body if method name equals to the specified value.
531      *
532      * @param template The body of the block tag
533      * @param attributes The attributes of the template tag
534      * @exception XDocletException Description of Exception
535      * @doc.tag type="block"
536      * @doc.param name="name" optional="false" description="The method name."
537      */

538     public void ifMethodNameNotEquals(String JavaDoc template, Properties attributes) throws XDocletException
539     {
540         ifMethodNameEquals_Impl(template, attributes, false);
541     }
542
543     /**
544      * Evaluates the body if value for the method tag not equals the specified value.
545      *
546      * @param template The body of the block tag
547      * @param attributes The attributes of the template tag
548      * @exception XDocletException Description of Exception
549      * @doc.tag type="block"
550      * @doc.param name="tagName" optional="false" description="The tag name."
551      * @doc.param name="paramName" description="The parameter name. If not specified, then the raw
552      * content of the tag is returned."
553      * @doc.param name="paramNum" description="The zero-based parameter number. It's used if the user
554      * used the space-separated format for specifying parameters."
555      */

556     public void ifMethodTagValueNotEquals(String JavaDoc template, Properties attributes) throws XDocletException
557     {
558         if (!isTagValueEqual(attributes, FOR_METHOD)) {
559             generate(template);
560         }
561     }
562
563     /**
564      * Iterates over all method tags with the specified tagName for the current method probably inside of a
565      * forAllMethodTags body.
566      *
567      * @param attributes The attributes of the template tag
568      * @return Description of the Returned Value
569      * @exception XDocletException Description of Exception
570      * @doc.tag type="content"
571      * @doc.param name="tagName" optional="false" description="The tag name."
572      * @doc.param name="paramName" description="The parameter name. If not specified, then the raw
573      * content of the tag is returned."
574      * @doc.param name="paramNum" description="The zero-based parameter number. It's used if the user
575      * used the space-separated format for specifying parameters."
576      * @doc.param name="values" description="The valid values for the parameter, comma separated. An
577      * error message is printed if the parameter value is not one of the values."
578      * @doc.param name="default" description="The default value is returned if parameter not specified
579      * by user for the tag."
580      */

581     public String JavaDoc methodTagValue(Properties attributes) throws XDocletException
582     {
583         return getExpandedDelimitedTagValue(attributes, FOR_METHOD);
584     }
585
586     /**
587      * Iterates over all tags of current method and evaluates the body of the tag for each method.
588      *
589      * @param template The body of the block tag
590      * @param attributes The attributes of the template tag
591      * @exception XDocletException Description of Exception
592      * @doc.tag type="block"
593      * @doc.param name="tagName" optional="false" description="The tag name."
594      */

595     public void forAllMethodTags(String JavaDoc template, Properties attributes) throws XDocletException
596     {
597         forAllMemberTags(template, attributes, FOR_METHOD, XDocletTagshandlerMessages.ONLY_CALL_METHOD_NOT_NULL, new String JavaDoc[]{"forAllMethodTags"});
598     }
599
600     /**
601      * Iterates over all tokens in current method tag with the name tagName and evaluates the body for every token.
602      *
603      * @param template The body of the block tag
604      * @param attributes The attributes of the template tag
605      * @exception XDocletException Description of Exception
606      * @doc.tag type="block"
607      * @doc.param name="tagName" optional="false" description="The tag name."
608      * @doc.param name="delimiter" description="delimiter for the StringTokenizer. consult javadoc for
609      * java.util.StringTokenizer default is ','"
610      * @doc.param name="skip" description="how many tokens to skip on start"
611      */

612     public void forAllMethodTagTokens(String JavaDoc template, Properties attributes) throws XDocletException
613     {
614         forAllMemberTagTokens(template, attributes, FOR_METHOD);
615     }
616
617     /**
618      * Return standard javadoc of current method.
619      *
620      * @return Description of the Returned Value
621      * @exception XDocletException Description of Exception
622      * @doc.tag type="content"
623      */

624     public String JavaDoc firstSentenceDescriptionOfCurrentMethod() throws XDocletException
625     {
626         return firstSentenceDescriptionOfCurrentMember(getCurrentMethod());
627     }
628
629     /**
630      * Returns the return type of the current method.
631      *
632      * @param attributes The attributes of the template tag
633      * @return return type
634      * @exception XDocletException Description of Exception
635      * @doc.tag type="content"
636      */

637     public String JavaDoc methodType(Properties attributes) throws XDocletException
638     {
639         return getMethodTypeFor(getCurrentMethod());
640     }
641
642     /**
643      * Returns the transformed return type of the current method.
644      *
645      * @param attributes
646      * @return Description of the Returned Value
647      * @exception XDocletException Description of Exception
648      * @doc.tag type="content"
649      */

650     public String JavaDoc transformedMethodType(Properties attributes) throws XDocletException
651     {
652         return getTransformedMethodTypeFor(getCurrentMethod());
653     }
654
655     /**
656      * Evaluates the body if the return type of the current method equals the specified value.
657      *
658      * @param template The body of the block tag
659      * @param attributes The attributes of the template tag
660      * @exception XDocletException Description of Exception
661      * @doc.tag type="block"
662      * @doc.param name="type" optional="false" description="The type to compare."
663      */

664     public void ifIsOfType(String JavaDoc template, Properties attributes) throws XDocletException
665     {
666         if (ifIsOfTypeImpl(template, attributes))
667             generate(template);
668     }
669
670     /**
671      * Evaluates the body if the return type of the current method doesn't equal the specified value.
672      *
673      * @param template The body of the block tag
674      * @param attributes The attributes of the template tag
675      * @exception XDocletException Description of Exception
676      * @doc.tag type="block"
677      * @doc.param name="type" optional="false" description="The type to compare."
678      */

679     public void ifIsNotOfType(String JavaDoc template, Properties attributes) throws XDocletException
680     {
681         if (!ifIsOfTypeImpl(template, attributes))
682             generate(template);
683     }
684
685     public boolean ifIsOfTypeImpl(String JavaDoc template, Properties attributes) throws XDocletException
686     {
687         return methodType(attributes).equals(attributes.getProperty("type"));
688     }
689
690     /**
691      * Returns the name of the current method.
692      *
693      * @param attributes The attributes of the template tag
694      * @return Description of the Returned Value
695      * @exception XDocletException Description of Exception
696      * @doc.tag type="content"
697      */

698     public String JavaDoc methodName(Properties attributes) throws XDocletException
699     {
700         if (attributes != null) {
701             String JavaDoc value = (String JavaDoc) attributes.get("value");
702
703             if (value != null) {
704                 String JavaDoc m = getCurrentMethod().getName().substring(Integer.parseInt(value));
705                 // replace first character to lowercase
706
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     /**
717      * Returns the name of the current method without the first three characters. Used for cases where the method name
718      * without the get/set prefix is needed.
719      *
720      * @return Description of the Returned Value
721      * @exception XDocletException Description of Exception
722      * @doc.tag type="content"
723      */

724     public String JavaDoc methodNameWithoutPrefix() throws XDocletException
725     {
726         return getMethodNameWithoutPrefixFor(getCurrentMethod());
727     }
728
729     /**
730      * Returns the current method name. Used inside block elements.
731      *
732      * @return method name
733      * @exception XDocletException Description of Exception
734      */

735     public String JavaDoc currentMethodName() throws XDocletException
736     {
737         return getCurrentMethod().getName();
738     }
739
740     /**
741      * Returns the property name extracted from the current method name. Remove any getter/setter prefix from method
742      * name and decapitalize it.
743      *
744      * @return property name
745      * @exception XDocletException Description of Exception
746      * @doc.tag type="content"
747      */

748     public String JavaDoc propertyName() throws XDocletException
749     {
750         return getPropertyNameFor(getCurrentMethod());
751     }
752
753     /**
754      * Evaluate the body if current class has a method with the specified name+parameters. If parameters not specified
755      * then any method with the given name and any set of parameters is considered equal to the given method name and so
756      * the test result is positive and the body is evaluated. This method does not change the current method to the one
757      * specified.
758      *
759      * @param template The body of the block tag
760      * @param attributes The attributes of the template tag
761      * @exception XDocletException Description of Exception
762      * @see #ifDoesntHaveMethod(java.lang.String,java.util.Properties)
763      * @doc.tag type="block"
764      * @doc.param name="name" optional="false" description="The name of the method we're searching for
765      * its existence in current class."
766      * @doc.param name="parameters" optional="true" description="We're searching for a method that has
767      * the exact set of parameters specified in parameters param."
768      * @doc.param name="delimiter" optional="true" description="The parameters param is delimited by
769      * the string specified in delimiter parameter."
770      */

771     public void ifHasMethod(String JavaDoc template, Properties attributes) throws XDocletException
772     {
773         ifHasMethod_Impl(template, attributes, true);
774     }
775
776     /**
777      * Evaluate the body if current class doesn't have a method with the specified name+parameters. If parameters not
778      * specified then any method with the given name and any set of parameters is considered equal to the given method
779      * name and so the test result is positive and the body is evaluated.
780      *
781      * @param template The body of the block tag
782      * @param attributes The attributes of the template tag
783      * @exception XDocletException Description of Exception
784      * @see #ifHasMethod(java.lang.String,java.util.Properties)
785      * @doc.tag type="block"
786      * @doc.param name="name" optional="false" description="The name of the method we're searching for
787      * its existence in current class."
788      * @doc.param name="parameters" optional="true" description="We're searching for a method that has
789      * the exact set of parameters specified in parameters param."
790      * @doc.param name="delimiter" optional="true" description="The parameters param is delimited by
791      * the string specified in delimiter parameter."
792      */

793     public void ifDoesntHaveMethod(String JavaDoc template, Properties attributes) throws XDocletException
794     {
795         ifHasMethod_Impl(template, attributes, false);
796     }
797
798     /**
799      * Evaluates the body if the specified method (or current method if none is supplied) is a getter method.
800      *
801      * @param template The body of the block tag
802      * @param attributes The attributes of the template tag
803      * @exception XDocletException Description of Exception
804      * @doc.tag type="block"
805      * @doc.param name="method" optional="true" description="The method name."
806      */

807     public void ifIsGetter(String JavaDoc template, Properties attributes) throws XDocletException
808     {
809         String JavaDoc 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     /**
824      * Evaluates the body if the specified method (or current method if none is supplied) is a setter method.
825      *
826      * @param template The body of the block tag
827      * @param attributes The attributes of the template tag
828      * @exception XDocletException Description of Exception
829      * @doc.tag type="block"
830      * @doc.param name="method" optional="true" description="The method name."
831      */

832     public void ifIsSetter(String JavaDoc template, Properties attributes) throws XDocletException
833     {
834         String JavaDoc 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     /**
849      * Evaluates the body if the current method has public visibility.
850      *
851      * @param template The body of the block tag
852      * @exception XDocletException Description of Exception
853      * @doc.tag type="block"
854      */

855     public void ifIsPublic(String JavaDoc template) throws XDocletException
856     {
857         if (getCurrentMethod().isPublic()) {
858             generate(template);
859         }
860     }
861
862     /**
863      * Evaluate the body block if current method throws the exceptions specified in the <code>exceptions</code>
864      * attribute.
865      *
866      * @param template The body of the block tag
867      * @param attributes The attributes of the template tag
868      * @exception XDocletException Description of Exception
869      * @see #ifDoesntThrowException(java.lang.String,java.util.Properties)
870      * @doc.tag type="block"
871      * @doc.param name="method" optional="true" description="The method name whose return type is
872      * checked. If not specified then current method is used."
873      * @doc.param name="exceptions" optional="false" description="The exception name which is checked
874      * for"
875      */

876     public void ifThrowsException(String JavaDoc template, Properties attributes) throws XDocletException
877     {
878         if (throwsException(attributes)) {
879             generate(template);
880         }
881     }
882
883     /**
884      * Evaluate the body block if current method doesn't throw the exceptions specified in the <code>exceptions</code>
885      * attribute.
886      *
887      * @param template The body of the block tag
888      * @param attributes The attributes of the template tag
889      * @exception XDocletException Description of Exception
890      * @see #ifThrowsException(java.lang.String,java.util.Properties)
891      * @doc.tag type="block"
892      * @doc.param name="method" optional="true" description="The method name whose return type is
893      * checked. If not specified then current method is used."
894      * @doc.param name="exceptions" optional="false" description="The exception name which is checked
895      * for"
896      */

897     public void ifDoesntThrowException(String JavaDoc 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 JavaDoc 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             // no method with the specified name found in class
915
if (method == null) {
916                 throw new XDocletException(Translator.getString(XDocletMessages.class, XDocletMessages.METHOD_NOT_FOUND, new String JavaDoc[]{methodName}));
917             }
918
919             return method.isAbstract();
920         }
921     }
922
923     /**
924      * Returns wheather the current method throws the exceptions specified in the <code>exceptions</code> attribute.
925      *
926      * @param attributes the attributes
927      * @return <code>true</code> if the current method throws the listed exceptions
928      * @exception XDocletException if a error occures
929      */

930     private boolean throwsException(Properties attributes) throws XDocletException
931     {
932         String JavaDoc checkExceptions = attributes.getProperty("exceptions");
933         String JavaDoc 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             // no member with the specified name found in class
952
if (executableMember != null) {
953                 exceptions = executableMember.getThrownExceptions();
954             }
955             else {
956                 exceptions = new ArrayList();
957             }
958         }
959
960         String JavaDoc 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 JavaDoc 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             // no method with the specified name found in class
985
if (method == null) {
986                 throw new XDocletException(Translator.getString(XDocletMessages.class, XDocletMessages.METHOD_NOT_FOUND, new String JavaDoc[]{methodName}));
987             }
988
989             return ("void".equals(getMethodTypeFor(method)));
990         }
991     }
992
993     private void ifMethodNameEquals_Impl(String JavaDoc template, Properties attributes, boolean condition) throws XDocletException
994     {
995         String JavaDoc method_name = attributes.getProperty("name");
996
997         if (getCurrentMethod().getName().equals(method_name) == condition) {
998             generate(template);
999         }
1000    }
1001
1002    /**
1003     * The implementation of ifHasMethod and ifDoesntHaveMethod tags.
1004     *
1005     * @param template The body of the block tag
1006     * @param attributes The attributes of the template tag
1007     * @param hasMethod Flag that determines whether we're testing that the method does (true) or does not
1008     * (false) exist
1009     * @exception XDocletException Description of Exception
1010     * @see #ifHasMethod(java.lang.String,java.util.Properties)
1011     * @see #ifDoesntHaveMethod(java.lang.String,java.util.Properties)
1012     * @see #hasMethod(xjavadoc.XClass,java.lang.String,java.lang.String[],boolean)
1013     */

1014    private void ifHasMethod_Impl(String JavaDoc template, Properties attributes, boolean hasMethod) throws XDocletException
1015    {
1016        Log log = LogUtil.getLog(MethodTagsHandler.class, "ifHasMethod_Impl");
1017
1018        String JavaDoc methodName = attributes.getProperty("name");
1019        String JavaDoc parametersStr = attributes.getProperty("parameters");
1020        String JavaDoc delimiter = attributes.getProperty("delimiter");
1021
1022        String JavaDoc[] 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