Coverage Report - com.puppycrawl.tools.checkstyle.checks.TodoCommentCheck
 
Classes in this File Line Coverage Branch Coverage Complexity
TodoCommentCheck
100%
23/23
100%
12/12
2.2
 
 1  
 ////////////////////////////////////////////////////////////////////////////////
 2  
 // checkstyle: Checks Java source code for adherence to a set of rules.
 3  
 // Copyright (C) 2001-2014  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 com.puppycrawl.tools.checkstyle.api.DetailAST;
 22  
 import com.puppycrawl.tools.checkstyle.api.FileContents;
 23  
 import com.puppycrawl.tools.checkstyle.api.TextBlock;
 24  
 import java.util.List;
 25  
 import java.util.Map;
 26  
 
 27  
 /**
 28  
  * <p>
 29  
  * A check for TODO comments.
 30  
  * Actually it is a generic {@link java.util.regex.Pattern regular expression}
 31  
  * matcher on Java comments.
 32  
  * To check for other patterns in Java comments, set property format.
 33  
  * </p>
 34  
  * <p>
 35  
  * An example of how to configure the check is:
 36  
  * </p>
 37  
  * <pre>
 38  
  * &lt;module name="TodoComment"/&gt;
 39  
  * </pre>
 40  
  * <p>
 41  
  * An example of how to configure the check for comments that contain
 42  
  * <code>WARNING</code> is:
 43  
  * </p>
 44  
  * <pre>
 45  
  * &lt;module name="TodoComment"&gt;
 46  
  *    &lt;property name="format" value="WARNING"/&gt;
 47  
  * &lt;/module&gt;
 48  
  * </pre>
 49  
  * @author Oliver Burn
 50  
  * @version 1.0
 51  
  */
 52  
 public class TodoCommentCheck
 53  
     extends AbstractFormatCheck
 54  
 {
 55  
     /**
 56  
      * Creates a new <code>TodoCommentCheck</code> instance.
 57  
      */
 58  
     public TodoCommentCheck()
 59  
     {
 60  1
         super("TODO:"); // the empty language
 61  1
     }
 62  
 
 63  
     @Override
 64  
     public int[] getDefaultTokens()
 65  
     {
 66  1
         return new int[0];
 67  
     }
 68  
 
 69  
     @Override
 70  
     public void beginTree(DetailAST aRootAST)
 71  
     {
 72  1
         final FileContents contents = getFileContents();
 73  1
         checkCppComments(contents);
 74  1
         checkBadComments(contents);
 75  1
     }
 76  
 
 77  
     /**
 78  
      * Checks the C++ comments for todo expressions.
 79  
      * @param aContents the <code>FileContents</code>
 80  
      */
 81  
     private void checkCppComments(FileContents aContents)
 82  
     {
 83  1
         final Map<Integer, TextBlock> comments = aContents.getCppComments();
 84  1
         for (Map.Entry<Integer, TextBlock> entry : comments.entrySet()) {
 85  52
             final String cmt = entry.getValue().getText()[0];
 86  52
             if (getRegexp().matcher(cmt).find()) {
 87  1
                 log(entry.getKey().intValue(), "todo.match", getFormat());
 88  
             }
 89  52
         }
 90  1
     }
 91  
 
 92  
     /**
 93  
      * Checks the C-style comments for todo expressions.
 94  
      * @param aContents the <code>FileContents</code>
 95  
      */
 96  
     private void checkBadComments(FileContents aContents)
 97  
     {
 98  1
         final Map<Integer, List<TextBlock>> allComments = aContents
 99  
                 .getCComments();
 100  1
         for (Map.Entry<Integer, List<TextBlock>> entry : allComments.entrySet())
 101  
         {
 102  33
             for (TextBlock line : entry.getValue()) {
 103  35
                 final String[] cmt = line.getText();
 104  104
                 for (int i = 0; i < cmt.length; i++) {
 105  69
                     if (getRegexp().matcher(cmt[i]).find()) {
 106  3
                         log(entry.getKey().intValue() + i, "todo.match",
 107  
                                 getFormat());
 108  
                     }
 109  
                 }
 110  35
             }
 111  
         }
 112  1
     }
 113  
 }