001////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code for adherence to a set of rules.
003// Copyright (C) 2001-2020 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 format} - Specifies valid identifiers.
032 * Type is {@code java.util.regex.Pattern}.
033 * Default value is {@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 * 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 applyToPackage} - Controls whether to apply the check to package-private member.
047 * Type is {@code boolean}.
048 * Default value is {@code true}.
049 * </li>
050 * <li>
051 * Property {@code applyToPrivate} - Controls whether to apply the check to private member.
052 * Type is {@code boolean}.
053 * Default value is {@code true}.
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 * To configure the check:
074 * </p>
075 * <pre>
076 * &lt;module name=&quot;TypeName&quot;/&gt;
077 * </pre>
078 * <p>Code Example:</p>
079 * <pre>
080 * public interface FirstName {} // OK
081 * protected class SecondName {} // OK
082 * enum Third_Name {} // violation, name 'Third_Name' must match pattern '^[A-Z][a-zA-Z0-9]*$'
083 * private class FourthName_ {} // violation, name 'FourthName_'
084 *                              // must match pattern '^[A-Z][a-zA-Z0-9]*$'
085 * </pre>
086 * <p>
087 * An example of how to configure the check for names that begin with
088 * a lower case letter, followed by letters, digits, and underscores.
089 * Also, suppress the check from being applied to protected and private type:
090 * </p>
091 * <pre>
092 * &lt;module name=&quot;TypeName&quot;&gt;
093 *   &lt;property name=&quot;format&quot; value=&quot;^[a-z](_?[a-zA-Z0-9]+)*$&quot;/&gt;
094 *   &lt;property name=&quot;applyToProtected&quot; value=&quot;false&quot;/&gt;
095 *   &lt;property name=&quot;applyToPrivate&quot; value=&quot;false&quot;/&gt;
096 * &lt;/module&gt;
097 * </pre>
098 * <p>Code Example:</p>
099 * <pre>
100 * public interface firstName {} // OK
101 * public class SecondName {} // violation, name 'SecondName'
102 *                            // must match pattern '^[a-z](_?[a-zA-Z0-9]+)*$'
103 * protected class ThirdName {} // OK
104 * private class FourthName {} // OK
105 * </pre>
106 * <p>
107 * The following configuration element ensures that interface names begin with {@code "I_"},
108 * followed by letters and digits:
109 * </p>
110 * <pre>
111 * &lt;module name=&quot;TypeName&quot;&gt;
112 *   &lt;property name=&quot;format&quot;
113 *     value=&quot;^I_[a-zA-Z0-9]*$&quot;/&gt;
114 *   &lt;property name=&quot;tokens&quot;
115 *     value=&quot;INTERFACE_DEF&quot;/&gt;
116 * &lt;/module&gt;
117 * </pre>
118 * <p>Code Example:</p>
119 * <pre>
120 * public interface I_firstName {} // OK
121 * interface SecondName {} // violation, name 'SecondName'
122 *                         // must match pattern '^I_[a-zA-Z0-9]*$'
123 * </pre>
124 * <p>
125 * Parent is {@code com.puppycrawl.tools.checkstyle.TreeWalker}
126 * </p>
127 * <p>
128 * Violation Message Keys:
129 * </p>
130 * <ul>
131 * <li>
132 * {@code name.invalidPattern}
133 * </li>
134 * </ul>
135 *
136 * @since 3.0
137 */
138public class TypeNameCheck
139    extends AbstractAccessControlNameCheck {
140
141    /**
142     * Default pattern for type name.
143     */
144    public static final String DEFAULT_PATTERN = "^[A-Z][a-zA-Z0-9]*$";
145
146    /**
147     * Creates a new {@code TypeNameCheck} instance.
148     */
149    public TypeNameCheck() {
150        super(DEFAULT_PATTERN);
151    }
152
153    @Override
154    public int[] getDefaultTokens() {
155        return getAcceptableTokens();
156    }
157
158    @Override
159    public int[] getAcceptableTokens() {
160        return new int[] {
161            TokenTypes.CLASS_DEF,
162            TokenTypes.INTERFACE_DEF,
163            TokenTypes.ENUM_DEF,
164            TokenTypes.ANNOTATION_DEF,
165            TokenTypes.RECORD_DEF,
166        };
167    }
168
169    @Override
170    public int[] getRequiredTokens() {
171        return CommonUtil.EMPTY_INT_ARRAY;
172    }
173
174}