001////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code for adherence to a set of rules.
003// Copyright (C) 2001-2014  Oliver Burn
004//
005// This library is free software; you can redistribute it and/or
006// modify it under the terms of the GNU Lesser General Public
007// License as published by the Free Software Foundation; either
008// version 2.1 of the License, or (at your option) any later version.
009//
010// This library is distributed in the hope that it will be useful,
011// but WITHOUT ANY WARRANTY; without even the implied warranty of
012// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
013// Lesser General Public License for more details.
014//
015// You should have received a copy of the GNU Lesser General Public
016// License along with this library; if not, write to the Free Software
017// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
018////////////////////////////////////////////////////////////////////////////////
019
020package com.puppycrawl.tools.checkstyle.checks.naming;
021
022import com.puppycrawl.tools.checkstyle.api.DetailAST;
023import com.puppycrawl.tools.checkstyle.api.ScopeUtils;
024import com.puppycrawl.tools.checkstyle.api.TokenTypes;
025
026/**
027 * <p>
028 * Checks that constant names conform to a format specified
029 * by the format property.
030 * A <em>constant</em> is a <strong>static</strong> and <strong>final</strong>
031 * field or an interface/annotation field, except
032 * <strong>serialVersionUID</strong> and <strong>serialPersistentFields
033 * </strong>.  The format is a regular expression
034 * and defaults to <strong>^[A-Z][A-Z0-9]*(_[A-Z0-9]+)*$</strong>.
035 * </p>
036 * <p>
037 * An example of how to configure the check is:
038 * </p>
039 * <pre>
040 * &lt;module name="ConstantName"/&gt;
041 * </pre>
042 *
043 * <p>
044 * An example of how to configure the check for names that are only upper case
045 * letters and digits is:
046 * </p>
047 * <pre>
048 * &lt;module name="ConstantName"&gt;
049 *    &lt;property name="format" value="^[A-Z][A-Z0-9]*$"/&gt;
050 * &lt;/module&gt;
051 * </pre>
052 *
053 *
054 * @author Rick Giles
055 * @version 1.0
056 */
057public class ConstantNameCheck
058    extends AbstractAccessControlNameCheck
059{
060    /** Creates a new <code>ConstantNameCheck</code> instance. */
061    public ConstantNameCheck()
062    {
063        super("^[A-Z][A-Z0-9]*(_[A-Z0-9]+)*$");
064    }
065
066    @Override
067    public int[] getDefaultTokens()
068    {
069        return new int[] {TokenTypes.VARIABLE_DEF};
070    }
071
072    @Override
073    protected final boolean mustCheckName(DetailAST ast)
074    {
075        boolean retVal = false;
076
077        final DetailAST modifiersAST =
078            ast.findFirstToken(TokenTypes.MODIFIERS);
079        final boolean isStatic = (modifiersAST != null)
080            && modifiersAST.branchContains(TokenTypes.LITERAL_STATIC);
081        final boolean isFinal = (modifiersAST != null)
082            && modifiersAST.branchContains(TokenTypes.FINAL);
083
084        if ((isStatic  && isFinal && shouldCheckInScope(modifiersAST))
085                || ScopeUtils.inAnnotationBlock(ast)
086                || (ScopeUtils.inInterfaceOrAnnotationBlock(ast)
087                        && !ScopeUtils.inCodeBlock(ast)))
088        {
089            // Handle the serialVersionUID and serialPersistentFields  constants
090            // which are used for Serialization. Cannot enforce rules on it. :-)
091            final DetailAST nameAST = ast.findFirstToken(TokenTypes.IDENT);
092            if ((nameAST != null)
093                && !("serialVersionUID".equals(nameAST.getText()))
094                && !("serialPersistentFields".equals(nameAST.getText())))
095            {
096                retVal = true;
097            }
098        }
099
100        return retVal;
101    }
102}