001/////////////////////////////////////////////////////////////////////////////////////////////// 002// checkstyle: Checks Java source code and other text files for adherence to a set of rules. 003// Copyright (C) 2001-2025 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 /** Specify the format of the regular expression to match. */ 048 @XdocsPropertyType(PropertyType.PATTERN) 049 private String format = "$."; 050 /** 051 * Specify the message which is used to notify about violations, 052 * if empty then default (hard-coded) message is used. 053 */ 054 private String message; 055 /** Specify the minimum number of matches required in each file. */ 056 private int minimum; 057 /** Specify the maximum number of matches required in each file. */ 058 private int maximum; 059 /** Control whether to ignore case when searching. */ 060 private boolean ignoreCase; 061 /** Control whether to ignore text in comments when searching. */ 062 private boolean ignoreComments; 063 064 @Override 065 public int[] getDefaultTokens() { 066 return getRequiredTokens(); 067 } 068 069 @Override 070 public int[] getAcceptableTokens() { 071 return getRequiredTokens(); 072 } 073 074 @Override 075 public int[] getRequiredTokens() { 076 return CommonUtil.EMPTY_INT_ARRAY; 077 } 078 079 // suppress deprecation until https://github.com/checkstyle/checkstyle/issues/11166 080 @SuppressWarnings("deprecation") 081 @Override 082 public void beginTree(DetailAST rootAST) { 083 MatchSuppressor suppressor = null; 084 if (ignoreComments) { 085 suppressor = new CommentSuppressor(getFileContents()); 086 } 087 088 final DetectorOptions options = DetectorOptions.newBuilder() 089 .reporter(this) 090 .suppressor(suppressor) 091 .format(format) 092 .message(message) 093 .minimum(minimum) 094 .maximum(maximum) 095 .ignoreCase(ignoreCase) 096 .build(); 097 final SinglelineDetector detector = new SinglelineDetector(options); 098 detector.processLines(getFileContents().getText()); 099 } 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}