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 java.util.Objects; 023 024import com.puppycrawl.tools.checkstyle.api.DetailAST; 025import com.puppycrawl.tools.checkstyle.api.TokenTypes; 026import com.puppycrawl.tools.checkstyle.utils.TokenUtil; 027 028/** 029 * <p> 030 * Checks lambda parameter names. 031 * </p> 032 * <ul> 033 * <li> 034 * Property {@code format} - Specifies valid identifiers. 035 * Type is {@code java.util.regex.Pattern}. 036 * Default value is {@code "^[a-z][a-zA-Z0-9]*$"}. 037 * </li> 038 * </ul> 039 * <p> 040 * To configure the check: 041 * </p> 042 * <pre> 043 * <module name="LambdaParameterName"/> 044 * </pre> 045 * <p>Code Example:</p> 046 * <pre> 047 * Function<String, String> function1 = s -> s.toLowerCase(); // OK 048 * Function<String, String> function2 = S -> S.toLowerCase(); // violation, name 'S' 049 * // must match pattern '^[a-z][a-zA-Z0-9]*$' 050 * </pre> 051 * <p> 052 * An example of how to configure the check for names that begin 053 * with a lower case letter, followed by letters is: 054 * </p> 055 * <pre> 056 * <module name="LambdaParameterName"> 057 * <property name="format" value="^[a-z]([a-zA-Z]+)*$"/> 058 * </module> 059 * </pre> 060 * <p> 061 * Code Example: 062 * </p> 063 * <pre> 064 * class MyClass { 065 * Function<String, String> function1 = str -> str.toUpperCase().trim(); // OK 066 * Function<String, String> function2 = _s -> _s.trim(); // violation, name '_s' 067 * // must match pattern '^[a-z]([a-zA-Z]+)*$' 068 * 069 * public boolean myMethod(String sentence) { 070 * return Stream.of(sentence.split(" ")) 071 * .map(word -> word.trim()) // OK 072 * .anyMatch(Word -> "in".equals(Word)); // violation, name 'Word' 073 * // must match pattern '^[a-z]([a-zA-Z]+)*$' 074 * } 075 * } 076 * 077 * </pre> 078 * <p> 079 * Parent is {@code com.puppycrawl.tools.checkstyle.TreeWalker} 080 * </p> 081 * <p> 082 * Violation Message Keys: 083 * </p> 084 * <ul> 085 * <li> 086 * {@code name.invalidPattern} 087 * </li> 088 * </ul> 089 * 090 * @since 8.11 091 */ 092public class LambdaParameterNameCheck extends AbstractNameCheck { 093 094 /** Creates new instance of {@code LambdaParameterNameCheck}. */ 095 public LambdaParameterNameCheck() { 096 super("^[a-z][a-zA-Z0-9]*$"); 097 } 098 099 @Override 100 public int[] getDefaultTokens() { 101 return getRequiredTokens(); 102 } 103 104 @Override 105 public int[] getAcceptableTokens() { 106 return getRequiredTokens(); 107 } 108 109 @Override 110 public int[] getRequiredTokens() { 111 return new int[] { 112 TokenTypes.LAMBDA, 113 }; 114 } 115 116 @Override 117 public void visitToken(DetailAST ast) { 118 final boolean isInSwitchRule = ast.getParent().getType() == TokenTypes.SWITCH_RULE; 119 120 if (Objects.nonNull(ast.findFirstToken(TokenTypes.PARAMETERS))) { 121 final DetailAST parametersNode = ast.findFirstToken(TokenTypes.PARAMETERS); 122 TokenUtil.forEachChild(parametersNode, TokenTypes.PARAMETER_DEF, super::visitToken); 123 } 124 else if (!isInSwitchRule) { 125 super.visitToken(ast); 126 } 127 } 128 129 @Override 130 protected boolean mustCheckName(DetailAST ast) { 131 return true; 132 } 133 134}