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 /** 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 @Override 080 @SuppressWarnings("deprecation") 081 public void beginTree(DetailAST rootAST) { 082 MatchSuppressor suppressor = null; 083 if (ignoreComments) { 084 suppressor = new CommentSuppressor(getFileContents()); 085 } 086 087 final DetectorOptions options = DetectorOptions.newBuilder() 088 .reporter(this) 089 .suppressor(suppressor) 090 .format(format) 091 .message(message) 092 .minimum(minimum) 093 .maximum(maximum) 094 .ignoreCase(ignoreCase) 095 .build(); 096 final SinglelineDetector detector = new SinglelineDetector(options); 097 detector.processLines(getFileContents().getText()); 098 } 099 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}