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