001//////////////////////////////////////////////////////////////////////////////// 002// checkstyle: Checks Java source code for adherence to a set of rules. 003// Copyright (C) 2001-2020 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 format} - Specifies valid identifiers. Default value is 033 * {@code "^[a-z][a-zA-Z0-9]*$"}. 034 * </li> 035 * <li> 036 * Property {@code applyToPublic} - Controls whether to apply the check to public member. 037 * Default value is {@code true}. 038 * </li> 039 * <li> 040 * Property {@code applyToProtected} - Controls whether to apply the check to protected member. 041 * Default value is {@code true}. 042 * </li> 043 * <li> 044 * Property {@code applyToPackage} - Controls whether to apply the check to package-private member. 045 * Default value is {@code true}. 046 * </li> 047 * <li> 048 * Property {@code applyToPrivate} - Controls whether to apply the check to private member. 049 * Default value is {@code true}. 050 * </li> 051 * </ul> 052 * <p> 053 * An example of how to configure the check is: 054 * </p> 055 * <pre> 056 * <module name="MemberName"/> 057 * </pre> 058 * <p>Code Example:</p> 059 * <pre> 060 * class MyClass { 061 * public int num1; // OK 062 * protected int num2; // OK 063 * final int num3 = 3; // OK 064 * private int num4; // OK 065 * 066 * static int num5; // ignored: not an instance variable 067 * public static final int CONSTANT = 1; // ignored: not an instance variable 068 * 069 * public int NUM1; // violation, name 'NUM1' 070 * // must match pattern '^[a-z][a-zA-Z0-9]*$' 071 * protected int NUM2; // violation, name 'NUM2' 072 * // must match pattern '^[a-z][a-zA-Z0-9]*$' 073 * final int NUM3; // violation, name 'NUM3' 074 * // must match pattern '^[a-z][a-zA-Z0-9]*$' 075 * private int NUM4; // violation, name 'NUM4' 076 * // must match pattern '^[a-z][a-zA-Z0-9]*$' 077 * } 078 * </pre> 079 * <p> 080 * An example of how to configure the check for names that begin with 081 * {@code "m"}, followed by an upper case letter, and then letters 082 * and digits. Also, suppress the check from being applied to protected 083 * and package-private member: 084 * </p> 085 * 086 * <pre> 087 * <module name="MemberName"> 088 * <property name="format" value="^m[A-Z][a-zA-Z0-9]*$"/> 089 * <property name="applyToProtected" value="false"/> 090 * <property name="applyToPackage" value="false"/> 091 * </module> 092 * </pre> 093 * <p>Code Example:</p> 094 * <pre> 095 * class MyClass { 096 * public int num1; // violation, name 'num1' 097 * // must match pattern '^m[A-Z][a-zA-Z0-9]*$' 098 * protected int num2; // OK 099 * int num3; // OK 100 * private int num4; // violation, name 'num4' 101 * // must match pattern '^m[A-Z][a-zA-Z0-9]*$' 102 * } 103 * </pre> 104 * <p> 105 * An example of how to suppress the check which is applied to 106 * public and private member: 107 * </p> 108 * <pre> 109 * <module name="MemberName"> 110 * <property name="applyToPublic" value="false"/> 111 * <property name="applyToPrivate" value="false"/> 112 * </module> 113 * </pre> 114 * <p>Code Example:</p> 115 * <pre> 116 * class MyClass { 117 * public int NUM1; // OK 118 * protected int NUM2; // violation, name 'NUM2' 119 * // must match pattern '^[a-z][a-zA-Z0-9]*$' 120 * int NUM3; // violation, name 'NUM3' 121 * // must match pattern '^[a-z][a-zA-Z0-9]*$' 122 * private int NUM4; // OK 123 * } 124 * </pre> 125 * 126 * @since 3.0 127 */ 128public class MemberNameCheck 129 extends AbstractAccessControlNameCheck { 130 131 /** Creates a new {@code MemberNameCheck} instance. */ 132 public MemberNameCheck() { 133 super("^[a-z][a-zA-Z0-9]*$"); 134 } 135 136 @Override 137 public int[] getDefaultTokens() { 138 return getRequiredTokens(); 139 } 140 141 @Override 142 public int[] getAcceptableTokens() { 143 return getRequiredTokens(); 144 } 145 146 @Override 147 public int[] getRequiredTokens() { 148 return new int[] {TokenTypes.VARIABLE_DEF}; 149 } 150 151 @Override 152 protected final boolean mustCheckName(DetailAST ast) { 153 final DetailAST modifiersAST = 154 ast.findFirstToken(TokenTypes.MODIFIERS); 155 final boolean isStatic = modifiersAST.findFirstToken(TokenTypes.LITERAL_STATIC) != null; 156 157 return !isStatic && !ScopeUtil.isInInterfaceOrAnnotationBlock(ast) 158 && !ScopeUtil.isLocalVariableDef(ast) 159 && shouldCheckInScope(modifiersAST); 160 } 161 162}