001///////////////////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code and other text files for adherence to a set of rules.
003// Copyright (C) 2001-2026 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.ScopeUtil;
025
026/**
027 * <div>
028 * Checks that constant names conform to a specified pattern.
029 * A <em>constant</em> is a <strong>static</strong> and <strong>final</strong>
030 * field or an interface/annotation field, except
031 * <strong>serialVersionUID</strong> and <strong>serialPersistentFields
032 * </strong>.
033 * </div>
034 *
035 * @since 3.0
036 */
037public class ConstantNameCheck
038    extends AbstractAccessControlNameCheck {
039
040    /**
041     * A key is pointing to the warning message text in "messages.properties"
042     * file.
043     */
044    public static final String MSG_INVALID_PATTERN = "name.invalidPattern";
045
046    /** Creates a new {@code ConstantNameCheck} instance. */
047    public ConstantNameCheck() {
048        super("^[A-Z][A-Z0-9]*(_[A-Z0-9]+)*$", MSG_INVALID_PATTERN);
049    }
050
051    @Override
052    public int[] getDefaultTokens() {
053        return getRequiredTokens();
054    }
055
056    @Override
057    public int[] getAcceptableTokens() {
058        return getRequiredTokens();
059    }
060
061    @Override
062    public int[] getRequiredTokens() {
063        return new int[] {TokenTypes.VARIABLE_DEF};
064    }
065
066    /**
067     * Setter to control if check should apply to package-private members.
068     *
069     * @param applyTo new value of the property.
070     * @propertySince 5.0
071     */
072    @Override
073    public final void setApplyToPackage(boolean applyTo) {
074        super.setApplyToPackage(applyTo);
075    }
076
077    /**
078     * Setter to control if check should apply to private members.
079     *
080     * @param applyTo new value of the property.
081     * @propertySince 5.0
082     */
083    @Override
084    public final void setApplyToPrivate(boolean applyTo) {
085        super.setApplyToPrivate(applyTo);
086    }
087
088    /**
089     * Setter to control if check should apply to protected members.
090     *
091     * @param applyTo new value of the property.
092     * @propertySince 5.0
093     */
094    @Override
095    public final void setApplyToProtected(boolean applyTo) {
096        super.setApplyToProtected(applyTo);
097    }
098
099    /**
100     * Setter to control if check should apply to public members.
101     *
102     * @param applyTo new value of the property.
103     * @propertySince 5.0
104     */
105    @Override
106    public final void setApplyToPublic(boolean applyTo) {
107        super.setApplyToPublic(applyTo);
108    }
109
110    @Override
111    protected final boolean mustCheckName(DetailAST ast) {
112        boolean returnValue = false;
113
114        final DetailAST modifiersAST =
115            ast.findFirstToken(TokenTypes.MODIFIERS);
116        final boolean isStaticFinal =
117            modifiersAST.findFirstToken(TokenTypes.LITERAL_STATIC) != null
118                && modifiersAST.findFirstToken(TokenTypes.FINAL) != null
119            || ScopeUtil.isInAnnotationBlock(ast)
120            || ScopeUtil.isInInterfaceBlock(ast);
121        if (isStaticFinal && shouldCheckInScope(modifiersAST)
122                        && !ScopeUtil.isInCodeBlock(ast)) {
123            // Handle the serialVersionUID and serialPersistentFields constants
124            // which are used for Serialization. Cannot enforce rules on it. :-)
125            final DetailAST nameAST = ast.findFirstToken(TokenTypes.IDENT);
126            if (!"serialVersionUID".equals(nameAST.getText())
127                && !"serialPersistentFields".equals(nameAST.getText())) {
128                returnValue = true;
129            }
130        }
131
132        return returnValue;
133    }
134
135}