Coverage Report - com.puppycrawl.tools.checkstyle.checks.regexp.RegexpSinglelineJavaCheck
 
Classes in this File Line Coverage Branch Coverage Complexity
RegexpSinglelineJavaCheck
100%
33/33
100%
2/2
1.1
 
 1  
 ////////////////////////////////////////////////////////////////////////////////
 2  
 // checkstyle: Checks Java source code for adherence to a set of rules.
 3  
 // Copyright (C) 2001-2017 the original author or authors.
 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  
 
 20  
 package com.puppycrawl.tools.checkstyle.checks.regexp;
 21  
 
 22  
 import com.puppycrawl.tools.checkstyle.StatelessCheck;
 23  
 import com.puppycrawl.tools.checkstyle.api.AbstractCheck;
 24  
 import com.puppycrawl.tools.checkstyle.api.DetailAST;
 25  
 import com.puppycrawl.tools.checkstyle.utils.CommonUtils;
 26  
 
 27  
 /**
 28  
  * Implementation of a check that looks for a single line in Java files.
 29  
  * Supports ignoring comments for matches.
 30  
  * @author Oliver Burn
 31  
  */
 32  
 @StatelessCheck
 33  34
 public class RegexpSinglelineJavaCheck extends AbstractCheck {
 34  
 
 35  
     /** The format of the regular expression to match. */
 36  34
     private String format = "$.";
 37  
     /** The message to report for a match. */
 38  
     private String message;
 39  
     /** The minimum number of matches required per file. */
 40  
     private int minimum;
 41  
     /** The maximum number of matches required per file. */
 42  
     private int maximum;
 43  
     /** Whether to ignore case when matching. */
 44  
     private boolean ignoreCase;
 45  
     /** Suppress comments. **/
 46  
     private boolean ignoreComments;
 47  
 
 48  
     @Override
 49  
     public int[] getDefaultTokens() {
 50  32
         return getAcceptableTokens();
 51  
     }
 52  
 
 53  
     @Override
 54  
     public int[] getAcceptableTokens() {
 55  68
         return CommonUtils.EMPTY_INT_ARRAY;
 56  
     }
 57  
 
 58  
     @Override
 59  
     public int[] getRequiredTokens() {
 60  32
         return getAcceptableTokens();
 61  
     }
 62  
 
 63  
     @Override
 64  
     public void beginTree(DetailAST rootAST) {
 65  19
         MatchSuppressor suppressor = null;
 66  19
         if (ignoreComments) {
 67  9
             suppressor = new CommentSuppressor(getFileContents());
 68  
         }
 69  
 
 70  19
         final DetectorOptions options = DetectorOptions.newBuilder()
 71  19
             .reporter(this)
 72  19
             .compileFlags(0)
 73  19
             .suppressor(suppressor)
 74  19
             .format(format)
 75  19
             .message(message)
 76  19
             .minimum(minimum)
 77  19
             .maximum(maximum)
 78  19
             .ignoreCase(ignoreCase)
 79  19
             .build();
 80  19
         final SinglelineDetector detector = new SinglelineDetector(options);
 81  19
         detector.processLines(getFileContents().getText());
 82  19
     }
 83  
 
 84  
     /**
 85  
      * Set the format of the regular expression to match.
 86  
      * @param format the format of the regular expression to match.
 87  
      */
 88  
     public void setFormat(String format) {
 89  26
         this.format = format;
 90  26
     }
 91  
 
 92  
     /**
 93  
      * Set the message to report for a match.
 94  
      * @param message the message to report for a match.
 95  
      */
 96  
     public void setMessage(String message) {
 97  8
         this.message = message;
 98  8
     }
 99  
 
 100  
     /**
 101  
      * Set the minimum number of matches required per file.
 102  
      * @param minimum the minimum number of matches required per file.
 103  
      */
 104  
     public void setMinimum(int minimum) {
 105  3
         this.minimum = minimum;
 106  3
     }
 107  
 
 108  
     /**
 109  
      * Set the maximum number of matches required per file.
 110  
      * @param maximum the maximum number of matches required per file.
 111  
      */
 112  
     public void setMaximum(int maximum) {
 113  3
         this.maximum = maximum;
 114  3
     }
 115  
 
 116  
     /**
 117  
      * Set whether to ignore case when matching.
 118  
      * @param ignoreCase whether to ignore case when matching.
 119  
      */
 120  
     public void setIgnoreCase(boolean ignoreCase) {
 121  3
         this.ignoreCase = ignoreCase;
 122  3
     }
 123  
 
 124  
     /**
 125  
      * Set whether to ignore comments when matching.
 126  
      * @param ignore whether to ignore comments when matching.
 127  
      */
 128  
     public void setIgnoreComments(boolean ignore) {
 129  11
         ignoreComments = ignore;
 130  11
     }
 131  
 }