1 ///////////////////////////////////////////////////////////////////////////////////////////////
2 // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3 // Copyright (C) 2001-2026 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 *
31 * @since 3.0
32 */
33 public class StaticVariableNameCheck
34 extends AbstractAccessControlNameCheck {
35
36 /**
37 * A key is pointing to the warning message text in "messages.properties"
38 * file.
39 */
40 public static final String MSG_INVALID_PATTERN = "name.invalidPattern";
41
42 /** Creates a new {@code StaticVariableNameCheck} instance. */
43 public StaticVariableNameCheck() {
44 super("^[a-z][a-zA-Z0-9]*$", MSG_INVALID_PATTERN);
45 }
46
47 @Override
48 public int[] getDefaultTokens() {
49 return getRequiredTokens();
50 }
51
52 @Override
53 public int[] getAcceptableTokens() {
54 return getRequiredTokens();
55 }
56
57 @Override
58 public int[] getRequiredTokens() {
59 return new int[] {TokenTypes.VARIABLE_DEF};
60 }
61
62 /**
63 * Setter to control if check should apply to package-private members.
64 *
65 * @param applyTo new value of the property.
66 * @propertySince 5.0
67 */
68 @Override
69 public final void setApplyToPackage(boolean applyTo) {
70 super.setApplyToPackage(applyTo);
71 }
72
73 /**
74 * Setter to control if check should apply to private members.
75 *
76 * @param applyTo new value of the property.
77 * @propertySince 5.0
78 */
79 @Override
80 public final void setApplyToPrivate(boolean applyTo) {
81 super.setApplyToPrivate(applyTo);
82 }
83
84 /**
85 * Setter to control if check should apply to protected members.
86 *
87 * @param applyTo new value of the property.
88 * @propertySince 5.0
89 */
90 @Override
91 public final void setApplyToProtected(boolean applyTo) {
92 super.setApplyToProtected(applyTo);
93 }
94
95 /**
96 * Setter to control if check should apply to public members.
97 *
98 * @param applyTo new value of the property.
99 * @propertySince 5.0
100 */
101 @Override
102 public final void setApplyToPublic(boolean applyTo) {
103 super.setApplyToPublic(applyTo);
104 }
105
106 @Override
107 protected final boolean mustCheckName(DetailAST ast) {
108 final DetailAST modifiersAST =
109 ast.findFirstToken(TokenTypes.MODIFIERS);
110 final boolean isStatic = modifiersAST.findFirstToken(TokenTypes.LITERAL_STATIC) != null;
111 final boolean isFinal = modifiersAST.findFirstToken(TokenTypes.FINAL) != null;
112
113 return isStatic
114 && !isFinal
115 && shouldCheckInScope(modifiersAST)
116 && !ScopeUtil.isInInterfaceOrAnnotationBlock(ast);
117 }
118
119 }