001////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code for adherence to a set of rules.
003// Copyright (C) 2001-2017 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    /** Creates a new {@code StaticVariableNameCheck} instance. */
053    public StaticVariableNameCheck() {
054        super("^[a-z][a-zA-Z0-9]*$");
055    }
056
057    @Override
058    public int[] getDefaultTokens() {
059        return getAcceptableTokens();
060    }
061
062    @Override
063    public int[] getAcceptableTokens() {
064        return new int[] {TokenTypes.VARIABLE_DEF};
065    }
066
067    @Override
068    public int[] getRequiredTokens() {
069        return getAcceptableTokens();
070    }
071
072    @Override
073    protected final boolean mustCheckName(DetailAST ast) {
074        final DetailAST modifiersAST =
075            ast.findFirstToken(TokenTypes.MODIFIERS);
076        final boolean isStatic = modifiersAST.branchContains(TokenTypes.LITERAL_STATIC);
077        final boolean isFinal = modifiersAST.branchContains(TokenTypes.FINAL);
078
079        return isStatic
080                && !isFinal
081                && shouldCheckInScope(modifiersAST)
082                && !ScopeUtils.isInInterfaceOrAnnotationBlock(ast);
083    }
084}