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. Default value is 032 * {@code "^[A-Z][a-zA-Z0-9]*$"}. 033 * </li> 034 * <li> 035 * Property {@code applyToPublic} - Controls whether to apply the check to public member. 036 * Default value is {@code true}. 037 * </li> 038 * <li> 039 * Property {@code applyToProtected} - Controls whether to apply the check to protected member. 040 * Default value is {@code true}. 041 * </li> 042 * <li> 043 * Property {@code applyToPackage} - Controls whether to apply the check to package-private member. 044 * Default value is {@code true}. 045 * </li> 046 * <li> 047 * Property {@code applyToPrivate} - Controls whether to apply the check to private member. 048 * Default value is {@code true}. 049 * </li> 050 * <li> 051 * Property {@code tokens} - tokens to check Default value is: 052 * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#CLASS_DEF"> 053 * CLASS_DEF</a>, 054 * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#INTERFACE_DEF"> 055 * INTERFACE_DEF</a>, 056 * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#ENUM_DEF"> 057 * ENUM_DEF</a>, 058 * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#ANNOTATION_DEF"> 059 * ANNOTATION_DEF</a>. 060 * </li> 061 * </ul> 062 * <p> 063 * An example of how to configure the check is: 064 * </p> 065 * <pre> 066 * <module name="TypeName"/> 067 * </pre> 068 * <p> 069 * An example of how to configure the check for names that begin with 070 * a lower case letter, followed by letters, digits, and underscores is: 071 * </p> 072 * <pre> 073 * <module name="TypeName"> 074 * <property name="format" value="^[a-z](_?[a-zA-Z0-9]+)*$"/> 075 * </module> 076 * </pre> 077 * <p> 078 * The following configuration element ensures that interface names begin with {@code "I_"}, 079 * followed by letters and digits: 080 * </p> 081 * <pre> 082 * <module name="TypeName"> 083 * <property name="format" 084 * value="^I_[a-zA-Z0-9]*$"/> 085 * <property name="tokens" 086 * value="INTERFACE_DEF"/> 087 * </module> 088 * </pre> 089 * 090 * @since 3.0 091 */ 092public class TypeNameCheck 093 extends AbstractAccessControlNameCheck { 094 095 /** 096 * Default pattern for type name. 097 */ 098 public static final String DEFAULT_PATTERN = "^[A-Z][a-zA-Z0-9]*$"; 099 100 /** 101 * Creates a new {@code TypeNameCheck} instance. 102 */ 103 public TypeNameCheck() { 104 super(DEFAULT_PATTERN); 105 } 106 107 @Override 108 public int[] getDefaultTokens() { 109 return getAcceptableTokens(); 110 } 111 112 @Override 113 public int[] getAcceptableTokens() { 114 return new int[] {TokenTypes.CLASS_DEF, 115 TokenTypes.INTERFACE_DEF, 116 TokenTypes.ENUM_DEF, 117 TokenTypes.ANNOTATION_DEF, 118 }; 119 } 120 121 @Override 122 public int[] getRequiredTokens() { 123 return CommonUtil.EMPTY_INT_ARRAY; 124 } 125 126}