001/////////////////////////////////////////////////////////////////////////////////////////////// 002// checkstyle: Checks Java source code and other text files 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.naming; 021 022import com.puppycrawl.tools.checkstyle.api.DetailAST; 023import com.puppycrawl.tools.checkstyle.api.TokenTypes; 024 025/** 026 * <p> 027 * Checks that record component names conform to a specified pattern. 028 * </p> 029 * <ul> 030 * <li> 031 * Property {@code format} - Specifies valid identifiers. 032 * Type is {@code java.util.regex.Pattern}. 033 * Default value is {@code "^[a-z][a-zA-Z0-9]*$"}. 034 * </li> 035 * </ul> 036 * <p> 037 * To configure the check: 038 * </p> 039 * <pre> 040 * <module name="RecordComponentName"/> 041 * </pre> 042 * <p>Example:</p> 043 * <pre> 044 * record MyRecord1(String value, int otherComponentName) {} // OK 045 * record MyRecord2(String... Values) {} // violation, the record component name 046 * // should match the regular expression "^[a-z][a-zA-Z0-9]*$" 047 * record MyRecord3(double my_number) {} // violation, the record component name 048 * // should match the regular expression "^[a-z][a-zA-Z0-9]*$" 049 * </pre> 050 * <p> 051 * An example of how to configure the check for names that are only letters in lowercase: 052 * </p> 053 * <p>Configuration:</p> 054 * <pre> 055 * <module name="RecordComponentName"> 056 * <property name="format" value="^[a-z]+$"/> 057 * </module> 058 * </pre> 059 * <p>Example:</p> 060 * <pre> 061 * record MyRecord1(String value, int other) {} // OK 062 * record MyRecord2(String... strings) {} // OK 063 * record MyRecord3(double myNumber) {} // violation, the record component name 064 * // should match the regular expression "^[a-z]+$" 065 * </pre> 066 * <p> 067 * Parent is {@code com.puppycrawl.tools.checkstyle.TreeWalker} 068 * </p> 069 * <p> 070 * Violation Message Keys: 071 * </p> 072 * <ul> 073 * <li> 074 * {@code name.invalidPattern} 075 * </li> 076 * </ul> 077 * 078 * @since 8.40 079 */ 080public class RecordComponentNameCheck extends AbstractNameCheck { 081 082 /** Creates a new {@code RecordComponentNameCheck} instance. */ 083 public RecordComponentNameCheck() { 084 super("^[a-z][a-zA-Z0-9]*$"); 085 } 086 087 @Override 088 public int[] getDefaultTokens() { 089 return getRequiredTokens(); 090 } 091 092 @Override 093 public final int[] getAcceptableTokens() { 094 return getRequiredTokens(); 095 } 096 097 @Override 098 public int[] getRequiredTokens() { 099 return new int[] { 100 TokenTypes.RECORD_COMPONENT_DEF, 101 }; 102 } 103 104 @Override 105 protected final boolean mustCheckName(DetailAST ast) { 106 return true; 107 } 108 109}