Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/puppycrawl/tools/checkstyle/checks/ConstantNameCheck.java


1   ////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code for adherence to a set of rules.
3   // Copyright (C) 2001-2002  Oliver Burn
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;
21  
22  import com.puppycrawl.tools.checkstyle.api.DetailAST;
23  import com.puppycrawl.tools.checkstyle.api.ScopeUtils;
24  import com.puppycrawl.tools.checkstyle.api.TokenTypes;
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 field, except <strong>serialVersionUID</strong>.
32   * The format is a regular expression and defaults to
33   * <strong>^[A-Z](_?[A-Z0-9]+)*$</strong>.
34   * </p>
35   * <p>
36   * An example of how to configure the check is:
37   * </p>
38   * <pre>
39   * &lt;module name="ConstantName"/&gt;
40   * </pre>
41   * 
42   * <p>
43   * An example of how to configure the check for names that are only upper case
44   * letters and digits is:
45   * </p>
46   * <pre>
47   * &lt;module name="ConstantName"&gt;
48   *    &lt;property name="format" value="^[A-Z][A-Z0-9]*$"/&gt;
49   * &lt;/module&gt;
50   * </pre>
51   *   
52   *
53   * @author Rick Giles
54   * @version 1.0
55   */
56  public class ConstantNameCheck
57      extends AbstractNameCheck
58  {
59      /** Creates a new <code>ConstantNameCheck</code> instance. */
60      public ConstantNameCheck()
61      {
62          super("^[A-Z](_?[A-Z0-9]+)*$");
63      }
64  
65      /** @see com.puppycrawl.tools.checkstyle.api.Check */
66      public int[] getDefaultTokens()
67      {
68          return new int[] {TokenTypes.VARIABLE_DEF};
69      }
70  
71      /** @see com.puppycrawl.tools.checkstyle.checks.AbstractNameCheck */
72      protected final boolean mustCheckName(DetailAST aAST)
73      {
74          boolean retVal = false;
75  
76          DetailAST modifiersAST = aAST.findFirstToken(TokenTypes.MODIFIERS);
77          final boolean isStatic = modifiersAST != null
78              && modifiersAST.branchContains(TokenTypes.LITERAL_STATIC);
79          final boolean isFinal = modifiersAST != null
80              && modifiersAST.branchContains(TokenTypes.FINAL);
81  
82          if ((isStatic  && isFinal) || ScopeUtils.inInterfaceBlock(aAST)) {
83              // Handle the serialVersionUID constant which is used for
84              // Serialization. Cannot enforce rules on it. :-)
85              final DetailAST nameAST = aAST.findFirstToken(TokenTypes.IDENT);
86              if ((nameAST != null)
87                  && !("serialVersionUID".equals(nameAST.getText())))
88              {
89                  retVal = true;
90              }
91          }
92  
93          return retVal;
94      }
95  }