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. 032 * Type is {@code java.util.regex.Pattern}. 033 * Default value is {@code "^[A-Z]$"}. 034 * </li> 035 * </ul> 036 * <p> 037 * To configure the check: 038 * </p> 039 * <pre> 040 * <module name="ClassTypeParameterName"/> 041 * </pre> 042 * <p>Example:</p> 043 * <pre> 044 * class MyClass1<T> {} // OK 045 * class MyClass2<t> {} // violation 046 * class MyClass3<abc> {} // violation 047 * class MyClass4<LISTENER> {} // violation 048 * class MyClass5<RequestT> {} // violation 049 * </pre> 050 * <p> 051 * To configure the check for names that are uppercase word: 052 * </p> 053 * <pre> 054 * <module name="ClassTypeParameterName"> 055 * <property name="format" value="^[A-Z]{2,}$"/> 056 * </module> 057 * </pre> 058 * <p>Example:</p> 059 * <pre> 060 * class MyClass1<T> {} // violation 061 * class MyClass2<t> {} // violation 062 * class MyClass3<abc> {} // violation 063 * class MyClass4<LISTENER> {} // OK 064 * class MyClass5<RequestT> {} // violation 065 * </pre> 066 * <p> 067 * To configure the check for names that are camel case word with T as suffix ( 068 * <a href="https://checkstyle.org/styleguides/google-java-style-20180523/javaguide.html#s5.2.8-type-variable-names"> 069 * Google Style</a>): 070 * </p> 071 * <pre> 072 * <module name="ClassTypeParameterName"> 073 * <property name="format" value="(^[A-Z][0-9]?)$|([A-Z][a-zA-Z0-9]*[T]$)"/> 074 * </module> 075 * </pre> 076 * <p>Example:</p> 077 * <pre> 078 * class MyClass1<T> {} // violation 079 * class MyClass2<t> {} // violation 080 * class MyClass3<abc> {} // violation 081 * class MyClass4<LISTENER> {} // violation 082 * class MyClass5<RequestT> {} // OK 083 * </pre> 084 * <p> 085 * Parent is {@code com.puppycrawl.tools.checkstyle.TreeWalker} 086 * </p> 087 * <p> 088 * Violation Message Keys: 089 * </p> 090 * <ul> 091 * <li> 092 * {@code name.invalidPattern} 093 * </li> 094 * </ul> 095 * 096 * @since 5.0 097 */ 098public class ClassTypeParameterNameCheck 099 extends AbstractNameCheck { 100 101 /** Creates a new {@code ClassTypeParameterNameCheck} instance. */ 102 public ClassTypeParameterNameCheck() { 103 super("^[A-Z]$"); 104 } 105 106 @Override 107 public int[] getDefaultTokens() { 108 return getRequiredTokens(); 109 } 110 111 @Override 112 public final int[] getAcceptableTokens() { 113 return getRequiredTokens(); 114 } 115 116 @Override 117 public int[] getRequiredTokens() { 118 return new int[] { 119 TokenTypes.TYPE_PARAMETER, 120 }; 121 } 122 123 @Override 124 protected final boolean mustCheckName(DetailAST ast) { 125 final DetailAST location = 126 ast.getParent().getParent(); 127 return location.getType() == TokenTypes.CLASS_DEF; 128 } 129 130}