KickJava   Java API By Example, From Geeks To Geeks.

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


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

14 public class FieldDocAnalyzer extends ItemDocAnalyzer
15 {
16     public final static String JavaDoc MSG_SERIALFIELD_WITHOUT_NAME_TYPE_AND_DESCRIPTION = "@serialField without field name, type, and description.";
17
18     public final static String JavaDoc MSG_SERIALFIELD_WITHOUT_TYPE_AND_DESCRIPTION = "@serialField without field type and description.";
19
20     public final static String JavaDoc MSG_SERIALFIELD_WITHOUT_DESCRIPTION = "@serialField without description.";
21     
22     private ASTFieldDeclaration _field;
23     
24     public FieldDocAnalyzer(Report r, ASTFieldDeclaration field)
25     {
26         super(r, field);
27         
28         _field = field;
29     }
30
31     public String JavaDoc getItemType()
32     {
33         return "field";
34     }
35     
36     protected void checkJavadoc(JavadocNode javadoc)
37     {
38         super.checkJavadoc(javadoc);
39
40         SimpleNode encNode = getEnclosingNode();
41
42         if (javadoc != null && isCheckable(encNode, CHKLVL_TAG_CONTENT)) {
43             JavadocTaggedNode[] taggedComments = javadoc.getTaggedComments();
44             for (int ti = 0; ti < taggedComments.length; ++ti) {
45                 JavadocTaggedNode jtn = taggedComments[ti];
46                 JavadocTag tag = jtn.getTag();
47
48                 tr.Ace.log("checking tag: " + tag);
49                 if (tag.text.equals(JavadocTags.SERIALFIELD)) {
50
51                     // expecting: field-name field-type field-description
52
JavadocElement desc = jtn.getDescription();
53                     if (desc == null) {
54                         addViolation(MSG_SERIALFIELD_WITHOUT_NAME_TYPE_AND_DESCRIPTION, tag.start, tag.end);
55                     }
56                     else {
57                         JavadocElement nontgt = jtn.getDescriptionNonTarget();
58
59                         if (nontgt == null) {
60                             addViolation(MSG_SERIALFIELD_WITHOUT_TYPE_AND_DESCRIPTION, desc.start, desc.end);
61                         }
62                         else {
63                             String JavaDoc text = nontgt.text;
64                             
65                             int pos = 0;
66                             int len = text.length();
67
68                             boolean gotAnotherWord = false;
69                             while (!gotAnotherWord && pos < len) {
70                                 if (Character.isWhitespace(text.charAt(pos))) {
71                                     gotAnotherWord = true;
72                                 }
73                                 ++pos;
74                             }
75
76                             if (!gotAnotherWord) {
77                                 addViolation(MSG_SERIALFIELD_WITHOUT_DESCRIPTION, desc.start, desc.end);
78                             }
79                         }
80                     }
81                 }
82             }
83         }
84     }
85
86     /**
87      * Returns the valid tags, as strings, for fields.
88      */

89     protected List JavaDoc getValidTags()
90     {
91         return JavadocTags.getValidFieldTags();
92     }
93
94     /**
95      * Adds a violation for a field, with the violation pointing to the field
96      * name.
97      */

98     protected void addUndocumentedViolation(String JavaDoc desc)
99     {
100         // reference the list of variables declared in this field.
101

102         ASTVariableDeclarator[] vds = FieldUtil.getVariableDeclarators(_field);
103
104         Token begin = vds[0].getFirstToken();
105         Token end = vds[vds.length - 1].getFirstToken();
106
107         addViolation(desc, begin, end);
108     }
109
110     protected SimpleNode getEnclosingNode()
111     {
112         return SimpleNodeUtil.getParent(_field);
113     }
114
115 }
116
Popular Tags