001//////////////////////////////////////////////////////////////////////////////// 002// checkstyle: Checks Java source code for adherence to a set of rules. 003// Copyright (C) 2001-2022 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.coding; 021 022import java.util.regex.Pattern; 023 024import com.puppycrawl.tools.checkstyle.StatelessCheck; 025import com.puppycrawl.tools.checkstyle.api.AbstractCheck; 026import com.puppycrawl.tools.checkstyle.api.DetailAST; 027import com.puppycrawl.tools.checkstyle.api.TokenTypes; 028import com.puppycrawl.tools.checkstyle.utils.CommonUtil; 029 030/** 031 * <p> 032 * Checks specified tokens text for matching an illegal pattern. 033 * By default no tokens are specified. 034 * </p> 035 * <ul> 036 * <li> 037 * Property {@code format} - Define the RegExp for illegal pattern. 038 * Type is {@code java.util.regex.Pattern}. 039 * Default value is {@code "^$"}. 040 * </li> 041 * <li> 042 * Property {@code ignoreCase} - Control whether to ignore case when matching. 043 * Type is {@code boolean}. 044 * Default value is {@code false}. 045 * </li> 046 * <li> 047 * Property {@code message} - Define the message which is used to notify about violations; 048 * if empty then the default message is used. 049 * Type is {@code java.lang.String}. 050 * Default value is {@code ""}. 051 * </li> 052 * <li> 053 * Property {@code tokens} - tokens to check 054 * Type is {@code java.lang.String[]}. 055 * Validation type is {@code tokenSet}. 056 * Default value is: {@code ""}. 057 * </li> 058 * </ul> 059 * <p> 060 * To configure the check to forbid String literals containing {@code "a href"}: 061 * </p> 062 * <pre> 063 * <module name="IllegalTokenText"> 064 * <property name="tokens" value="STRING_LITERAL"/> 065 * <property name="format" value="a href"/> 066 * </module> 067 * </pre> 068 * <p>Example:</p> 069 * <pre> 070 * public void myTest() { 071 * String test = "a href"; // violation 072 * String test2 = "A href"; // OK, case is sensitive 073 * } 074 * </pre> 075 * <p> 076 * To configure the check to forbid String literals containing {@code "a href"} 077 * for the ignoreCase mode: 078 * </p> 079 * <pre> 080 * <module name="IllegalTokenText"> 081 * <property name="tokens" value="STRING_LITERAL"/> 082 * <property name="format" value="a href"/> 083 * <property name="ignoreCase" value="true"/> 084 * </module> 085 * </pre> 086 * <p>Example:</p> 087 * <pre> 088 * public void myTest() { 089 * String test = "a href"; // violation 090 * String test2 = "A href"; // violation, case is ignored 091 * } 092 * </pre> 093 * <p> 094 * To configure the check to forbid string literal text blocks containing {@code """}: 095 * </p> 096 * <pre> 097 * <module name="IllegalTokenText"> 098 * <property name="tokens" value="TEXT_BLOCK_CONTENT"/> 099 * <property name="format" value='"'/> 100 * </module> 101 * </pre> 102 * <p>Example:</p> 103 * <pre> 104 * public void myTest() { 105 * final String quote = """ 106 * \""""; // violation 107 * } 108 * </pre> 109 * <p> 110 * To configure the check to forbid leading zeros in an integer literal, 111 * other than zero and a hex literal: 112 * </p> 113 * <pre> 114 * <module name="IllegalTokenText"> 115 * <property name="tokens" value="NUM_INT,NUM_LONG"/> 116 * <property name="format" value="^0[^lx]"/> 117 * <property name="ignoreCase" value="true"/> 118 * </module> 119 * </pre> 120 * <p>Example:</p> 121 * <pre> 122 * public void myTest() { 123 * int test1 = 0; // OK 124 * int test2 = 0x111; // OK 125 * int test3 = 0X111; // OK, case is ignored 126 * int test4 = 010; // violation 127 * long test5 = 0L; // OK 128 * long test6 = 010L; // violation 129 * } 130 * </pre> 131 * <p> 132 * Parent is {@code com.puppycrawl.tools.checkstyle.TreeWalker} 133 * </p> 134 * <p> 135 * Violation Message Keys: 136 * </p> 137 * <ul> 138 * <li> 139 * {@code illegal.token.text} 140 * </li> 141 * </ul> 142 * 143 * @since 3.2 144 */ 145@StatelessCheck 146public class IllegalTokenTextCheck 147 extends AbstractCheck { 148 149 /** 150 * A key is pointing to the warning message text in "messages.properties" 151 * file. 152 */ 153 public static final String MSG_KEY = "illegal.token.text"; 154 155 /** 156 * Define the message which is used to notify about violations; 157 * if empty then the default message is used. 158 */ 159 private String message = ""; 160 161 /** The format string of the regexp. */ 162 private String formatString = "^$"; 163 164 /** Define the RegExp for illegal pattern. */ 165 private Pattern format = Pattern.compile(formatString); 166 167 /** Control whether to ignore case when matching. */ 168 private boolean ignoreCase; 169 170 @Override 171 public int[] getDefaultTokens() { 172 return CommonUtil.EMPTY_INT_ARRAY; 173 } 174 175 @Override 176 public int[] getAcceptableTokens() { 177 return new int[] { 178 TokenTypes.NUM_DOUBLE, 179 TokenTypes.NUM_FLOAT, 180 TokenTypes.NUM_INT, 181 TokenTypes.NUM_LONG, 182 TokenTypes.IDENT, 183 TokenTypes.COMMENT_CONTENT, 184 TokenTypes.STRING_LITERAL, 185 TokenTypes.CHAR_LITERAL, 186 TokenTypes.TEXT_BLOCK_CONTENT, 187 }; 188 } 189 190 @Override 191 public int[] getRequiredTokens() { 192 return CommonUtil.EMPTY_INT_ARRAY; 193 } 194 195 @Override 196 public boolean isCommentNodesRequired() { 197 return true; 198 } 199 200 @Override 201 public void visitToken(DetailAST ast) { 202 final String text = ast.getText(); 203 if (format.matcher(text).find()) { 204 String customMessage = message; 205 if (customMessage.isEmpty()) { 206 customMessage = MSG_KEY; 207 } 208 log( 209 ast, 210 customMessage, 211 formatString); 212 } 213 } 214 215 /** 216 * Setter to define the message which is used to notify about violations; 217 * if empty then the default message is used. 218 * 219 * @param message custom message which should be used 220 * to report about violations. 221 */ 222 public void setMessage(String message) { 223 if (message == null) { 224 this.message = ""; 225 } 226 else { 227 this.message = message; 228 } 229 } 230 231 /** 232 * Setter to define the RegExp for illegal pattern. 233 * 234 * @param format a {@code String} value 235 */ 236 public void setFormat(String format) { 237 formatString = format; 238 updateRegexp(); 239 } 240 241 /** 242 * Setter to control whether to ignore case when matching. 243 * 244 * @param caseInsensitive true if the match is case insensitive. 245 */ 246 public void setIgnoreCase(boolean caseInsensitive) { 247 ignoreCase = caseInsensitive; 248 updateRegexp(); 249 } 250 251 /** 252 * Updates the {@link #format} based on the values from {@link #formatString} and 253 * {@link #ignoreCase}. 254 */ 255 private void updateRegexp() { 256 final int compileFlags; 257 if (ignoreCase) { 258 compileFlags = Pattern.CASE_INSENSITIVE; 259 } 260 else { 261 compileFlags = 0; 262 } 263 format = CommonUtil.createPattern(formatString, compileFlags); 264 } 265 266}