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 *
031 * @since 3.0
032 */
033public class StaticVariableNameCheck
034    extends AbstractAccessControlNameCheck {
035
036    /** Creates a new {@code StaticVariableNameCheck} instance. */
037    public StaticVariableNameCheck() {
038        super("^[a-z][a-zA-Z0-9]*$");
039    }
040
041    @Override
042    public int[] getDefaultTokens() {
043        return getRequiredTokens();
044    }
045
046    @Override
047    public int[] getAcceptableTokens() {
048        return getRequiredTokens();
049    }
050
051    @Override
052    public int[] getRequiredTokens() {
053        return new int[] {TokenTypes.VARIABLE_DEF};
054    }
055
056    /**
057     * Setter to control if check should apply to package-private members.
058     *
059     * @param applyTo new value of the property.
060     * @propertySince 5.0
061     */
062    @Override
063    public final void setApplyToPackage(boolean applyTo) {
064        super.setApplyToPackage(applyTo);
065    }
066
067    /**
068     * Setter to control if check should apply to private members.
069     *
070     * @param applyTo new value of the property.
071     * @propertySince 5.0
072     */
073    @Override
074    public final void setApplyToPrivate(boolean applyTo) {
075        super.setApplyToPrivate(applyTo);
076    }
077
078    /**
079     * Setter to control if check should apply to protected members.
080     *
081     * @param applyTo new value of the property.
082     * @propertySince 5.0
083     */
084    @Override
085    public final void setApplyToProtected(boolean applyTo) {
086        super.setApplyToProtected(applyTo);
087    }
088
089    /**
090     * Setter to control if check should apply to public members.
091     *
092     * @param applyTo new value of the property.
093     * @propertySince 5.0
094     */
095    @Override
096    public final void setApplyToPublic(boolean applyTo) {
097        super.setApplyToPublic(applyTo);
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        final boolean isFinal = modifiersAST.findFirstToken(TokenTypes.FINAL) != null;
106
107        return isStatic
108                && !isFinal
109                && shouldCheckInScope(modifiersAST)
110                && !ScopeUtil.isInInterfaceOrAnnotationBlock(ast);
111    }
112
113}