View Javadoc
1   ///////////////////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3   // Copyright (C) 2001-2024 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.PropertyType;
23  import com.puppycrawl.tools.checkstyle.StatelessCheck;
24  import com.puppycrawl.tools.checkstyle.XdocsPropertyType;
25  import com.puppycrawl.tools.checkstyle.api.AbstractCheck;
26  import com.puppycrawl.tools.checkstyle.api.DetailAST;
27  import com.puppycrawl.tools.checkstyle.utils.CommonUtil;
28  
29  /**
30   * <div>
31   * Checks that a specified pattern matches a single-line in Java files.
32   * </div>
33   *
34   * <p>
35   * This class is variation on
36   * <a href="https://checkstyle.org/checks/regexp/regexpsingleline.html#RegexpSingleline">
37   * RegexpSingleline</a>
38   * for detecting single-lines that match a supplied regular expression in Java files.
39   * It supports suppressing matches in Java comments.
40   * </p>
41   * <ul>
42   * <li>
43   * Property {@code format} - Specify the format of the regular expression to match.
44   * Type is {@code java.util.regex.Pattern}.
45   * Default value is {@code "$."}.
46   * </li>
47   * <li>
48   * Property {@code ignoreCase} - Control whether to ignore case when searching.
49   * Type is {@code boolean}.
50   * Default value is {@code false}.
51   * </li>
52   * <li>
53   * Property {@code ignoreComments} - Control whether to ignore text in comments when searching.
54   * Type is {@code boolean}.
55   * Default value is {@code false}.
56   * </li>
57   * <li>
58   * Property {@code maximum} - Specify the maximum number of matches required in each file.
59   * Type is {@code int}.
60   * Default value is {@code 0}.
61   * </li>
62   * <li>
63   * Property {@code message} - Specify the message which is used to notify about
64   * violations, if empty then default (hard-coded) message is used.
65   * Type is {@code java.lang.String}.
66   * Default value is {@code null}.
67   * </li>
68   * <li>
69   * Property {@code minimum} - Specify the minimum number of matches required in each file.
70   * Type is {@code int}.
71   * Default value is {@code 0}.
72   * </li>
73   * </ul>
74   *
75   * <p>
76   * Parent is {@code com.puppycrawl.tools.checkstyle.TreeWalker}
77   * </p>
78   *
79   * <p>
80   * Violation Message Keys:
81   * </p>
82   * <ul>
83   * <li>
84   * {@code regexp.exceeded}
85   * </li>
86   * <li>
87   * {@code regexp.minimum}
88   * </li>
89   * </ul>
90   *
91   * @since 6.0
92   */
93  @StatelessCheck
94  public class RegexpSinglelineJavaCheck extends AbstractCheck {
95  
96      /** Specify the format of the regular expression to match. */
97      @XdocsPropertyType(PropertyType.PATTERN)
98      private String format = "$.";
99      /**
100      * Specify the message which is used to notify about violations,
101      * if empty then default (hard-coded) message is used.
102      */
103     private String message;
104     /** Specify the minimum number of matches required in each file. */
105     private int minimum;
106     /** Specify the maximum number of matches required in each file. */
107     private int maximum;
108     /** Control whether to ignore case when searching. */
109     private boolean ignoreCase;
110     /** Control whether to ignore text in comments when searching. */
111     private boolean ignoreComments;
112 
113     @Override
114     public int[] getDefaultTokens() {
115         return getRequiredTokens();
116     }
117 
118     @Override
119     public int[] getAcceptableTokens() {
120         return getRequiredTokens();
121     }
122 
123     @Override
124     public int[] getRequiredTokens() {
125         return CommonUtil.EMPTY_INT_ARRAY;
126     }
127 
128     // suppress deprecation until https://github.com/checkstyle/checkstyle/issues/11166
129     @SuppressWarnings("deprecation")
130     @Override
131     public void beginTree(DetailAST rootAST) {
132         MatchSuppressor suppressor = null;
133         if (ignoreComments) {
134             suppressor = new CommentSuppressor(getFileContents());
135         }
136 
137         final DetectorOptions options = DetectorOptions.newBuilder()
138             .reporter(this)
139             .suppressor(suppressor)
140             .format(format)
141             .message(message)
142             .minimum(minimum)
143             .maximum(maximum)
144             .ignoreCase(ignoreCase)
145             .build();
146         final SinglelineDetector detector = new SinglelineDetector(options);
147         detector.processLines(getFileContents().getText());
148     }
149 
150     /**
151      * Setter to specify the format of the regular expression to match.
152      *
153      * @param format the format of the regular expression to match.
154      * @since 5.0
155      */
156     public void setFormat(String format) {
157         this.format = format;
158     }
159 
160     /**
161      * Setter to specify the message which is used to notify about violations,
162      * if empty then default (hard-coded) message is used.
163      *
164      * @param message the message to report for a match.
165      * @since 6.0
166      */
167     public void setMessage(String message) {
168         this.message = message;
169     }
170 
171     /**
172      * Setter to specify the minimum number of matches required in each file.
173      *
174      * @param minimum the minimum number of matches required in each file.
175      * @since 5.0
176      */
177     public void setMinimum(int minimum) {
178         this.minimum = minimum;
179     }
180 
181     /**
182      * Setter to specify the maximum number of matches required in each file.
183      *
184      * @param maximum the maximum number of matches required in each file.
185      * @since 5.0
186      */
187     public void setMaximum(int maximum) {
188         this.maximum = maximum;
189     }
190 
191     /**
192      * Setter to control whether to ignore case when searching.
193      *
194      * @param ignoreCase whether to ignore case when searching.
195      * @since 5.0
196      */
197     public void setIgnoreCase(boolean ignoreCase) {
198         this.ignoreCase = ignoreCase;
199     }
200 
201     /**
202      * Setter to control whether to ignore text in comments when searching.
203      *
204      * @param ignore whether to ignore text in comments when searching.
205      * @since 5.0
206      */
207     public void setIgnoreComments(boolean ignore) {
208         ignoreComments = ignore;
209     }
210 
211 }