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