001/////////////////////////////////////////////////////////////////////////////////////////////// 002// checkstyle: Checks Java source code and other text files for adherence to a set of rules. 003// Copyright (C) 2001-2023 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.naming; 021 022import com.puppycrawl.tools.checkstyle.StatelessCheck; 023import com.puppycrawl.tools.checkstyle.api.DetailAST; 024import com.puppycrawl.tools.checkstyle.api.TokenTypes; 025import com.puppycrawl.tools.checkstyle.utils.CommonUtil; 026 027/** 028 * <p> 029 * Checks identifiers with a pattern for a set of illegal names, such as those 030 * that are restricted or contextual keywords. Examples include "yield", "record", "_", and 031 * "var". Please read more at 032 * <a href="https://docs.oracle.com/javase/specs/jls/se14/html/jls-3.html#jls-3.9"> 033 * Java Language Specification</a> to get to know more about restricted keywords. Since this 034 * check uses a pattern to specify valid identifiers, users can also prohibit the usage 035 * of certain symbols, such as "$", or any non-ascii character. 036 * </p> 037 * <ul> 038 * <li> 039 * Property {@code format} - Specifies valid identifiers. 040 * Type is {@code java.util.regex.Pattern}. 041 * Default value is {@code "(?i)^(?!(record|yield|var|permits|sealed|_)$).+$"}. 042 * </li> 043 * <li> 044 * Property {@code tokens} - tokens to check 045 * Type is {@code java.lang.String[]}. 046 * Validation type is {@code tokenSet}. 047 * Default value is: 048 * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#CLASS_DEF"> 049 * CLASS_DEF</a>, 050 * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#INTERFACE_DEF"> 051 * INTERFACE_DEF</a>, 052 * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#ENUM_DEF"> 053 * ENUM_DEF</a>, 054 * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#ANNOTATION_DEF"> 055 * ANNOTATION_DEF</a>, 056 * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#ANNOTATION_FIELD_DEF"> 057 * ANNOTATION_FIELD_DEF</a>, 058 * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#PARAMETER_DEF"> 059 * PARAMETER_DEF</a>, 060 * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#VARIABLE_DEF"> 061 * VARIABLE_DEF</a>, 062 * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#METHOD_DEF"> 063 * METHOD_DEF</a>, 064 * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#ENUM_CONSTANT_DEF"> 065 * ENUM_CONSTANT_DEF</a>, 066 * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#PATTERN_VARIABLE_DEF"> 067 * PATTERN_VARIABLE_DEF</a>, 068 * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#RECORD_DEF"> 069 * RECORD_DEF</a>, 070 * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#RECORD_COMPONENT_DEF"> 071 * RECORD_COMPONENT_DEF</a>, 072 * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#LAMBDA"> 073 * LAMBDA</a>. 074 * </li> 075 * </ul> 076 * <p> 077 * To configure the check: 078 * </p> 079 * <p>Configuration:</p> 080 * <pre> 081 * <module name="IllegalIdentifierName"/> 082 * </pre> 083 * <p> 084 * Example: 085 * </p> 086 * <pre> 087 * public class TestClass { 088 * public static void main(String... args) { 089 * var var = 4; // violation, "var" should not be used as an identifier. 090 * int record = 15; // violation, "record" should not be used as an identifier. 091 * String yield = "yield"; // violation, "yield" should not be used as an identifier. 092 * 093 * record Record // violation, "Record" should not be used as an identifier. 094 * (Record record) { // violation, "record" should not be used as an identifier. 095 * } 096 * 097 * String yieldString = "yieldString"; // ok, part of another word 098 * record MyRecord(){} // ok, part of another word 099 * var variable = 2; // ok, part of another word 100 * String _; // violation, underscore should not be used as an identifier. 101 * } 102 * } 103 * </pre> 104 * <p> 105 * To configure the check to include "open" and "transitive" in the set of illegal identifiers: 106 * </p> 107 * <p>Configuration:</p> 108 * <pre> 109 * <module name="IllegalIdentifierName"> 110 * <property name="format" value="(?i)^(?!(record|yield|var 111 * |permits|sealed|open|transitive|_)$).+$"/> 112 * </module> 113 * </pre> 114 * <p>Example:</p> 115 * <pre> 116 * public class TestClass { 117 * public static void main(String... args) { 118 * 119 * int open = 4; // violation, "open" should not be used as an identifier 120 * Object transitive = "transitive"; // violation, "transitive" should not 121 * // be used as an identifier 122 * 123 * int openInt = 4; // ok, "open" is part of another word 124 * Object transitiveObject = "transitiveObject"; // ok, "transitive" is part of another word 125 * } 126 * } 127 * </pre> 128 * <p> 129 * Parent is {@code com.puppycrawl.tools.checkstyle.TreeWalker} 130 * </p> 131 * <p> 132 * Violation Message Keys: 133 * </p> 134 * <ul> 135 * <li> 136 * {@code name.invalidPattern} 137 * </li> 138 * </ul> 139 * 140 * @since 8.36 141 */ 142@StatelessCheck 143public class IllegalIdentifierNameCheck extends AbstractNameCheck { 144 145 /** 146 * Creates a new {@code IllegalIdentifierNameCheck} instance. 147 */ 148 public IllegalIdentifierNameCheck() { 149 super("(?i)^(?!(record|yield|var|permits|sealed|_)$).+$"); 150 } 151 152 @Override 153 public int[] getDefaultTokens() { 154 return getAcceptableTokens(); 155 } 156 157 @Override 158 public int[] getAcceptableTokens() { 159 return new int[] { 160 TokenTypes.CLASS_DEF, 161 TokenTypes.INTERFACE_DEF, 162 TokenTypes.ENUM_DEF, 163 TokenTypes.ANNOTATION_DEF, 164 TokenTypes.ANNOTATION_FIELD_DEF, 165 TokenTypes.PARAMETER_DEF, 166 TokenTypes.VARIABLE_DEF, 167 TokenTypes.METHOD_DEF, 168 TokenTypes.ENUM_CONSTANT_DEF, 169 TokenTypes.PATTERN_VARIABLE_DEF, 170 TokenTypes.RECORD_DEF, 171 TokenTypes.RECORD_COMPONENT_DEF, 172 TokenTypes.LAMBDA, 173 }; 174 } 175 176 @Override 177 public int[] getRequiredTokens() { 178 return CommonUtil.EMPTY_INT_ARRAY; 179 } 180 181 @Override 182 protected boolean mustCheckName(DetailAST ast) { 183 return ast.findFirstToken(TokenTypes.IDENT) != null; 184 } 185 186}