1 ///////////////////////////////////////////////////////////////////////////////////////////////
2 // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3 // Copyright (C) 2001-2024 the original author or authors.
4 //
5 // This library is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU Lesser General Public
7 // License as published by the Free Software Foundation; either
8 // version 2.1 of the License, or (at your option) any later version.
9 //
10 // This library is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // Lesser General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public
16 // License along with this library; if not, write to the Free Software
17 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 ///////////////////////////////////////////////////////////////////////////////////////////////
19
20 package com.puppycrawl.tools.checkstyle.checks.naming;
21
22 import com.puppycrawl.tools.checkstyle.api.DetailAST;
23 import com.puppycrawl.tools.checkstyle.api.TokenTypes;
24 import com.puppycrawl.tools.checkstyle.utils.ScopeUtil;
25
26 /**
27 * <div>
28 * Checks that {@code static}, non-{@code final} variable names conform to a specified pattern.
29 * </div>
30 * <ul>
31 * <li>
32 * Property {@code applyToPackage} - Control if check should apply to package-private
33 * members.
34 * Type is {@code boolean}.
35 * Default value is {@code true}.
36 * </li>
37 * <li>
38 * Property {@code applyToPrivate} - Control if check should apply to private members.
39 * Type is {@code boolean}.
40 * Default value is {@code true}.
41 * </li>
42 * <li>
43 * Property {@code applyToProtected} - Control if check should apply to protected
44 * members.
45 * Type is {@code boolean}.
46 * Default value is {@code true}.
47 * </li>
48 * <li>
49 * Property {@code applyToPublic} - Control if check should apply to public members.
50 * Type is {@code boolean}.
51 * Default value is {@code true}.
52 * </li>
53 * <li>
54 * Property {@code format} - Sets the pattern to match valid identifiers.
55 * Type is {@code java.util.regex.Pattern}.
56 * Default value is {@code "^[a-z][a-zA-Z0-9]*$"}.
57 * </li>
58 * </ul>
59 *
60 * <p>
61 * Parent is {@code com.puppycrawl.tools.checkstyle.TreeWalker}
62 * </p>
63 *
64 * <p>
65 * Violation Message Keys:
66 * </p>
67 * <ul>
68 * <li>
69 * {@code name.invalidPattern}
70 * </li>
71 * </ul>
72 *
73 * @since 3.0
74 */
75 public class StaticVariableNameCheck
76 extends AbstractAccessControlNameCheck {
77
78 /** Creates a new {@code StaticVariableNameCheck} instance. */
79 public StaticVariableNameCheck() {
80 super("^[a-z][a-zA-Z0-9]*$");
81 }
82
83 @Override
84 public int[] getDefaultTokens() {
85 return getRequiredTokens();
86 }
87
88 @Override
89 public int[] getAcceptableTokens() {
90 return getRequiredTokens();
91 }
92
93 @Override
94 public int[] getRequiredTokens() {
95 return new int[] {TokenTypes.VARIABLE_DEF};
96 }
97
98 @Override
99 protected final boolean mustCheckName(DetailAST ast) {
100 final DetailAST modifiersAST =
101 ast.findFirstToken(TokenTypes.MODIFIERS);
102 final boolean isStatic = modifiersAST.findFirstToken(TokenTypes.LITERAL_STATIC) != null;
103 final boolean isFinal = modifiersAST.findFirstToken(TokenTypes.FINAL) != null;
104
105 return isStatic
106 && !isFinal
107 && shouldCheckInScope(modifiersAST)
108 && !ScopeUtil.isInInterfaceOrAnnotationBlock(ast);
109 }
110
111 }