001///////////////////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code and other text files for adherence to a set of rules.
003// Copyright (C) 2001-2026 the original author or authors.
004//
005// This library is free software; you can redistribute it and/or
006// modify it under the terms of the GNU Lesser General Public
007// License as published by the Free Software Foundation; either
008// version 2.1 of the License, or (at your option) any later version.
009//
010// This library is distributed in the hope that it will be useful,
011// but WITHOUT ANY WARRANTY; without even the implied warranty of
012// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
013// Lesser General Public License for more details.
014//
015// You should have received a copy of the GNU Lesser General Public
016// License along with this library; if not, write to the Free Software
017// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
018///////////////////////////////////////////////////////////////////////////////////////////////
019
020package com.puppycrawl.tools.checkstyle.checks.regexp;
021
022import com.puppycrawl.tools.checkstyle.PropertyType;
023import com.puppycrawl.tools.checkstyle.StatelessCheck;
024import com.puppycrawl.tools.checkstyle.XdocsPropertyType;
025import com.puppycrawl.tools.checkstyle.api.AbstractCheck;
026import com.puppycrawl.tools.checkstyle.api.DetailAST;
027import com.puppycrawl.tools.checkstyle.utils.CommonUtil;
028
029/**
030 * <div>
031 * Checks that a specified pattern matches a single-line in Java files.
032 * </div>
033 *
034 * <p>
035 * This class is variation on
036 * <a href="https://checkstyle.org/checks/regexp/regexpsingleline.html">
037 * RegexpSingleline</a>
038 * for detecting single-lines that match a supplied regular expression in Java files.
039 * It supports suppressing matches in Java comments.
040 * </p>
041 *
042 * @since 5.0
043 */
044@StatelessCheck
045public class RegexpSinglelineJavaCheck extends AbstractCheck {
046
047    /** A key is pointing to the warning message text in "messages.properties" file. */
048    public static final String MSG_REGEXP_EXCEEDED = "regexp.exceeded";
049
050    /** A key is pointing to the warning message text in "messages.properties" file. */
051    public static final String MSG_REGEXP_MINIMUM = "regexp.minimum";
052
053    /** Specify the format of the regular expression to match. */
054    @XdocsPropertyType(PropertyType.PATTERN)
055    private String format = "$.";
056    /**
057     * Specify the message which is used to notify about violations,
058     * if empty then default (hard-coded) message is used.
059     */
060    private String message;
061    /** Specify the minimum number of matches required in each file. */
062    private int minimum;
063    /** Specify the maximum number of matches required in each file. */
064    private int maximum;
065    /** Control whether to ignore case when searching. */
066    private boolean ignoreCase;
067    /** Control whether to ignore text in comments when searching. */
068    private boolean ignoreComments;
069
070    /**
071     * Creates a new {@code RegexpSinglelineJavaCheck} instance.
072     */
073    public RegexpSinglelineJavaCheck() {
074        // no code by default
075    }
076
077    @Override
078    public int[] getDefaultTokens() {
079        return getRequiredTokens();
080    }
081
082    @Override
083    public int[] getAcceptableTokens() {
084        return getRequiredTokens();
085    }
086
087    @Override
088    public int[] getRequiredTokens() {
089        return CommonUtil.EMPTY_INT_ARRAY;
090    }
091
092    @Override
093    @SuppressWarnings("deprecation")
094    public void beginTree(DetailAST rootAST) {
095        MatchSuppressor suppressor = null;
096        if (ignoreComments) {
097            suppressor = new CommentSuppressor(getFileContents());
098        }
099
100        final DetectorOptions options = DetectorOptions.newBuilder()
101            .reporter(this)
102            .suppressor(suppressor)
103            .format(format)
104            .message(message)
105            .minimum(minimum)
106            .maximum(maximum)
107            .ignoreCase(ignoreCase)
108            .build();
109        final SinglelineDetector detector = new SinglelineDetector(options,
110                MSG_REGEXP_EXCEEDED, MSG_REGEXP_MINIMUM);
111        detector.processLines(getFileContents().getText());
112    }
113
114    /**
115     * Setter to specify the format of the regular expression to match.
116     *
117     * @param format the format of the regular expression to match.
118     * @since 5.0
119     */
120    public void setFormat(String format) {
121        this.format = format;
122    }
123
124    /**
125     * Setter to specify the message which is used to notify about violations,
126     * if empty then default (hard-coded) message is used.
127     *
128     * @param message the message to report for a match.
129     * @since 6.0
130     */
131    public void setMessage(String message) {
132        this.message = message;
133    }
134
135    /**
136     * Setter to specify the minimum number of matches required in each file.
137     *
138     * @param minimum the minimum number of matches required in each file.
139     * @since 5.0
140     */
141    public void setMinimum(int minimum) {
142        this.minimum = minimum;
143    }
144
145    /**
146     * Setter to specify the maximum number of matches required in each file.
147     *
148     * @param maximum the maximum number of matches required in each file.
149     * @since 5.0
150     */
151    public void setMaximum(int maximum) {
152        this.maximum = maximum;
153    }
154
155    /**
156     * Setter to control whether to ignore case when searching.
157     *
158     * @param ignoreCase whether to ignore case when searching.
159     * @since 5.0
160     */
161    public void setIgnoreCase(boolean ignoreCase) {
162        this.ignoreCase = ignoreCase;
163    }
164
165    /**
166     * Setter to control whether to ignore text in comments when searching.
167     *
168     * @param ignore whether to ignore text in comments when searching.
169     * @since 5.0
170     */
171    public void setIgnoreComments(boolean ignore) {
172        ignoreComments = ignore;
173    }
174
175}