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.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 * &lt;module name="MemberName"/&gt;
058 * </pre>
059 * <p>
060 * An example of how to configure the check for names that begin with
061 * {@code "m"}, followed by an upper case letter, and then letters and
062 * digits is:
063 * </p>
064 * <pre>
065 * &lt;module name="MemberName"&gt;
066 *   &lt;property name="format" value="^m[A-Z][a-zA-Z0-9]*$"/&gt;
067 * &lt;/module&gt;
068 * </pre>
069 *
070 * @since 3.0
071 */
072public class MemberNameCheck
073    extends AbstractAccessControlNameCheck {
074
075    /** Creates a new {@code MemberNameCheck} instance. */
076    public MemberNameCheck() {
077        super("^[a-z][a-zA-Z0-9]*$");
078    }
079
080    @Override
081    public int[] getDefaultTokens() {
082        return getRequiredTokens();
083    }
084
085    @Override
086    public int[] getAcceptableTokens() {
087        return getRequiredTokens();
088    }
089
090    @Override
091    public int[] getRequiredTokens() {
092        return new int[] {TokenTypes.VARIABLE_DEF};
093    }
094
095    @Override
096    protected final boolean mustCheckName(DetailAST ast) {
097        final DetailAST modifiersAST =
098            ast.findFirstToken(TokenTypes.MODIFIERS);
099        final boolean isStatic = modifiersAST.findFirstToken(TokenTypes.LITERAL_STATIC) != null;
100
101        return !isStatic && !ScopeUtil.isInInterfaceOrAnnotationBlock(ast)
102            && !ScopeUtil.isLocalVariableDef(ast)
103                && shouldCheckInScope(modifiersAST);
104    }
105
106}