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