KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > incava > doctorj > MethodDocAnalyzer


1 package org.incava.doctorj;
2
3 import java.util.Iterator JavaDoc;
4 import java.util.List JavaDoc;
5 import net.sourceforge.pmd.ast.*;
6 import org.incava.analysis.Report;
7 import org.incava.java.*;
8 import org.incava.javadoc.*;
9
10
11 /**
12  * Analyzes Javadoc and code for methods.
13  */

14 public class MethodDocAnalyzer extends FunctionDocAnalyzer
15 {
16     public final static String JavaDoc MSG_RETURN_WITHOUT_DESCRIPTION = "@return without description.";
17
18     public final static String JavaDoc MSG_RETURN_FOR_VOID_METHOD = "@return for method returning void";
19
20     public final static String JavaDoc MSG_RETURN_TYPE_USED = "@return refers to method return type";
21     
22     private ASTMethodDeclaration _method;
23     
24     public MethodDocAnalyzer(Report r, ASTMethodDeclaration method)
25     {
26         super(r, method);
27         
28         _method = method;
29     }
30
31     public String JavaDoc getItemType()
32     {
33         return "method";
34     }
35
36     /**
37      * Returns the parent node, which is the enclosing declaration.
38      */

39     protected SimpleNode getEnclosingNode()
40     {
41         return SimpleNodeUtil.getParent(_method);
42     }
43
44     protected void checkJavadoc(JavadocNode javadoc)
45     {
46         tr.Ace.log("javadoc: " + javadoc);
47
48         super.checkJavadoc(javadoc);
49
50         if (javadoc != null) {
51             JavadocTaggedNode[] taggedComments = javadoc.getTaggedComments();
52             for (int ti = 0; ti < taggedComments.length; ++ti) {
53                 JavadocTaggedNode jtn = taggedComments[ti];
54                 JavadocTag tag = jtn.getTag();
55                 tr.Ace.log("checking tag: " + tag);
56                 
57                 if (tag.text.equals(JavadocTags.RETURN)) {
58                     ASTResultType resType = (ASTResultType)SimpleNodeUtil.findChild(_method, ASTResultType.class);
59                     Token resTkn = resType.getFirstToken();
60                     
61                     if (resTkn.kind == JavaParserConstants.VOID) {
62                         addViolation(MSG_RETURN_FOR_VOID_METHOD, tag.start, tag.end);
63                     }
64                     else {
65                         JavadocElement tgt = jtn.getTarget();
66                         if (tgt == null) {
67                             if (isCheckable(getEnclosingNode(), CHKLVL_TAG_CONTENT)) {
68                                 addViolation(MSG_RETURN_WITHOUT_DESCRIPTION, tag.start, tag.end);
69                             }
70                         }
71                         else {
72                             String JavaDoc text = tgt.text;
73                             tr.Ace.log("text: '" + text + "'");
74                             if (text.equals(resTkn.image)) {
75                                 addViolation(MSG_RETURN_TYPE_USED, tgt.start, tgt.end);
76                             }
77                         }
78                     }
79                 }
80             }
81         }
82     }
83
84     /**
85      * Returns the parameter list for the method.
86      */

87     protected ASTFormalParameters getParameterList()
88     {
89         return MethodUtil.getParameters(_method);
90     }
91
92     /**
93      * Returns the valid tags, as strings, for methods.
94      */

95     protected List JavaDoc getValidTags()
96     {
97         return JavadocTags.getValidMethodTags();
98     }
99
100     /**
101      * Adds a violation for a method, with the violation pointing to the method
102      * name.
103      */

104     protected void addUndocumentedViolation(String JavaDoc desc)
105     {
106         Token nameTk = MethodUtil.getName(_method);
107         addViolation(desc, nameTk);
108     }
109
110 }
111
Popular Tags