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