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