001///////////////////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code and other text files for adherence to a set of rules.
003// Copyright (C) 2001-2026 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 * <div>
028 * Checks that instance variable names conform to a specified pattern.
029 * </div>
030 *
031 * @since 3.0
032 */
033public class MemberNameCheck
034    extends AbstractAccessControlNameCheck {
035
036    /**
037     * A key is pointing to the warning message text in "messages.properties"
038     * file.
039     */
040    public static final String MSG_INVALID_PATTERN = "name.invalidPattern";
041
042    /** Creates a new {@code MemberNameCheck} instance. */
043    public MemberNameCheck() {
044        super("^[a-z][a-zA-Z0-9]*$", MSG_INVALID_PATTERN);
045    }
046
047    @Override
048    public int[] getDefaultTokens() {
049        return getRequiredTokens();
050    }
051
052    @Override
053    public int[] getAcceptableTokens() {
054        return getRequiredTokens();
055    }
056
057    @Override
058    public int[] getRequiredTokens() {
059        return new int[] {TokenTypes.VARIABLE_DEF};
060    }
061
062    /**
063     * Setter to control if check should apply to package-private members.
064     *
065     * @param applyTo new value of the property.
066     * @propertySince 3.4
067     */
068    @Override
069    public final void setApplyToPackage(boolean applyTo) {
070        super.setApplyToPackage(applyTo);
071    }
072
073    /**
074     * Setter to control if check should apply to private members.
075     *
076     * @param applyTo new value of the property.
077     * @propertySince 3.4
078     */
079    @Override
080    public final void setApplyToPrivate(boolean applyTo) {
081        super.setApplyToPrivate(applyTo);
082    }
083
084    /**
085     * Setter to control if check should apply to protected members.
086     *
087     * @param applyTo new value of the property.
088     * @propertySince 3.4
089     */
090    @Override
091    public final void setApplyToProtected(boolean applyTo) {
092        super.setApplyToProtected(applyTo);
093    }
094
095    /**
096     * Setter to control if check should apply to public members.
097     *
098     * @param applyTo new value of the property.
099     * @propertySince 3.4
100     */
101    @Override
102    public final void setApplyToPublic(boolean applyTo) {
103        super.setApplyToPublic(applyTo);
104    }
105
106    @Override
107    protected final boolean mustCheckName(DetailAST ast) {
108        final DetailAST modifiersAST =
109            ast.findFirstToken(TokenTypes.MODIFIERS);
110        final boolean isStatic = modifiersAST.findFirstToken(TokenTypes.LITERAL_STATIC) != null;
111
112        return !isStatic && !ScopeUtil.isInInterfaceOrAnnotationBlock(ast)
113            && !ScopeUtil.isLocalVariableDef(ast)
114                && shouldCheckInScope(modifiersAST);
115    }
116
117}