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