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