001/////////////////////////////////////////////////////////////////////////////////////////////// 002// checkstyle: Checks Java source code and other text files for adherence to a set of rules. 003// Copyright (C) 2001-2024 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.sizes; 021 022import com.puppycrawl.tools.checkstyle.StatelessCheck; 023import com.puppycrawl.tools.checkstyle.api.AbstractCheck; 024import com.puppycrawl.tools.checkstyle.api.DetailAST; 025import com.puppycrawl.tools.checkstyle.api.TokenTypes; 026 027/** 028 * <p> 029 * Checks lambda body length. 030 * </p> 031 * <p> 032 * Rationale: Similar to anonymous inner classes, if lambda body becomes very long 033 * it is hard to understand and to see the flow of the method 034 * where the lambda is defined. Therefore, long lambda body 035 * should usually be extracted to method. 036 * </p> 037 * <ul> 038 * <li> 039 * Property {@code max} - Specify the maximum number of lines allowed. 040 * Type is {@code int}. 041 * Default value is {@code 10}. 042 * </li> 043 * </ul> 044 * <p> 045 * Parent is {@code com.puppycrawl.tools.checkstyle.TreeWalker} 046 * </p> 047 * <p> 048 * Violation Message Keys: 049 * </p> 050 * <ul> 051 * <li> 052 * {@code maxLen.lambdaBody} 053 * </li> 054 * </ul> 055 * 056 * @since 8.37 057 */ 058@StatelessCheck 059public class LambdaBodyLengthCheck extends AbstractCheck { 060 061 /** 062 * A key is pointing to the warning message text in "messages.properties" 063 * file. 064 */ 065 public static final String MSG_KEY = "maxLen.lambdaBody"; 066 067 /** Default maximum number of lines. */ 068 private static final int DEFAULT_MAX = 10; 069 070 /** Specify the maximum number of lines allowed. */ 071 private int max = DEFAULT_MAX; 072 073 /** 074 * Setter to specify the maximum number of lines allowed. 075 * 076 * @param length the maximum length of lambda body. 077 * @since 8.37 078 */ 079 public void setMax(int length) { 080 max = length; 081 } 082 083 @Override 084 public int[] getDefaultTokens() { 085 return getRequiredTokens(); 086 } 087 088 @Override 089 public int[] getAcceptableTokens() { 090 return getRequiredTokens(); 091 } 092 093 @Override 094 public int[] getRequiredTokens() { 095 return new int[] {TokenTypes.LAMBDA}; 096 } 097 098 @Override 099 public void visitToken(DetailAST ast) { 100 if (ast.getParent().getType() != TokenTypes.SWITCH_RULE) { 101 final int length = getLength(ast); 102 if (length > max) { 103 log(ast, MSG_KEY, length, max); 104 } 105 } 106 } 107 108 /** 109 * Get length of lambda body. 110 * 111 * @param ast lambda body node. 112 * @return length of lambda body. 113 */ 114 private static int getLength(DetailAST ast) { 115 final DetailAST lambdaBody = ast.getLastChild(); 116 final int length; 117 if (lambdaBody.getType() == TokenTypes.SLIST) { 118 length = lambdaBody.getLastChild().getLineNo() - lambdaBody.getLineNo(); 119 } 120 else { 121 length = getLastNodeLineNumber(lambdaBody) - getFirstNodeLineNumber(lambdaBody); 122 } 123 return length + 1; 124 } 125 126 /** 127 * Get last child node in the tree line number. 128 * 129 * @param lambdaBody lambda body node. 130 * @return last child node in the tree line number. 131 */ 132 private static int getLastNodeLineNumber(DetailAST lambdaBody) { 133 DetailAST node = lambdaBody; 134 int result; 135 do { 136 result = node.getLineNo(); 137 node = node.getLastChild(); 138 } while (node != null); 139 return result; 140 } 141 142 /** 143 * Get first child node in the tree line number. 144 * 145 * @param lambdaBody lambda body node. 146 * @return first child node in the tree line number. 147 */ 148 private static int getFirstNodeLineNumber(DetailAST lambdaBody) { 149 DetailAST node = lambdaBody; 150 int result; 151 do { 152 result = node.getLineNo(); 153 node = node.getFirstChild(); 154 } while (node != null); 155 return result; 156 } 157 158}