001////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code for adherence to a set of rules.
003// Copyright (C) 2001-2019 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 for classes, interfaces, enums, and annotations conform to a format
028 * specified by the format property.
029 * </p>
030 * <ul>
031 * <li>
032 * Property {@code format} - Specifies valid identifiers. Default value is
033 * {@code "^[A-Z][a-zA-Z0-9]*$"}.
034 * </li>
035 * <li>
036 * Property {@code applyToPublic} - Controls whether to apply the check to public member.
037 * Default value is {@code true}.
038 * </li>
039 * <li>
040 * Property {@code applyToProtected} - Controls whether to apply the check to protected member.
041 * Default value is {@code true}.
042 * </li>
043 * <li>
044 * Property {@code applyToPackage} - Controls whether to apply the check to package-private member.
045 * Default value is {@code true}.
046 * </li>
047 * <li>
048 * Property {@code applyToPrivate} - Controls whether to apply the check to private member.
049 * Default value is {@code true}.
050 * </li>
051 * <li>
052 * Property {@code tokens} - tokens to check Default value is:
053 * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#CLASS_DEF">
054 * CLASS_DEF</a>,
055 * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#INTERFACE_DEF">
056 * INTERFACE_DEF</a>,
057 * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#ENUM_DEF">
058 * ENUM_DEF</a>,
059 * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#ANNOTATION_DEF">
060 * ANNOTATION_DEF</a>.
061 * </li>
062 * </ul>
063 * <p>
064 * An example of how to configure the check is:
065 * </p>
066 * <pre>
067 * &lt;module name="TypeName"/&gt;
068 * </pre>
069 * <p>
070 * An example of how to configure the check for names that begin with
071 * a lower case letter, followed by letters, digits, and underscores is:
072 * </p>
073 * <pre>
074 * &lt;module name="TypeName"&gt;
075 *   &lt;property name="format" value="^[a-z](_?[a-zA-Z0-9]+)*$"/&gt;
076 * &lt;/module&gt;
077 * </pre>
078 * <p>
079 * The following configuration element ensures that interface names begin with {@code "I_"},
080 * followed by letters and digits:
081 * </p>
082 * <pre>
083 * &lt;module name="TypeName"&gt;
084 *   &lt;property name="format"
085 *     value="^I_[a-zA-Z0-9]*$"/&gt;
086 *   &lt;property name="tokens"
087 *     value="INTERFACE_DEF"/&gt;
088 * &lt;/module&gt;
089 * </pre>
090 *
091 * @since 3.0
092 */
093public class TypeNameCheck
094    extends AbstractAccessControlNameCheck {
095
096    /**
097     * Default pattern for type name.
098     */
099    public static final String DEFAULT_PATTERN = "^[A-Z][a-zA-Z0-9]*$";
100
101    /**
102     * Creates a new {@code TypeNameCheck} instance.
103     */
104    public TypeNameCheck() {
105        super(DEFAULT_PATTERN);
106    }
107
108    @Override
109    public int[] getDefaultTokens() {
110        return getAcceptableTokens();
111    }
112
113    @Override
114    public int[] getAcceptableTokens() {
115        return new int[] {TokenTypes.CLASS_DEF,
116                          TokenTypes.INTERFACE_DEF,
117                          TokenTypes.ENUM_DEF,
118                          TokenTypes.ANNOTATION_DEF,
119        };
120    }
121
122    @Override
123    public int[] getRequiredTokens() {
124        return CommonUtil.EMPTY_INT_ARRAY;
125    }
126
127}