1 ///////////////////////////////////////////////////////////////////////////////////////////////
2 // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3 // Copyright (C) 2001-2026 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">
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 *
42 * @since 5.0
43 */
44 @StatelessCheck
45 public class RegexpSinglelineJavaCheck extends AbstractCheck {
46
47 /** Specify the format of the regular expression to match. */
48 @XdocsPropertyType(PropertyType.PATTERN)
49 private String format = "$.";
50 /**
51 * Specify the message which is used to notify about violations,
52 * if empty then default (hard-coded) message is used.
53 */
54 private String message;
55 /** Specify the minimum number of matches required in each file. */
56 private int minimum;
57 /** Specify the maximum number of matches required in each file. */
58 private int maximum;
59 /** Control whether to ignore case when searching. */
60 private boolean ignoreCase;
61 /** Control whether to ignore text in comments when searching. */
62 private boolean ignoreComments;
63
64 @Override
65 public int[] getDefaultTokens() {
66 return getRequiredTokens();
67 }
68
69 @Override
70 public int[] getAcceptableTokens() {
71 return getRequiredTokens();
72 }
73
74 @Override
75 public int[] getRequiredTokens() {
76 return CommonUtil.EMPTY_INT_ARRAY;
77 }
78
79 @Override
80 @SuppressWarnings("deprecation")
81 public void beginTree(DetailAST rootAST) {
82 MatchSuppressor suppressor = null;
83 if (ignoreComments) {
84 suppressor = new CommentSuppressor(getFileContents());
85 }
86
87 final DetectorOptions options = DetectorOptions.newBuilder()
88 .reporter(this)
89 .suppressor(suppressor)
90 .format(format)
91 .message(message)
92 .minimum(minimum)
93 .maximum(maximum)
94 .ignoreCase(ignoreCase)
95 .build();
96 final SinglelineDetector detector = new SinglelineDetector(options);
97 detector.processLines(getFileContents().getText());
98 }
99
100 /**
101 * Setter to specify the format of the regular expression to match.
102 *
103 * @param format the format of the regular expression to match.
104 * @since 5.0
105 */
106 public void setFormat(String format) {
107 this.format = format;
108 }
109
110 /**
111 * Setter to specify the message which is used to notify about violations,
112 * if empty then default (hard-coded) message is used.
113 *
114 * @param message the message to report for a match.
115 * @since 6.0
116 */
117 public void setMessage(String message) {
118 this.message = message;
119 }
120
121 /**
122 * Setter to specify the minimum number of matches required in each file.
123 *
124 * @param minimum the minimum number of matches required in each file.
125 * @since 5.0
126 */
127 public void setMinimum(int minimum) {
128 this.minimum = minimum;
129 }
130
131 /**
132 * Setter to specify the maximum number of matches required in each file.
133 *
134 * @param maximum the maximum number of matches required in each file.
135 * @since 5.0
136 */
137 public void setMaximum(int maximum) {
138 this.maximum = maximum;
139 }
140
141 /**
142 * Setter to control whether to ignore case when searching.
143 *
144 * @param ignoreCase whether to ignore case when searching.
145 * @since 5.0
146 */
147 public void setIgnoreCase(boolean ignoreCase) {
148 this.ignoreCase = ignoreCase;
149 }
150
151 /**
152 * Setter to control whether to ignore text in comments when searching.
153 *
154 * @param ignore whether to ignore text in comments when searching.
155 * @since 5.0
156 */
157 public void setIgnoreComments(boolean ignore) {
158 ignoreComments = ignore;
159 }
160
161 }