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 * <div>
027 * Checks that type names conform to a specified pattern.
028 * </div>
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 *
075 * <p>
076 * Parent is {@code com.puppycrawl.tools.checkstyle.TreeWalker}
077 * </p>
078 *
079 * <p>
080 * Violation Message Keys:
081 * </p>
082 * <ul>
083 * <li>
084 * {@code name.invalidPattern}
085 * </li>
086 * </ul>
087 *
088 * @since 3.0
089 */
090public class TypeNameCheck
091    extends AbstractAccessControlNameCheck {
092
093    /**
094     * Default pattern for type name.
095     */
096    public static final String DEFAULT_PATTERN = "^[A-Z][a-zA-Z0-9]*$";
097
098    /**
099     * Creates a new {@code TypeNameCheck} instance.
100     */
101    public TypeNameCheck() {
102        super(DEFAULT_PATTERN);
103    }
104
105    @Override
106    public int[] getDefaultTokens() {
107        return getAcceptableTokens();
108    }
109
110    @Override
111    public int[] getAcceptableTokens() {
112        return new int[] {
113            TokenTypes.CLASS_DEF,
114            TokenTypes.INTERFACE_DEF,
115            TokenTypes.ENUM_DEF,
116            TokenTypes.ANNOTATION_DEF,
117            TokenTypes.RECORD_DEF,
118        };
119    }
120
121    @Override
122    public int[] getRequiredTokens() {
123        return CommonUtil.EMPTY_INT_ARRAY;
124    }
125
126}