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