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.ScopeUtil;
025
026/**
027 * <p>
028 * Checks that {@code static}, non-{@code final} variable names conform to a format specified
029 * by the format property.
030 * </p>
031 * <ul>
032 * <li>
033 * Property {@code format} - Specifies valid identifiers. Default value is
034 * {@code "^[a-z][a-zA-Z0-9]*$"}.
035 * </li>
036 * <li>
037 * Property {@code applyToPublic} - Controls whether to apply the check to public member.
038 * Default value is {@code true}.
039 * </li>
040 * <li>
041 * Property {@code applyToProtected} - Controls whether to apply the check to protected member.
042 * Default value is {@code true}.
043 * </li>
044 * <li>
045 * Property {@code applyToPackage} - Controls whether to apply the check to package-private member.
046 * Default value is {@code true}.
047 * </li>
048 * <li>
049 * Property {@code applyToPrivate} - Controls whether to apply the check to private member.
050 * Default value is {@code true}.
051 * </li>
052 * </ul>
053 * <p>
054 * An example of how to configure the check is:
055 * </p>
056 * <pre>
057 * &lt;module name="StaticVariableName"/&gt;
058 * </pre>
059 * <p>
060 * An example of how to configure the check for names that begin with
061 * a lower case letter, followed by letters, digits, and underscores is:
062 * </p>
063 * <pre>
064 * &lt;module name="StaticVariableName"&gt;
065 *   &lt;property name="format" value="^[a-z](_?[a-zA-Z0-9]+)*$"/&gt;
066 * &lt;/module&gt;
067 * </pre>
068 *
069 * @since 3.0
070 */
071public class StaticVariableNameCheck
072    extends AbstractAccessControlNameCheck {
073
074    /** Creates a new {@code StaticVariableNameCheck} instance. */
075    public StaticVariableNameCheck() {
076        super("^[a-z][a-zA-Z0-9]*$");
077    }
078
079    @Override
080    public int[] getDefaultTokens() {
081        return getRequiredTokens();
082    }
083
084    @Override
085    public int[] getAcceptableTokens() {
086        return getRequiredTokens();
087    }
088
089    @Override
090    public int[] getRequiredTokens() {
091        return new int[] {TokenTypes.VARIABLE_DEF};
092    }
093
094    @Override
095    protected final boolean mustCheckName(DetailAST ast) {
096        final DetailAST modifiersAST =
097            ast.findFirstToken(TokenTypes.MODIFIERS);
098        final boolean isStatic = modifiersAST.findFirstToken(TokenTypes.LITERAL_STATIC) != null;
099        final boolean isFinal = modifiersAST.findFirstToken(TokenTypes.FINAL) != null;
100
101        return isStatic
102                && !isFinal
103                && shouldCheckInScope(modifiersAST)
104                && !ScopeUtil.isInInterfaceOrAnnotationBlock(ast);
105    }
106
107}