KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > puppycrawl > tools > checkstyle > checks > TodoCommentCheck


1 ////////////////////////////////////////////////////////////////////////////////
2
// checkstyle: Checks Java source code for adherence to a set of rules.
3
// Copyright (C) 2001-2005 Oliver Burn
4
//
5
// This library is free software; you can redistribute it and/or
6
// modify it under the terms of the GNU Lesser General Public
7
// License as published by the Free Software Foundation; either
8
// version 2.1 of the License, or (at your option) any later version.
9
//
10
// This library is distributed in the hope that it will be useful,
11
// but WITHOUT ANY WARRANTY; without even the implied warranty of
12
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
// Lesser General Public License for more details.
14
//
15
// You should have received a copy of the GNU Lesser General Public
16
// License along with this library; if not, write to the Free Software
17
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18
////////////////////////////////////////////////////////////////////////////////
19
package com.puppycrawl.tools.checkstyle.checks;
20
21 import java.util.Iterator JavaDoc;
22 import java.util.List JavaDoc;
23 import java.util.Map JavaDoc;
24
25 import com.puppycrawl.tools.checkstyle.api.TextBlock;
26 import com.puppycrawl.tools.checkstyle.api.DetailAST;
27 import com.puppycrawl.tools.checkstyle.api.FileContents;
28
29 /**
30  * <p>
31  * A check for TODO comments.
32  * Actually it is a generic {@link java.util.regex.Pattern regular expression}
33  * matcher on Java comments.
34  * To check for other patterns in Java comments, set property format.
35  * </p>
36  * <p>
37  * An example of how to configure the check is:
38  * </p>
39  * <pre>
40  * &lt;module name="TodoComment"/&gt;
41  * </pre>
42  * <p>
43  * An example of how to configure the check for comments that contain
44  * <code>WARNING</code> is:
45  * </p>
46  * <pre>
47  * &lt;module name="TodoComment"&gt;
48  * &lt;property name="format" value="WARNING"/&gt;
49  * &lt;/module&gt;
50  * </pre>
51  * @author Oliver Burn
52  * @version 1.0
53  */

54 public class TodoCommentCheck
55     extends AbstractFormatCheck
56 {
57     /**
58      * Creates a new <code>TodoCommentCheck</code> instance.
59      */

60     public TodoCommentCheck()
61     {
62         super("TODO:"); // the empty language
63
}
64
65     /** {@inheritDoc} */
66     public int[] getDefaultTokens()
67     {
68         return new int[0];
69     }
70
71     /** {@inheritDoc} */
72     public void beginTree(DetailAST aRootAST)
73     {
74         final FileContents contents = getFileContents();
75         checkCppComments(contents);
76         checkBadComments(contents);
77     }
78
79     /**
80      * Checks the C++ comments for todo expressions.
81      * @param aContents the <code>FileContents</code>
82      */

83     private void checkCppComments(FileContents aContents)
84     {
85         final Map JavaDoc comments = aContents.getCppComments();
86         for (final Iterator JavaDoc it = comments.keySet().iterator(); it.hasNext();) {
87             final Integer JavaDoc key = (Integer JavaDoc) it.next();
88             final String JavaDoc cmt = ((TextBlock) comments.get(key)).getText()[0];
89             if (getRegexp().matcher(cmt).find()) {
90                 log(key.intValue(), "todo.match", getFormat());
91             }
92         }
93     }
94
95     /**
96      * Checks the C-style comments for todo expressions.
97      * @param aContents the <code>FileContents</code>
98      */

99     private void checkBadComments(FileContents aContents)
100     {
101         final Map JavaDoc allComments = aContents.getCComments();
102         final Iterator JavaDoc allIter = allComments.keySet().iterator();
103         while (allIter.hasNext()) {
104             final Integer JavaDoc key = (Integer JavaDoc) allIter.next();
105             final List JavaDoc lineComments = (List JavaDoc) allComments.get(key);
106             final Iterator JavaDoc lineIter = lineComments.iterator();
107             while (lineIter.hasNext()) {
108                 final String JavaDoc[] cmt = ((TextBlock) lineIter.next()).getText();
109                 for (int i = 0; i < cmt.length; i++) {
110                     if (getRegexp().matcher(cmt[i]).find()) {
111                         log(key.intValue() + i, "todo.match", getFormat());
112                     }
113                 }
114             }
115         }
116     }
117 }
118
Popular Tags