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 lambda parameter names.
028 * </p>
029 * <ul>
030 * <li>
031 * Property {@code format} - Specifies valid identifiers.
032 * Default value is {@code "^[a-z][a-zA-Z0-9]*$"}.
033 * </li>
034 * </ul>
035 * <p>
036 * An example of how to configure the check is:
037 * </p>
038 * <pre>
039 * &lt;module name="LambdaParameterName"/&gt;
040 * </pre>
041 * <p>
042 * An example of how to configure the check for names that begin
043 * with a lower case letter, followed by letters is:
044 * </p>
045 * <pre>
046 * &lt;module name="LambdaParameterName"&gt;
047 *   &lt;property name="format" value="^[a-z]([a-zA-Z]+)*$"/&gt;
048 * &lt;/module&gt;
049 * </pre>
050 * <p>
051 * Example of checking with this config:
052 * </p>
053 * <pre>
054 * public class TestClass {
055 *
056 *     Function&lt;String, String&gt; function1 = str -&gt; str.toUpperCase().trim();
057 *
058 *     Function&lt;String, String&gt; function2 = _s -&gt; _s.trim().toUpperCase(); // violation
059 *
060 *     public boolean testMethod(String sentence) {
061 *         return Stream.of(sentence.split(" "))
062 *             .map(word -&gt; word.trim())
063 *             .anyMatch(Word -&gt; "in".equals(Word)); // violation
064 *     }
065 *
066 * }
067 *
068 * </pre>
069 *
070 * @since 8.11
071 */
072public class LambdaParameterNameCheck extends AbstractNameCheck {
073
074    /** Creates new instance of {@code LambdaParameterNameCheck}. */
075    public LambdaParameterNameCheck() {
076        super("^[a-z][a-zA-Z0-9]*$");
077    }
078
079    @Override
080    public int[] getDefaultTokens() {
081        return getRequiredTokens();
082    }
083
084    @Override
085    public int[] getAcceptableTokens() {
086        return getRequiredTokens();
087    }
088
089    @Override
090    public int[] getRequiredTokens() {
091        return new int[] {
092            TokenTypes.LAMBDA,
093        };
094    }
095
096    @Override
097    public void visitToken(DetailAST ast) {
098        final DetailAST parametersNode = ast.findFirstToken(TokenTypes.PARAMETERS);
099        if (parametersNode == null) {
100            super.visitToken(ast);
101        }
102        else {
103            for (DetailAST parameterDef = parametersNode.getFirstChild();
104                 parameterDef != null;
105                 parameterDef = parameterDef.getNextSibling()) {
106                if (parameterDef.getType() == TokenTypes.PARAMETER_DEF) {
107                    super.visitToken(parameterDef);
108                }
109            }
110        }
111    }
112
113    @Override
114    protected boolean mustCheckName(DetailAST ast) {
115        return true;
116    }
117
118}