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