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 method 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="MethodTypeParameterName"/> 039 * </pre> 040 * <p>Code Example:</p> 041 * <pre> 042 * class MyClass { 043 * public <T> void method1() {} // OK 044 * public <a> void method2() {} // violation, name 'a' must match pattern '^[A-Z]$' 045 * public <K, V> void method3() {} // OK 046 * public <k, V> void method4() {} // violation, name 'k' must match pattern '^[A-Z]$' 047 * } 048 * </pre> 049 * <p> 050 * An example of how to configure the check for names that are only a single letter is: 051 * </p> 052 * <pre> 053 * <module name="MethodTypeParameterName"> 054 * <property name="format" value="^[a-zA-Z]$"/> 055 * </module> 056 * </pre> 057 * <p>Code Example:</p> 058 * <pre> 059 * class MyClass { 060 * public <T> void method1() {} // OK 061 * public <a> void method2() {} // OK 062 * public <K, V> void method3() {} // OK 063 * public <k, V> void method4() {} // OK 064 * } 065 * </pre> 066 * 067 * @since 5.0 068 */ 069public class MethodTypeParameterNameCheck 070 extends AbstractNameCheck { 071 072 /** Creates a new {@code MethodTypeParameterNameCheck} instance. */ 073 public MethodTypeParameterNameCheck() { 074 super("^[A-Z]$"); 075 } 076 077 @Override 078 public int[] getDefaultTokens() { 079 return getRequiredTokens(); 080 } 081 082 @Override 083 public int[] getAcceptableTokens() { 084 return getRequiredTokens(); 085 } 086 087 @Override 088 public int[] getRequiredTokens() { 089 return new int[] { 090 TokenTypes.TYPE_PARAMETER, 091 }; 092 } 093 094 @Override 095 protected final boolean mustCheckName(DetailAST ast) { 096 final DetailAST location = 097 ast.getParent().getParent(); 098 return location.getType() == TokenTypes.METHOD_DEF; 099 } 100 101}