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.ScopeUtils;
025
026/**
027 * <p>
028 * Checks that static, non-final variable names conform to a format specified
029 * by the format property. The format is a
030 * {@link java.util.regex.Pattern regular expression} and defaults to
031 * <strong>^[a-z][a-zA-Z0-9]*$</strong>.
032 * </p>
033 * <p>
034 * An example of how to configure the check is:
035 * </p>
036 * <pre>
037 * &lt;module name="StaticVariableName"/&gt;
038 * </pre>
039 * <p>
040 * An example of how to configure the check for names that begin with
041 * a lower case letter, followed by letters, digits, and underscores is:
042 * </p>
043 * <pre>
044 * &lt;module name="StaticVariableName"&gt;
045 *    &lt;property name="format" value="^[a-z](_?[a-zA-Z0-9]+)*$"/&gt;
046 * &lt;/module&gt;
047 * </pre>
048 * @author Rick Giles
049 */
050public class StaticVariableNameCheck
051    extends AbstractAccessControlNameCheck {
052
053    /** Creates a new {@code StaticVariableNameCheck} instance. */
054    public StaticVariableNameCheck() {
055        super("^[a-z][a-zA-Z0-9]*$");
056    }
057
058    @Override
059    public int[] getDefaultTokens() {
060        return getRequiredTokens();
061    }
062
063    @Override
064    public int[] getAcceptableTokens() {
065        return getRequiredTokens();
066    }
067
068    @Override
069    public int[] getRequiredTokens() {
070        return new int[] {TokenTypes.VARIABLE_DEF};
071    }
072
073    @Override
074    protected final boolean mustCheckName(DetailAST ast) {
075        final DetailAST modifiersAST =
076            ast.findFirstToken(TokenTypes.MODIFIERS);
077        final boolean isStatic = modifiersAST.findFirstToken(TokenTypes.LITERAL_STATIC) != null;
078        final boolean isFinal = modifiersAST.findFirstToken(TokenTypes.FINAL) != null;
079
080        return isStatic
081                && !isFinal
082                && shouldCheckInScope(modifiersAST)
083                && !ScopeUtils.isInInterfaceOrAnnotationBlock(ast);
084    }
085
086}