View Javadoc
1   ///////////////////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3   // Copyright (C) 2001-2024 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   * <p>
27   * Checks that type names conform to a specified pattern.
28   * </p>
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   * </li>
36   * <li>
37   * Property {@code applyToPrivate} - Control if check should apply to private members.
38   * Type is {@code boolean}.
39   * Default value is {@code true}.
40   * </li>
41   * <li>
42   * Property {@code applyToProtected} - Control if check should apply to protected
43   *   members.
44   * Type is {@code boolean}.
45   * Default value is {@code true}.
46   * </li>
47   * <li>
48   * Property {@code applyToPublic} - Control if check should apply to public members.
49   * Type is {@code boolean}.
50   * Default value is {@code true}.
51   * </li>
52   * <li>
53   * Property {@code format} - Sets the pattern to match valid identifiers.
54   * Type is {@code java.util.regex.Pattern}.
55   * Default value is {@code "^[A-Z][a-zA-Z0-9]*$"}.
56   * </li>
57   * <li>
58   * Property {@code tokens} - tokens to check
59   * Type is {@code java.lang.String[]}.
60   * Validation type is {@code tokenSet}.
61   * Default value is:
62   * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#CLASS_DEF">
63   * CLASS_DEF</a>,
64   * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#INTERFACE_DEF">
65   * INTERFACE_DEF</a>,
66   * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#ENUM_DEF">
67   * ENUM_DEF</a>,
68   * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#ANNOTATION_DEF">
69   * ANNOTATION_DEF</a>,
70   * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#RECORD_DEF">
71   * RECORD_DEF</a>.
72   * </li>
73   * </ul>
74   * <p>
75   * Parent is {@code com.puppycrawl.tools.checkstyle.TreeWalker}
76   * </p>
77   * <p>
78   * Violation Message Keys:
79   * </p>
80   * <ul>
81   * <li>
82   * {@code name.invalidPattern}
83   * </li>
84   * </ul>
85   *
86   * @since 3.0
87   */
88  public class TypeNameCheck
89      extends AbstractAccessControlNameCheck {
90  
91      /**
92       * Default pattern for type name.
93       */
94      public static final String DEFAULT_PATTERN = "^[A-Z][a-zA-Z0-9]*$";
95  
96      /**
97       * Creates a new {@code TypeNameCheck} instance.
98       */
99      public TypeNameCheck() {
100         super(DEFAULT_PATTERN);
101     }
102 
103     @Override
104     public int[] getDefaultTokens() {
105         return getAcceptableTokens();
106     }
107 
108     @Override
109     public int[] getAcceptableTokens() {
110         return new int[] {
111             TokenTypes.CLASS_DEF,
112             TokenTypes.INTERFACE_DEF,
113             TokenTypes.ENUM_DEF,
114             TokenTypes.ANNOTATION_DEF,
115             TokenTypes.RECORD_DEF,
116         };
117     }
118 
119     @Override
120     public int[] getRequiredTokens() {
121         return CommonUtil.EMPTY_INT_ARRAY;
122     }
123 
124 }