001///////////////////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code and other text files for adherence to a set of rules.
003// Copyright (C) 2001-2024 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.TokenTypes;
023import com.puppycrawl.tools.checkstyle.utils.CommonUtil;
024
025/**
026 * <p>
027 * Checks that type names conform to a specified pattern.
028 * </p>
029 * <ul>
030 * <li>
031 * Property {@code applyToPackage} - Control if check should apply to package-private
032 *   members.
033 * Type is {@code boolean}.
034 * Default value is {@code true}.
035 * </li>
036 * <li>
037 * Property {@code applyToPrivate} - Control if check should apply to private members.
038 * Type is {@code boolean}.
039 * Default value is {@code true}.
040 * </li>
041 * <li>
042 * Property {@code applyToProtected} - Control if check should apply to protected
043 *   members.
044 * Type is {@code boolean}.
045 * Default value is {@code true}.
046 * </li>
047 * <li>
048 * Property {@code applyToPublic} - Control if check should apply to public members.
049 * Type is {@code boolean}.
050 * Default value is {@code true}.
051 * </li>
052 * <li>
053 * Property {@code format} - Sets the pattern to match valid identifiers.
054 * Type is {@code java.util.regex.Pattern}.
055 * Default value is {@code "^[A-Z][a-zA-Z0-9]*$"}.
056 * </li>
057 * <li>
058 * Property {@code tokens} - tokens to check
059 * Type is {@code java.lang.String[]}.
060 * Validation type is {@code tokenSet}.
061 * Default value is:
062 * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#CLASS_DEF">
063 * CLASS_DEF</a>,
064 * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#INTERFACE_DEF">
065 * INTERFACE_DEF</a>,
066 * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#ENUM_DEF">
067 * ENUM_DEF</a>,
068 * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#ANNOTATION_DEF">
069 * ANNOTATION_DEF</a>,
070 * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#RECORD_DEF">
071 * RECORD_DEF</a>.
072 * </li>
073 * </ul>
074 * <p>
075 * Parent is {@code com.puppycrawl.tools.checkstyle.TreeWalker}
076 * </p>
077 * <p>
078 * Violation Message Keys:
079 * </p>
080 * <ul>
081 * <li>
082 * {@code name.invalidPattern}
083 * </li>
084 * </ul>
085 *
086 * @since 3.0
087 */
088public class TypeNameCheck
089    extends AbstractAccessControlNameCheck {
090
091    /**
092     * Default pattern for type name.
093     */
094    public static final String DEFAULT_PATTERN = "^[A-Z][a-zA-Z0-9]*$";
095
096    /**
097     * Creates a new {@code TypeNameCheck} instance.
098     */
099    public TypeNameCheck() {
100        super(DEFAULT_PATTERN);
101    }
102
103    @Override
104    public int[] getDefaultTokens() {
105        return getAcceptableTokens();
106    }
107
108    @Override
109    public int[] getAcceptableTokens() {
110        return new int[] {
111            TokenTypes.CLASS_DEF,
112            TokenTypes.INTERFACE_DEF,
113            TokenTypes.ENUM_DEF,
114            TokenTypes.ANNOTATION_DEF,
115            TokenTypes.RECORD_DEF,
116        };
117    }
118
119    @Override
120    public int[] getRequiredTokens() {
121        return CommonUtil.EMPTY_INT_ARRAY;
122    }
123
124}