001//////////////////////////////////////////////////////////////////////////////// 002// checkstyle: Checks Java source code for adherence to a set of rules. 003// Copyright (C) 2001-2014 Oliver Burn 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//////////////////////////////////////////////////////////////////////////////// 019package com.puppycrawl.tools.checkstyle.checks.naming; 020 021import com.puppycrawl.tools.checkstyle.api.DetailAST; 022import com.puppycrawl.tools.checkstyle.api.ScopeUtils; 023import com.puppycrawl.tools.checkstyle.api.TokenTypes; 024 025/** 026 * <p> 027 * Checks that instance variable names conform to a format specified 028 * by the format property. The format is a 029 * {@link java.util.regex.Pattern regular expression} 030 * and defaults to 031 * <strong>^[a-z][a-zA-Z0-9]*$</strong>. 032 * </p> 033 * <p> 034 * An example of how to configure the check is: 035 * </p> 036 * <pre> 037 * <module name="MemberName"/> 038 * </pre> 039 * <p> 040 * An example of how to configure the check for names that begin with 041 * "m", followed by an upper case letter, and then letters and 042 * digits is: 043 * </p> 044 * <pre> 045 * <module name="MemberName"> 046 * <property name="format" value="^m[A-Z][a-zA-Z0-9]*$"/> 047 * </module> 048 * </pre> 049 * @author Rick Giles 050 * @version 1.0 051 */ 052public class MemberNameCheck 053 extends AbstractAccessControlNameCheck 054{ 055 /** Creates a new <code>MemberNameCheck</code> instance. */ 056 public MemberNameCheck() 057 { 058 super("^[a-z][a-zA-Z0-9]*$"); 059 } 060 061 @Override 062 public int[] getDefaultTokens() 063 { 064 return new int[] {TokenTypes.VARIABLE_DEF}; 065 } 066 067 @Override 068 protected final boolean mustCheckName(DetailAST ast) 069 { 070 final DetailAST modifiersAST = 071 ast.findFirstToken(TokenTypes.MODIFIERS); 072 final boolean isStatic = (modifiersAST != null) 073 && modifiersAST.branchContains(TokenTypes.LITERAL_STATIC); 074 075 return (!isStatic && !ScopeUtils.inInterfaceOrAnnotationBlock(ast) 076 && !ScopeUtils.isLocalVariableDef(ast)) 077 && shouldCheckInScope(modifiersAST); 078 } 079}