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.api.DetailAST; 023import com.puppycrawl.tools.checkstyle.api.TokenTypes; 024import com.puppycrawl.tools.checkstyle.utils.ScopeUtil; 025 026/** 027 * <p> 028 * Checks that instance variable names conform to a specified pattern. 029 * </p> 030 * <ul> 031 * <li> 032 * Property {@code applyToPackage} - Control if check should apply to package-private members. 033 * Type is {@code boolean}. 034 * Default value is {@code true}. 035 * </li> 036 * <li> 037 * Property {@code applyToPrivate} - Control if check should apply to private members. 038 * Type is {@code boolean}. 039 * Default value is {@code true}. 040 * </li> 041 * <li> 042 * Property {@code applyToProtected} - Control if check should apply to protected members. 043 * Type is {@code boolean}. 044 * Default value is {@code true}. 045 * </li> 046 * <li> 047 * Property {@code applyToPublic} - Control if check should apply to public members. 048 * Type is {@code boolean}. 049 * Default value is {@code true}. 050 * </li> 051 * <li> 052 * Property {@code format} - Sets the pattern to match valid identifiers. 053 * Type is {@code java.util.regex.Pattern}. 054 * Default value is {@code "^[a-z][a-zA-Z0-9]*$"}. 055 * </li> 056 * </ul> 057 * <p> 058 * Parent is {@code com.puppycrawl.tools.checkstyle.TreeWalker} 059 * </p> 060 * <p> 061 * Violation Message Keys: 062 * </p> 063 * <ul> 064 * <li> 065 * {@code name.invalidPattern} 066 * </li> 067 * </ul> 068 * 069 * @since 3.0 070 */ 071public class MemberNameCheck 072 extends AbstractAccessControlNameCheck { 073 074 /** Creates a new {@code MemberNameCheck} instance. */ 075 public MemberNameCheck() { 076 super("^[a-z][a-zA-Z0-9]*$"); 077 } 078 079 @Override 080 public int[] getDefaultTokens() { 081 return getRequiredTokens(); 082 } 083 084 @Override 085 public int[] getAcceptableTokens() { 086 return getRequiredTokens(); 087 } 088 089 @Override 090 public int[] getRequiredTokens() { 091 return new int[] {TokenTypes.VARIABLE_DEF}; 092 } 093 094 @Override 095 protected final boolean mustCheckName(DetailAST ast) { 096 final DetailAST modifiersAST = 097 ast.findFirstToken(TokenTypes.MODIFIERS); 098 final boolean isStatic = modifiersAST.findFirstToken(TokenTypes.LITERAL_STATIC) != null; 099 100 return !isStatic && !ScopeUtil.isInInterfaceOrAnnotationBlock(ast) 101 && !ScopeUtil.isLocalVariableDef(ast) 102 && shouldCheckInScope(modifiersAST); 103 } 104 105}