001///////////////////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code and other text files for adherence to a set of rules.
003// Copyright (C) 2001-2025 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 * <ul>
031 * <li>
032 * Property {@code applyToPackage} - Control if check should apply to package-private members.
033 * Type is {@code boolean}.
034 * Default value is {@code true}.
035 * Since version 3.4
036 * </li>
037 * <li>
038 * Property {@code applyToPrivate} - Control if check should apply to private members.
039 * Type is {@code boolean}.
040 * Default value is {@code true}.
041 * Since version 3.4
042 * </li>
043 * <li>
044 * Property {@code applyToProtected} - Control if check should apply to protected members.
045 * Type is {@code boolean}.
046 * Default value is {@code true}.
047 * Since version 3.4
048 * </li>
049 * <li>
050 * Property {@code applyToPublic} - Control if check should apply to public members.
051 * Type is {@code boolean}.
052 * Default value is {@code true}.
053 * Since version 3.4
054 * </li>
055 * <li>
056 * Property {@code format} - Sets the pattern to match valid identifiers.
057 * Type is {@code java.util.regex.Pattern}.
058 * Default value is {@code "^[a-z][a-zA-Z0-9]*$"}.
059 * </li>
060 * </ul>
061 *
062 * <p>
063 * Parent is {@code com.puppycrawl.tools.checkstyle.TreeWalker}
064 * </p>
065 *
066 * <p>
067 * Violation Message Keys:
068 * </p>
069 * <ul>
070 * <li>
071 * {@code name.invalidPattern}
072 * </li>
073 * </ul>
074 *
075 * @since 3.0
076 */
077public class MemberNameCheck
078    extends AbstractAccessControlNameCheck {
079
080    /** Creates a new {@code MemberNameCheck} instance. */
081    public MemberNameCheck() {
082        super("^[a-z][a-zA-Z0-9]*$");
083    }
084
085    @Override
086    public int[] getDefaultTokens() {
087        return getRequiredTokens();
088    }
089
090    @Override
091    public int[] getAcceptableTokens() {
092        return getRequiredTokens();
093    }
094
095    @Override
096    public int[] getRequiredTokens() {
097        return new int[] {TokenTypes.VARIABLE_DEF};
098    }
099
100    @Override
101    protected final boolean mustCheckName(DetailAST ast) {
102        final DetailAST modifiersAST =
103            ast.findFirstToken(TokenTypes.MODIFIERS);
104        final boolean isStatic = modifiersAST.findFirstToken(TokenTypes.LITERAL_STATIC) != null;
105
106        return !isStatic && !ScopeUtil.isInInterfaceOrAnnotationBlock(ast)
107            && !ScopeUtil.isLocalVariableDef(ast)
108                && shouldCheckInScope(modifiersAST);
109    }
110
111}