001////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code for adherence to a set of rules.
003// Copyright (C) 2001-2020 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 specified pattern.
029 * </p>
030 * <ul>
031 * <li>
032 * Property {@code format} - Specifies valid identifiers. Default value is
033 * {@code "^[a-z][a-zA-Z0-9]*$"}.
034 * </li>
035 * <li>
036 * Property {@code applyToPublic} - Controls whether to apply the check to public member.
037 * Default value is {@code true}.
038 * </li>
039 * <li>
040 * Property {@code applyToProtected} - Controls whether to apply the check to protected member.
041 * Default value is {@code true}.
042 * </li>
043 * <li>
044 * Property {@code applyToPackage} - Controls whether to apply the check to package-private member.
045 * Default value is {@code true}.
046 * </li>
047 * <li>
048 * Property {@code applyToPrivate} - Controls whether to apply the check to private member.
049 * Default value is {@code true}.
050 * </li>
051 * </ul>
052 * <p>
053 * An example of how to configure the check is:
054 * </p>
055 * <pre>
056 * &lt;module name=&quot;StaticVariableName&quot;/&gt;
057 * </pre>
058 * <p>Code Example:</p>
059 * <pre>
060 * class MyClass {
061 *   public static int goodStatic = 2; // OK
062 *   private static int BadStatic = 2; // violation, name 'BadStatic'
063 *                                     // must match pattern '^[a-z][a-zA-Z0-9]*$'
064 * }
065 * </pre>
066 * <p>
067 * An example of how to suppress the check to public and protected types is:
068 * </p>
069 * <pre>
070 * &lt;module name=&quot;StaticVariableName&quot;&gt;
071 *   &lt;property name=&quot;applyToPublic&quot; value=&quot;false&quot;/&gt;
072 *   &lt;property name=&quot;applyToProtected&quot; value=&quot;false&quot;/&gt;
073 * &lt;/module&gt;
074 * </pre>
075 * <p>Code Example:</p>
076 * <pre>
077 * class MyClass {
078 *   public static int GoodStatic1 = 2; // OK
079 *   protected static int GoodStatic2 = 2; //OK
080 *   private static int goodStatic = 2 // OK
081 *   private static int BadStatic = 2; // violation, name 'BadStatic'
082 *                                     // must match pattern '^[a-z][a-zA-Z0-9]*$'
083 * }
084 * </pre>
085 * <p>
086 * An example of how to configure the check for names that begin with
087 * a lower case letter, followed by letters, digits, and underscores.
088 * Also, suppress the check from being applied to private and package-private types:
089 * </p>
090 * <pre>
091 * &lt;module name=&quot;StaticVariableName&quot;&gt;
092 *   &lt;property name=&quot;format&quot; value=&quot;^[a-z](_?[a-zA-Z0-9]+)*$&quot;/&gt;
093 *   &lt;property name=&quot;applyToPrivate&quot; value=&quot;false&quot;/&gt;
094 *   &lt;property name=&quot;applyToPackage&quot; value=&quot;false&quot;/&gt;
095 * &lt;/module&gt;
096 * </pre>
097 * <p>Code Example:</p>
098 * <pre>
099 * class MyClass {
100 *   public static int good_static = 2; // OK
101 *   public static int Bad_Static = 2; // violation, name 'Bad_Static'
102 *                                     // must match pattern '^[a-z](_?[a-zA-Z0-9]+)*$'
103 *   private static int Good_Static1 = 2; // OK
104 *   static int Good_Static2 = 2; // OK
105 * }
106 * </pre>
107 *
108 * @since 3.0
109 */
110public class StaticVariableNameCheck
111    extends AbstractAccessControlNameCheck {
112
113    /** Creates a new {@code StaticVariableNameCheck} instance. */
114    public StaticVariableNameCheck() {
115        super("^[a-z][a-zA-Z0-9]*$");
116    }
117
118    @Override
119    public int[] getDefaultTokens() {
120        return getRequiredTokens();
121    }
122
123    @Override
124    public int[] getAcceptableTokens() {
125        return getRequiredTokens();
126    }
127
128    @Override
129    public int[] getRequiredTokens() {
130        return new int[] {TokenTypes.VARIABLE_DEF};
131    }
132
133    @Override
134    protected final boolean mustCheckName(DetailAST ast) {
135        final DetailAST modifiersAST =
136            ast.findFirstToken(TokenTypes.MODIFIERS);
137        final boolean isStatic = modifiersAST.findFirstToken(TokenTypes.LITERAL_STATIC) != null;
138        final boolean isFinal = modifiersAST.findFirstToken(TokenTypes.FINAL) != null;
139
140        return isStatic
141                && !isFinal
142                && shouldCheckInScope(modifiersAST)
143                && !ScopeUtil.isInInterfaceOrAnnotationBlock(ast);
144    }
145
146}