001////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code for adherence to a set of rules.
003// Copyright (C) 2001-2022 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 record 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="RecordTypeParameterName"/&gt;
041 * </pre>
042 * <p>
043 * An example of how to configure the check for names that are only a single
044 * letter is:
045 * </p>
046 * <p>Configuration:</p>
047 * <pre>
048 * &lt;module name="RecordTypeParameterName"&gt;
049 *   &lt;property name="format" value="^[a-zA-Z]$"/&gt;
050 * &lt;/module&gt;
051 * </pre>
052 * <p>Example:</p>
053 * <pre>
054 * record MyRecord1&lt;T&gt; {} // OK
055 * record MyRecord2&lt;t&gt; {} // OK
056 * record MyRecord3&lt;abc&gt; {} // violation, the record type parameter
057 *                              // name should match the regular expression "^[a-zA-Z]$"
058 * </pre>
059 * <p>
060 * Parent is {@code com.puppycrawl.tools.checkstyle.TreeWalker}
061 * </p>
062 * <p>
063 * Violation Message Keys:
064 * </p>
065 * <ul>
066 * <li>
067 * {@code name.invalidPattern}
068 * </li>
069 * </ul>
070 *
071 * @since 8.36
072 */
073public class RecordTypeParameterNameCheck extends AbstractNameCheck {
074
075    /** Creates a new {@code RecordTypeParameterNameCheck} instance. */
076    public RecordTypeParameterNameCheck() {
077        super("^[A-Z]$");
078    }
079
080    @Override
081    public int[] getDefaultTokens() {
082        return getRequiredTokens();
083    }
084
085    @Override
086    public final int[] getAcceptableTokens() {
087        return getRequiredTokens();
088    }
089
090    @Override
091    public int[] getRequiredTokens() {
092        return new int[] {
093            TokenTypes.TYPE_PARAMETER,
094        };
095    }
096
097    @Override
098    protected final boolean mustCheckName(DetailAST ast) {
099        final DetailAST location =
100                ast.getParent().getParent();
101        return location.getType() == TokenTypes.RECORD_DEF;
102    }
103
104}