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