1 ///////////////////////////////////////////////////////////////////////////////////////////////
2 // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3 // Copyright (C) 2001-2025 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 constant names conform to a specified pattern.
29 * A <em>constant</em> is a <strong>static</strong> and <strong>final</strong>
30 * field or an interface/annotation field, except
31 * <strong>serialVersionUID</strong> and <strong>serialPersistentFields
32 * </strong>.
33 * </div>
34 *
35 * @since 3.0
36 */
37 public class ConstantNameCheck
38 extends AbstractAccessControlNameCheck {
39
40 /** Creates a new {@code ConstantNameCheck} instance. */
41 public ConstantNameCheck() {
42 super("^[A-Z][A-Z0-9]*(_[A-Z0-9]+)*$");
43 }
44
45 @Override
46 public int[] getDefaultTokens() {
47 return getRequiredTokens();
48 }
49
50 @Override
51 public int[] getAcceptableTokens() {
52 return getRequiredTokens();
53 }
54
55 @Override
56 public int[] getRequiredTokens() {
57 return new int[] {TokenTypes.VARIABLE_DEF};
58 }
59
60 /**
61 * Setter to control if check should apply to package-private members.
62 *
63 * @param applyTo new value of the property.
64 * @propertySince 5.0
65 */
66 @Override
67 public final void setApplyToPackage(boolean applyTo) {
68 super.setApplyToPackage(applyTo);
69 }
70
71 /**
72 * Setter to control if check should apply to private members.
73 *
74 * @param applyTo new value of the property.
75 * @propertySince 5.0
76 */
77 @Override
78 public final void setApplyToPrivate(boolean applyTo) {
79 super.setApplyToPrivate(applyTo);
80 }
81
82 /**
83 * Setter to control if check should apply to protected members.
84 *
85 * @param applyTo new value of the property.
86 * @propertySince 5.0
87 */
88 @Override
89 public final void setApplyToProtected(boolean applyTo) {
90 super.setApplyToProtected(applyTo);
91 }
92
93 /**
94 * Setter to control if check should apply to public members.
95 *
96 * @param applyTo new value of the property.
97 * @propertySince 5.0
98 */
99 @Override
100 public final void setApplyToPublic(boolean applyTo) {
101 super.setApplyToPublic(applyTo);
102 }
103
104 @Override
105 protected final boolean mustCheckName(DetailAST ast) {
106 boolean returnValue = false;
107
108 final DetailAST modifiersAST =
109 ast.findFirstToken(TokenTypes.MODIFIERS);
110 final boolean isStaticFinal =
111 modifiersAST.findFirstToken(TokenTypes.LITERAL_STATIC) != null
112 && modifiersAST.findFirstToken(TokenTypes.FINAL) != null
113 || ScopeUtil.isInAnnotationBlock(ast)
114 || ScopeUtil.isInInterfaceBlock(ast);
115 if (isStaticFinal && shouldCheckInScope(modifiersAST)
116 && !ScopeUtil.isInCodeBlock(ast)) {
117 // Handle the serialVersionUID and serialPersistentFields constants
118 // which are used for Serialization. Cannot enforce rules on it. :-)
119 final DetailAST nameAST = ast.findFirstToken(TokenTypes.IDENT);
120 if (!"serialVersionUID".equals(nameAST.getText())
121 && !"serialPersistentFields".equals(nameAST.getText())) {
122 returnValue = true;
123 }
124 }
125
126 return returnValue;
127 }
128
129 }