View Javadoc
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   * <ul>
36   * <li>
37   * Property {@code applyToPackage} - Control if check should apply to package-private members.
38   * Type is {@code boolean}.
39   * Default value is {@code true}.
40   * Since version 5.0
41   * </li>
42   * <li>
43   * Property {@code applyToPrivate} - Control if check should apply to private members.
44   * Type is {@code boolean}.
45   * Default value is {@code true}.
46   * Since version 5.0
47   * </li>
48   * <li>
49   * Property {@code applyToProtected} - Control if check should apply to protected members.
50   * Type is {@code boolean}.
51   * Default value is {@code true}.
52   * Since version 5.0
53   * </li>
54   * <li>
55   * Property {@code applyToPublic} - Control if check should apply to public members.
56   * Type is {@code boolean}.
57   * Default value is {@code true}.
58   * Since version 5.0
59   * </li>
60   * <li>
61   * Property {@code format} - Sets the pattern to match valid identifiers.
62   * Type is {@code java.util.regex.Pattern}.
63   * Default value is {@code "^[A-Z][A-Z0-9]*(_[A-Z0-9]+)*$"}.
64   * </li>
65   * </ul>
66   *
67   * <p>
68   * Parent is {@code com.puppycrawl.tools.checkstyle.TreeWalker}
69   * </p>
70   *
71   * <p>
72   * Violation Message Keys:
73   * </p>
74   * <ul>
75   * <li>
76   * {@code name.invalidPattern}
77   * </li>
78   * </ul>
79   *
80   * @since 3.0
81   */
82  public class ConstantNameCheck
83      extends AbstractAccessControlNameCheck {
84  
85      /** Creates a new {@code ConstantNameCheck} instance. */
86      public ConstantNameCheck() {
87          super("^[A-Z][A-Z0-9]*(_[A-Z0-9]+)*$");
88      }
89  
90      @Override
91      public int[] getDefaultTokens() {
92          return getRequiredTokens();
93      }
94  
95      @Override
96      public int[] getAcceptableTokens() {
97          return getRequiredTokens();
98      }
99  
100     @Override
101     public int[] getRequiredTokens() {
102         return new int[] {TokenTypes.VARIABLE_DEF};
103     }
104 
105     @Override
106     protected final boolean mustCheckName(DetailAST ast) {
107         boolean returnValue = false;
108 
109         final DetailAST modifiersAST =
110             ast.findFirstToken(TokenTypes.MODIFIERS);
111         final boolean isStaticFinal =
112             modifiersAST.findFirstToken(TokenTypes.LITERAL_STATIC) != null
113                 && modifiersAST.findFirstToken(TokenTypes.FINAL) != null
114             || ScopeUtil.isInAnnotationBlock(ast)
115             || ScopeUtil.isInInterfaceBlock(ast);
116         if (isStaticFinal && shouldCheckInScope(modifiersAST)
117                         && !ScopeUtil.isInCodeBlock(ast)) {
118             // Handle the serialVersionUID and serialPersistentFields constants
119             // which are used for Serialization. Cannot enforce rules on it. :-)
120             final DetailAST nameAST = ast.findFirstToken(TokenTypes.IDENT);
121             if (!"serialVersionUID".equals(nameAST.getText())
122                 && !"serialPersistentFields".equals(nameAST.getText())) {
123                 returnValue = true;
124             }
125         }
126 
127         return returnValue;
128     }
129 
130 }