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 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 // suppress deprecation until https://github.com/checkstyle/checkstyle/issues/11166
80 @Override
81 @SuppressWarnings("deprecation")
82 public void beginTree(DetailAST rootAST) {
83 MatchSuppressor suppressor = null;
84 if (ignoreComments) {
85 suppressor = new CommentSuppressor(getFileContents());
86 }
87
88 final DetectorOptions options = DetectorOptions.newBuilder()
89 .reporter(this)
90 .suppressor(suppressor)
91 .format(format)
92 .message(message)
93 .minimum(minimum)
94 .maximum(maximum)
95 .ignoreCase(ignoreCase)
96 .build();
97 final SinglelineDetector detector = new SinglelineDetector(options);
98 detector.processLines(getFileContents().getText());
99 }
100
101 /**
102 * Setter to specify the format of the regular expression to match.
103 *
104 * @param format the format of the regular expression to match.
105 * @since 5.0
106 */
107 public void setFormat(String format) {
108 this.format = format;
109 }
110
111 /**
112 * Setter to specify the message which is used to notify about violations,
113 * if empty then default (hard-coded) message is used.
114 *
115 * @param message the message to report for a match.
116 * @since 6.0
117 */
118 public void setMessage(String message) {
119 this.message = message;
120 }
121
122 /**
123 * Setter to specify the minimum number of matches required in each file.
124 *
125 * @param minimum the minimum number of matches required in each file.
126 * @since 5.0
127 */
128 public void setMinimum(int minimum) {
129 this.minimum = minimum;
130 }
131
132 /**
133 * Setter to specify the maximum number of matches required in each file.
134 *
135 * @param maximum the maximum number of matches required in each file.
136 * @since 5.0
137 */
138 public void setMaximum(int maximum) {
139 this.maximum = maximum;
140 }
141
142 /**
143 * Setter to control whether to ignore case when searching.
144 *
145 * @param ignoreCase whether to ignore case when searching.
146 * @since 5.0
147 */
148 public void setIgnoreCase(boolean ignoreCase) {
149 this.ignoreCase = ignoreCase;
150 }
151
152 /**
153 * Setter to control whether to ignore text in comments when searching.
154 *
155 * @param ignore whether to ignore text in comments when searching.
156 * @since 5.0
157 */
158 public void setIgnoreComments(boolean ignore) {
159 ignoreComments = ignore;
160 }
161
162 }