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.DetailAST; 023import com.puppycrawl.tools.checkstyle.api.TokenTypes; 024 025/** 026 * <p> 027 * Checks that class type parameter names conform to a specified pattern. 028 * </p> 029 * <ul> 030 * <li> 031 * Property {@code format} - Specifies valid identifiers. Default value is {@code "^[A-Z]$"}. 032 * </li> 033 * </ul> 034 * <p> 035 * An example of how to configure the check is: 036 * </p> 037 * <pre> 038 * <module name="ClassTypeParameterName"/> 039 * </pre> 040 * <p> 041 * An example of how to configure the check for names that are only a single 042 * letter is: 043 * </p> 044 * <p>Configuration:</p> 045 * <pre> 046 * <module name="ClassTypeParameterName"> 047 * <property name="format" value="^[a-zA-Z]$"/> 048 * </module> 049 * </pre> 050 * <p>Example:</p> 051 * <pre> 052 * class MyClass1<T> {} // OK 053 * class MyClass2<t> {} // OK 054 * class MyClass3<abc> {} // violation, the class type parameter 055 * // name should match the regular expression "^[a-zA-Z]$" 056 * </pre> 057 * @since 5.0 058 */ 059public class ClassTypeParameterNameCheck 060 extends AbstractNameCheck { 061 062 /** Creates a new {@code ClassTypeParameterNameCheck} instance. */ 063 public ClassTypeParameterNameCheck() { 064 super("^[A-Z]$"); 065 } 066 067 @Override 068 public int[] getDefaultTokens() { 069 return getRequiredTokens(); 070 } 071 072 @Override 073 public final int[] getAcceptableTokens() { 074 return getRequiredTokens(); 075 } 076 077 @Override 078 public int[] getRequiredTokens() { 079 return new int[] { 080 TokenTypes.TYPE_PARAMETER, 081 }; 082 } 083 084 @Override 085 protected final boolean mustCheckName(DetailAST ast) { 086 final DetailAST location = 087 ast.getParent().getParent(); 088 return location.getType() == TokenTypes.CLASS_DEF; 089 } 090 091}