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   * <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   * </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   *
75   * <p>
76   * Parent is {@code com.puppycrawl.tools.checkstyle.TreeWalker}
77   * </p>
78   *
79   * <p>
80   * Violation Message Keys:
81   * </p>
82   * <ul>
83   * <li>
84   * {@code name.invalidPattern}
85   * </li>
86   * </ul>
87   *
88   * @since 3.0
89   */
90  public class TypeNameCheck
91      extends AbstractAccessControlNameCheck {
92  
93      /**
94       * Default pattern for type name.
95       */
96      public static final String DEFAULT_PATTERN = "^[A-Z][a-zA-Z0-9]*$";
97  
98      /**
99       * Creates a new {@code TypeNameCheck} instance.
100      */
101     public TypeNameCheck() {
102         super(DEFAULT_PATTERN);
103     }
104 
105     @Override
106     public int[] getDefaultTokens() {
107         return getAcceptableTokens();
108     }
109 
110     @Override
111     public int[] getAcceptableTokens() {
112         return new int[] {
113             TokenTypes.CLASS_DEF,
114             TokenTypes.INTERFACE_DEF,
115             TokenTypes.ENUM_DEF,
116             TokenTypes.ANNOTATION_DEF,
117             TokenTypes.RECORD_DEF,
118         };
119     }
120 
121     @Override
122     public int[] getRequiredTokens() {
123         return CommonUtil.EMPTY_INT_ARRAY;
124     }
125 
126 }