View Javadoc
1   ///////////////////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3   // Copyright (C) 2001-2025 the original author or authors.
4   //
5   // This library is free software; you can redistribute it and/or
6   // modify it under the terms of the GNU Lesser General Public
7   // License as published by the Free Software Foundation; either
8   // version 2.1 of the License, or (at your option) any later version.
9   //
10  // This library is distributed in the hope that it will be useful,
11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  // Lesser General Public License for more details.
14  //
15  // You should have received a copy of the GNU Lesser General Public
16  // License along with this library; if not, write to the Free Software
17  // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  ///////////////////////////////////////////////////////////////////////////////////////////////
19  
20  package com.puppycrawl.tools.checkstyle.checks.naming;
21  
22  import com.puppycrawl.tools.checkstyle.api.TokenTypes;
23  import com.puppycrawl.tools.checkstyle.utils.CommonUtil;
24  
25  /**
26   * <div>
27   * Checks that type names conform to a specified pattern.
28   * </div>
29   * <ul>
30   * <li>
31   * Property {@code applyToPackage} - Control if check should apply to package-private
32   *   members.
33   * Type is {@code boolean}.
34   * Default value is {@code true}.
35   * Since version 5.0
36   * </li>
37   * <li>
38   * Property {@code applyToPrivate} - Control if check should apply to private members.
39   * Type is {@code boolean}.
40   * Default value is {@code true}.
41   * Since version 5.0
42   * </li>
43   * <li>
44   * Property {@code applyToProtected} - Control if check should apply to protected
45   *   members.
46   * Type is {@code boolean}.
47   * Default value is {@code true}.
48   * Since version 5.0
49   * </li>
50   * <li>
51   * Property {@code applyToPublic} - Control if check should apply to public members.
52   * Type is {@code boolean}.
53   * Default value is {@code true}.
54   * Since version 5.0
55   * </li>
56   * <li>
57   * Property {@code format} - Sets the pattern to match valid identifiers.
58   * Type is {@code java.util.regex.Pattern}.
59   * Default value is {@code "^[A-Z][a-zA-Z0-9]*$"}.
60   * </li>
61   * <li>
62   * Property {@code tokens} - tokens to check
63   * Type is {@code java.lang.String[]}.
64   * Validation type is {@code tokenSet}.
65   * Default value is:
66   * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#CLASS_DEF">
67   * CLASS_DEF</a>,
68   * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#INTERFACE_DEF">
69   * INTERFACE_DEF</a>,
70   * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#ENUM_DEF">
71   * ENUM_DEF</a>,
72   * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#ANNOTATION_DEF">
73   * ANNOTATION_DEF</a>,
74   * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#RECORD_DEF">
75   * RECORD_DEF</a>.
76   * </li>
77   * </ul>
78   *
79   * <p>
80   * Parent is {@code com.puppycrawl.tools.checkstyle.TreeWalker}
81   * </p>
82   *
83   * <p>
84   * Violation Message Keys:
85   * </p>
86   * <ul>
87   * <li>
88   * {@code name.invalidPattern}
89   * </li>
90   * </ul>
91   *
92   * @since 3.0
93   */
94  public class TypeNameCheck
95      extends AbstractAccessControlNameCheck {
96  
97      /**
98       * Default pattern for type name.
99       */
100     public static final String DEFAULT_PATTERN = "^[A-Z][a-zA-Z0-9]*$";
101 
102     /**
103      * Creates a new {@code TypeNameCheck} instance.
104      */
105     public TypeNameCheck() {
106         super(DEFAULT_PATTERN);
107     }
108 
109     @Override
110     public int[] getDefaultTokens() {
111         return getAcceptableTokens();
112     }
113 
114     @Override
115     public int[] getAcceptableTokens() {
116         return new int[] {
117             TokenTypes.CLASS_DEF,
118             TokenTypes.INTERFACE_DEF,
119             TokenTypes.ENUM_DEF,
120             TokenTypes.ANNOTATION_DEF,
121             TokenTypes.RECORD_DEF,
122         };
123     }
124 
125     @Override
126     public int[] getRequiredTokens() {
127         return CommonUtil.EMPTY_INT_ARRAY;
128     }
129 
130 }