View Javadoc
1   ////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code for adherence to a set of rules.
3   // Copyright (C) 2001-2017 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.ScopeUtils;
25  
26  /**
27   * <p>
28   * Checks that constant names conform to a format specified
29   * by the format property.
30   * A <em>constant</em> is a <strong>static</strong> and <strong>final</strong>
31   * field or an interface/annotation field, except
32   * <strong>serialVersionUID</strong> and <strong>serialPersistentFields
33   * </strong>.  The format is a regular expression
34   * and defaults to <strong>^[A-Z][A-Z0-9]*(_[A-Z0-9]+)*$</strong>.
35   * </p>
36   * <p>
37   * An example of how to configure the check is:
38   * </p>
39   * <pre>
40   * &lt;module name="ConstantName"/&gt;
41   * </pre>
42   *
43   * <p>
44   * An example of how to configure the check for names that are only upper case
45   * letters and digits is:
46   * </p>
47   * <pre>
48   * &lt;module name="ConstantName"&gt;
49   *    &lt;property name="format" value="^[A-Z][A-Z0-9]*$"/&gt;
50   * &lt;/module&gt;
51   * </pre>
52   *
53   *
54   * @author Rick Giles
55   */
56  public class ConstantNameCheck
57      extends AbstractAccessControlNameCheck {
58      /** Creates a new {@code ConstantNameCheck} instance. */
59      public ConstantNameCheck() {
60          super("^[A-Z][A-Z0-9]*(_[A-Z0-9]+)*$");
61      }
62  
63      @Override
64      public int[] getDefaultTokens() {
65          return getRequiredTokens();
66      }
67  
68      @Override
69      public int[] getAcceptableTokens() {
70          return getRequiredTokens();
71      }
72  
73      @Override
74      public int[] getRequiredTokens() {
75          return new int[] {TokenTypes.VARIABLE_DEF};
76      }
77  
78      @Override
79      protected final boolean mustCheckName(DetailAST ast) {
80          boolean returnValue = false;
81  
82          final DetailAST modifiersAST =
83              ast.findFirstToken(TokenTypes.MODIFIERS);
84          final boolean isStatic = modifiersAST.findFirstToken(TokenTypes.LITERAL_STATIC) != null;
85          final boolean isFinal = modifiersAST.findFirstToken(TokenTypes.FINAL) != null;
86  
87          if (isStatic && isFinal && shouldCheckInScope(modifiersAST)
88                  || ScopeUtils.isInAnnotationBlock(ast)
89                  || ScopeUtils.isInInterfaceOrAnnotationBlock(ast)
90                          && !ScopeUtils.isInCodeBlock(ast)) {
91              // Handle the serialVersionUID and serialPersistentFields constants
92              // which are used for Serialization. Cannot enforce rules on it. :-)
93              final DetailAST nameAST = ast.findFirstToken(TokenTypes.IDENT);
94              if (!"serialVersionUID".equals(nameAST.getText())
95                  && !"serialPersistentFields".equals(nameAST.getText())) {
96                  returnValue = true;
97              }
98          }
99  
100         return returnValue;
101     }
102 }