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