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 class 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 * &lt;module name="ClassTypeParameterName"/&gt;
041 * </pre>
042 * <p>Example:</p>
043 * <pre>
044 * class MyClass1&lt;T&gt; {}        // OK
045 * class MyClass2&lt;t&gt; {}        // violation
046 * class MyClass3&lt;abc&gt; {}      // violation
047 * class MyClass4&lt;LISTENER&gt; {} // violation
048 * class MyClass5&lt;RequestT&gt; {} // violation
049 * </pre>
050 * <p>
051 * To configure the check for names that are uppercase word:
052 * </p>
053 * <pre>
054 * &lt;module name="ClassTypeParameterName"&gt;
055 *   &lt;property name="format" value="^[A-Z]{2,}$"/&gt;
056 * &lt;/module&gt;
057 * </pre>
058 * <p>Example:</p>
059 * <pre>
060 * class MyClass1&lt;T&gt; {}        // violation
061 * class MyClass2&lt;t&gt; {}        // violation
062 * class MyClass3&lt;abc&gt; {}      // violation
063 * class MyClass4&lt;LISTENER&gt; {} // OK
064 * class MyClass5&lt;RequestT&gt; {} // violation
065 * </pre>
066 * <p>
067 * To configure the check for names that are camel case word with T as suffix (
068 * <a href="https://checkstyle.org/styleguides/google-java-style-20180523/javaguide.html#s5.2.8-type-variable-names">
069 * Google Style</a>):
070 * </p>
071 * <pre>
072 * &lt;module name="ClassTypeParameterName"&gt;
073 *   &lt;property name="format" value="(^[A-Z][0-9]?)$|([A-Z][a-zA-Z0-9]*[T]$)"/&gt;
074 * &lt;/module&gt;
075 * </pre>
076 * <p>Example:</p>
077 * <pre>
078 * class MyClass1&lt;T&gt; {}        // violation
079 * class MyClass2&lt;t&gt; {}        // violation
080 * class MyClass3&lt;abc&gt; {}      // violation
081 * class MyClass4&lt;LISTENER&gt; {} // violation
082 * class MyClass5&lt;RequestT&gt; {} // OK
083 * </pre>
084 * <p>
085 * Parent is {@code com.puppycrawl.tools.checkstyle.TreeWalker}
086 * </p>
087 * <p>
088 * Violation Message Keys:
089 * </p>
090 * <ul>
091 * <li>
092 * {@code name.invalidPattern}
093 * </li>
094 * </ul>
095 *
096 * @since 5.0
097 */
098public class ClassTypeParameterNameCheck
099    extends AbstractNameCheck {
100
101    /** Creates a new {@code ClassTypeParameterNameCheck} instance. */
102    public ClassTypeParameterNameCheck() {
103        super("^[A-Z]$");
104    }
105
106    @Override
107    public int[] getDefaultTokens() {
108        return getRequiredTokens();
109    }
110
111    @Override
112    public final int[] getAcceptableTokens() {
113        return getRequiredTokens();
114    }
115
116    @Override
117    public int[] getRequiredTokens() {
118        return new int[] {
119            TokenTypes.TYPE_PARAMETER,
120        };
121    }
122
123    @Override
124    protected final boolean mustCheckName(DetailAST ast) {
125        final DetailAST location =
126            ast.getParent().getParent();
127        return location.getType() == TokenTypes.CLASS_DEF;
128    }
129
130}