001/////////////////////////////////////////////////////////////////////////////////////////////// 002// checkstyle: Checks Java source code and other text files for adherence to a set of rules. 003// Copyright (C) 2001-2024 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 * <div> 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/se22/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 * </div> 037 * 038 * <ul> 039 * <li> 040 * Property {@code format} - Sets the pattern to match valid identifiers. 041 * Type is {@code java.util.regex.Pattern}. 042 * Default value is {@code "(?i)^(?!(record|yield|var|permits|sealed)$).+$"}. 043 * </li> 044 * <li> 045 * Property {@code tokens} - tokens to check 046 * Type is {@code java.lang.String[]}. 047 * Validation type is {@code tokenSet}. 048 * Default value is: 049 * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#CLASS_DEF"> 050 * CLASS_DEF</a>, 051 * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#INTERFACE_DEF"> 052 * INTERFACE_DEF</a>, 053 * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#ENUM_DEF"> 054 * ENUM_DEF</a>, 055 * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#ANNOTATION_DEF"> 056 * ANNOTATION_DEF</a>, 057 * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#ANNOTATION_FIELD_DEF"> 058 * ANNOTATION_FIELD_DEF</a>, 059 * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#PARAMETER_DEF"> 060 * PARAMETER_DEF</a>, 061 * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#VARIABLE_DEF"> 062 * VARIABLE_DEF</a>, 063 * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#METHOD_DEF"> 064 * METHOD_DEF</a>, 065 * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#ENUM_CONSTANT_DEF"> 066 * ENUM_CONSTANT_DEF</a>, 067 * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#PATTERN_VARIABLE_DEF"> 068 * PATTERN_VARIABLE_DEF</a>, 069 * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#RECORD_DEF"> 070 * RECORD_DEF</a>, 071 * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#RECORD_COMPONENT_DEF"> 072 * RECORD_COMPONENT_DEF</a>, 073 * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#LAMBDA"> 074 * LAMBDA</a>. 075 * </li> 076 * </ul> 077 * 078 * <p> 079 * Parent is {@code com.puppycrawl.tools.checkstyle.TreeWalker} 080 * </p> 081 * 082 * <p> 083 * Violation Message Keys: 084 * </p> 085 * <ul> 086 * <li> 087 * {@code name.invalidPattern} 088 * </li> 089 * </ul> 090 * 091 * @since 8.36 092 */ 093@StatelessCheck 094public class IllegalIdentifierNameCheck extends AbstractNameCheck { 095 096 /** 097 * Creates a new {@code IllegalIdentifierNameCheck} instance. 098 */ 099 public IllegalIdentifierNameCheck() { 100 super("(?i)^(?!(record|yield|var|permits|sealed)$).+$"); 101 } 102 103 @Override 104 public int[] getDefaultTokens() { 105 return getAcceptableTokens(); 106 } 107 108 @Override 109 public int[] getAcceptableTokens() { 110 return new int[] { 111 TokenTypes.CLASS_DEF, 112 TokenTypes.INTERFACE_DEF, 113 TokenTypes.ENUM_DEF, 114 TokenTypes.ANNOTATION_DEF, 115 TokenTypes.ANNOTATION_FIELD_DEF, 116 TokenTypes.PARAMETER_DEF, 117 TokenTypes.VARIABLE_DEF, 118 TokenTypes.METHOD_DEF, 119 TokenTypes.ENUM_CONSTANT_DEF, 120 TokenTypes.PATTERN_VARIABLE_DEF, 121 TokenTypes.RECORD_DEF, 122 TokenTypes.RECORD_COMPONENT_DEF, 123 TokenTypes.LAMBDA, 124 }; 125 } 126 127 @Override 128 public int[] getRequiredTokens() { 129 return CommonUtil.EMPTY_INT_ARRAY; 130 } 131 132 @Override 133 protected boolean mustCheckName(DetailAST ast) { 134 return ast.findFirstToken(TokenTypes.IDENT) != null; 135 } 136 137}