01: ////////////////////////////////////////////////////////////////////////////////
02: // checkstyle: Checks Java source code for adherence to a set of rules.
03: // Copyright (C) 2001-2007 Oliver Burn
04: //
05: // This library is free software; you can redistribute it and/or
06: // modify it under the terms of the GNU Lesser General Public
07: // License as published by the Free Software Foundation; either
08: // version 2.1 of the License, or (at your option) any later version.
09: //
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.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/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: * <module name="ConstantName"/>
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: * <module name="ConstantName">
49: * <property name="format" value="^[A-Z][A-Z0-9]*$"/>
50: * </module>
51: * </pre>
52: *
53: *
54: * @author Rick Giles
55: * @version 1.0
56: */
57: public class ConstantNameCheck extends AbstractNameCheck {
58: /** Creates a new <code>ConstantNameCheck</code> instance. */
59: public ConstantNameCheck() {
60: super ("^[A-Z][A-Z0-9]*(_[A-Z0-9]+)*$");
61: }
62:
63: /** {@inheritDoc} */
64: public int[] getDefaultTokens() {
65: return new int[] { TokenTypes.VARIABLE_DEF };
66: }
67:
68: /** {@inheritDoc} */
69: protected final boolean mustCheckName(DetailAST aAST) {
70: boolean retVal = false;
71:
72: final DetailAST modifiersAST = aAST
73: .findFirstToken(TokenTypes.MODIFIERS);
74: final boolean isStatic = (modifiersAST != null)
75: && modifiersAST
76: .branchContains(TokenTypes.LITERAL_STATIC);
77: final boolean isFinal = (modifiersAST != null)
78: && modifiersAST.branchContains(TokenTypes.FINAL);
79:
80: if ((isStatic && isFinal)
81: || ScopeUtils.inInterfaceOrAnnotationBlock(aAST)) {
82: // Handle the serialVersionUID and serialPersistentFields constants
83: // which are used for Serialization. Cannot enforce rules on it. :-)
84: final DetailAST nameAST = aAST
85: .findFirstToken(TokenTypes.IDENT);
86: if ((nameAST != null)
87: && !("serialVersionUID".equals(nameAST.getText()))
88: && !("serialPersistentFields".equals(nameAST
89: .getText()))) {
90: retVal = true;
91: }
92: }
93:
94: return retVal;
95: }
96: }
|