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