1 ///////////////////////////////////////////////////////////////////////////////////////////////
2 // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3 // Copyright (C) 2001-2026 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 *
30 * @since 3.0
31 */
32 public class TypeNameCheck
33 extends AbstractAccessControlNameCheck {
34
35 /**
36 * A key is pointing to the warning message text in "messages.properties"
37 * file.
38 */
39 public static final String MSG_INVALID_PATTERN = "name.invalidPattern";
40
41 /**
42 * Default pattern for type name.
43 */
44 public static final String DEFAULT_PATTERN = "^[A-Z][a-zA-Z0-9]*$";
45
46 /**
47 * Creates a new {@code TypeNameCheck} instance.
48 */
49 public TypeNameCheck() {
50 super(DEFAULT_PATTERN, MSG_INVALID_PATTERN);
51 }
52
53 @Override
54 public int[] getDefaultTokens() {
55 return getAcceptableTokens();
56 }
57
58 @Override
59 public int[] getAcceptableTokens() {
60 return new int[] {
61 TokenTypes.CLASS_DEF,
62 TokenTypes.INTERFACE_DEF,
63 TokenTypes.ENUM_DEF,
64 TokenTypes.ANNOTATION_DEF,
65 TokenTypes.RECORD_DEF,
66 };
67 }
68
69 @Override
70 public int[] getRequiredTokens() {
71 return CommonUtil.EMPTY_INT_ARRAY;
72 }
73
74 /**
75 * Setter to control if check should apply to package-private members.
76 *
77 * @param applyTo new value of the property.
78 * @propertySince 5.0
79 */
80 @Override
81 public final void setApplyToPackage(boolean applyTo) {
82 super.setApplyToPackage(applyTo);
83 }
84
85 /**
86 * Setter to control if check should apply to private members.
87 *
88 * @param applyTo new value of the property.
89 * @propertySince 5.0
90 */
91 @Override
92 public final void setApplyToPrivate(boolean applyTo) {
93 super.setApplyToPrivate(applyTo);
94 }
95
96 /**
97 * Setter to control if check should apply to protected members.
98 *
99 * @param applyTo new value of the property.
100 * @propertySince 5.0
101 */
102 @Override
103 public final void setApplyToProtected(boolean applyTo) {
104 super.setApplyToProtected(applyTo);
105 }
106
107 /**
108 * Setter to control if check should apply to public members.
109 *
110 * @param applyTo new value of the property.
111 * @propertySince 5.0
112 */
113 @Override
114 public final void setApplyToPublic(boolean applyTo) {
115 super.setApplyToPublic(applyTo);
116 }
117
118 }