001////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code for adherence to a set of rules.
003// Copyright (C) 2001-2021 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 pattern variable 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][a-zA-Z0-9]*$"}.
034 * </li>
035 * </ul>
036 * <p>
037 * To configure the check:
038 * </p>
039 * <pre>
040 * &lt;module name="PatternVariableName"/&gt;
041 * </pre>
042 * <p>Code Example:</p>
043 * <pre>
044 * class MyClass {
045 *     MyClass(Object o1){
046 *         if (o1 instanceof String STRING) { // violation, name 'STRING' must
047 *                                            // match pattern '^[a-z][a-zA-Z0-9]*$'
048 *         }
049 *         if (o1 instanceof Integer num) { // OK
050 *         }
051 *     }
052 * }
053 * </pre>
054 * <p>
055 * An example of how to configure the check for names that have a lower case letter, followed by
056 * letters and digits, optionally separated by underscore:
057 * </p>
058 * <pre>
059 * &lt;module name="PatternVariableName"&gt;
060 *   &lt;property name="format" value="^[a-z](_?[a-zA-Z0-9]+)*$"/&gt;
061 * &lt;/module&gt;
062 * </pre>
063 * <p>Code Example:</p>
064 * <pre>
065 * class MyClass {
066 *     MyClass(Object o1){
067 *         if (o1 instanceof String STR) { // violation, name 'STR' must
068 *                                         // match pattern '^[a-z](_?[a-zA-Z0-9]+)*$'
069 *         }
070 *         if (o1 instanceof Integer num) { // OK
071 *         }
072 *         if (o1 instanceof Integer num_1) { // OK
073 *         }
074 *     }
075 * }
076 * </pre>
077 * <p>
078 * An example of how to configure the check to that all variables have 3 or more chars in name:
079 * </p>
080 * <pre>
081 * &lt;module name="PatternVariableName"&gt;
082 *   &lt;property name="format" value="^[a-z][_a-zA-Z0-9]{2,}$"/&gt;
083 * &lt;/module&gt;
084 * </pre>
085 * <p>Code Example:</p>
086 * <pre>
087 * class MyClass {
088 *     MyClass(Object o1){
089 *         if (o1 instanceof String s) { // violation, name 's' must
090 *                                       // match pattern '^[a-z][_a-zA-Z0-9]{2,}$'
091 *         }
092 *         if (o1 instanceof Integer num) { // OK
093 *         }
094 *     }
095 * }
096 * </pre>
097 * <p>
098 * Parent is {@code com.puppycrawl.tools.checkstyle.TreeWalker}
099 * </p>
100 * <p>
101 * Violation Message Keys:
102 * </p>
103 * <ul>
104 * <li>
105 * {@code name.invalidPattern}
106 * </li>
107 * </ul>
108 *
109 * @since 8.36
110 */
111public class PatternVariableNameCheck extends AbstractNameCheck {
112
113    /** Creates a new {@code PatternVariableNameCheck} instance. */
114    public PatternVariableNameCheck() {
115        super("^[a-z][a-zA-Z0-9]*$");
116    }
117
118    @Override
119    public int[] getDefaultTokens() {
120        return getRequiredTokens();
121    }
122
123    @Override
124    public int[] getAcceptableTokens() {
125        return getRequiredTokens();
126    }
127
128    @Override
129    public int[] getRequiredTokens() {
130        return new int[] {
131            TokenTypes.PATTERN_VARIABLE_DEF,
132        };
133    }
134
135    @Override
136    protected final boolean mustCheckName(DetailAST ast) {
137        return true;
138    }
139}