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