View Javadoc
1   ///////////////////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3   // Copyright (C) 2001-2025 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 java.io.File;
23  
24  import com.puppycrawl.tools.checkstyle.PropertyType;
25  import com.puppycrawl.tools.checkstyle.StatelessCheck;
26  import com.puppycrawl.tools.checkstyle.XdocsPropertyType;
27  import com.puppycrawl.tools.checkstyle.api.AbstractFileSetCheck;
28  import com.puppycrawl.tools.checkstyle.api.FileText;
29  
30  /**
31   * <div>
32   * Checks that a specified pattern matches a single-line in any file type.
33   * </div>
34   *
35   * <p>
36   * Rationale: This check can be used to prototype checks and to find common bad
37   * practice such as calling {@code ex.printStacktrace()},
38   * {@code System.out.println()}, {@code System.exit()}, etc.
39   * </p>
40   *
41   * @since 5.0
42   */
43  @StatelessCheck
44  public class RegexpSinglelineCheck extends AbstractFileSetCheck {
45  
46      /** Specify the format of the regular expression to match. */
47      @XdocsPropertyType(PropertyType.PATTERN)
48      private String format = "$.";
49      /**
50       * Specify the message which is used to notify about violations,
51       * if empty then default (hard-coded) message is used.
52       */
53      private String message;
54      /** Specify the minimum number of matches required in each file. */
55      private int minimum;
56      /** Specify the maximum number of matches required in each file. */
57      private int maximum;
58      /** Control whether to ignore case when searching. */
59      private boolean ignoreCase;
60  
61      /** The detector to use. */
62      private SinglelineDetector detector;
63  
64      @Override
65      public void beginProcessing(String charset) {
66          final DetectorOptions options = DetectorOptions.newBuilder()
67              .reporter(this)
68              .format(format)
69              .message(message)
70              .minimum(minimum)
71              .maximum(maximum)
72              .ignoreCase(ignoreCase)
73              .build();
74          detector = new SinglelineDetector(options);
75      }
76  
77      @Override
78      protected void processFiltered(File file, FileText fileText) {
79          detector.processLines(fileText);
80      }
81  
82      /**
83       * Setter to specify the format of the regular expression to match.
84       *
85       * @param format the format of the regular expression to match.
86       * @since 5.0
87       */
88      public void setFormat(String format) {
89          this.format = format;
90      }
91  
92      /**
93       * Setter to specify the message which is used to notify about violations,
94       * if empty then default (hard-coded) message is used.
95       *
96       * @param message the message to report for a match.
97       * @since 5.0
98       */
99      public void setMessage(String message) {
100         this.message = message;
101     }
102 
103     /**
104      * Setter to specify the minimum number of matches required in each file.
105      *
106      * @param minimum the minimum number of matches required in each file.
107      * @since 5.0
108      */
109     public void setMinimum(int minimum) {
110         this.minimum = minimum;
111     }
112 
113     /**
114      * Setter to specify the maximum number of matches required in each file.
115      *
116      * @param maximum the maximum number of matches required in each file.
117      * @since 5.0
118      */
119     public void setMaximum(int maximum) {
120         this.maximum = maximum;
121     }
122 
123     /**
124      * Setter to control whether to ignore case when searching.
125      *
126      * @param ignoreCase whether to ignore case when searching.
127      * @since 5.0
128      */
129     public void setIgnoreCase(boolean ignoreCase) {
130         this.ignoreCase = ignoreCase;
131     }
132 
133 }