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