001////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code for adherence to a set of rules.
003// Copyright (C) 2001-2017 the original author or authors.
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.TokenTypes;
024import com.puppycrawl.tools.checkstyle.utils.ScopeUtils;
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 */
056public class ConstantNameCheck
057    extends AbstractAccessControlNameCheck {
058    /** Creates a new {@code ConstantNameCheck} instance. */
059    public ConstantNameCheck() {
060        super("^[A-Z][A-Z0-9]*(_[A-Z0-9]+)*$");
061    }
062
063    @Override
064    public int[] getDefaultTokens() {
065        return getRequiredTokens();
066    }
067
068    @Override
069    public int[] getAcceptableTokens() {
070        return getRequiredTokens();
071    }
072
073    @Override
074    public int[] getRequiredTokens() {
075        return new int[] {TokenTypes.VARIABLE_DEF};
076    }
077
078    @Override
079    protected final boolean mustCheckName(DetailAST ast) {
080        boolean returnValue = false;
081
082        final DetailAST modifiersAST =
083            ast.findFirstToken(TokenTypes.MODIFIERS);
084        final boolean isStatic = modifiersAST.findFirstToken(TokenTypes.LITERAL_STATIC) != null;
085        final boolean isFinal = modifiersAST.findFirstToken(TokenTypes.FINAL) != null;
086
087        if (isStatic && isFinal && shouldCheckInScope(modifiersAST)
088                || ScopeUtils.isInAnnotationBlock(ast)
089                || ScopeUtils.isInInterfaceOrAnnotationBlock(ast)
090                        && !ScopeUtils.isInCodeBlock(ast)) {
091            // Handle the serialVersionUID and serialPersistentFields constants
092            // which are used for Serialization. Cannot enforce rules on it. :-)
093            final DetailAST nameAST = ast.findFirstToken(TokenTypes.IDENT);
094            if (!"serialVersionUID".equals(nameAST.getText())
095                && !"serialPersistentFields".equals(nameAST.getText())) {
096                returnValue = true;
097            }
098        }
099
100        return returnValue;
101    }
102}