1 ///////////////////////////////////////////////////////////////////////////////////////////////
2 // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3 // Copyright (C) 2001-2025 the original author or authors.
4 //
5 // This library is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU Lesser General Public
7 // License as published by the Free Software Foundation; either
8 // version 2.1 of the License, or (at your option) any later version.
9 //
10 // This library is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // Lesser General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public
16 // License along with this library; if not, write to the Free Software
17 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 ///////////////////////////////////////////////////////////////////////////////////////////////
19
20 package com.puppycrawl.tools.checkstyle.checks.sizes;
21
22 import java.util.ArrayDeque;
23 import java.util.BitSet;
24 import java.util.Deque;
25 import java.util.Objects;
26 import java.util.stream.Stream;
27
28 import com.puppycrawl.tools.checkstyle.StatelessCheck;
29 import com.puppycrawl.tools.checkstyle.api.AbstractCheck;
30 import com.puppycrawl.tools.checkstyle.api.DetailAST;
31 import com.puppycrawl.tools.checkstyle.api.TokenTypes;
32 import com.puppycrawl.tools.checkstyle.utils.CommonUtil;
33
34 /**
35 * <div>
36 * Checks for long methods and constructors.
37 * </div>
38 *
39 * <p>
40 * Rationale: If a method becomes very long it is hard to understand.
41 * Therefore, long methods should usually be refactored into several
42 * individual methods that focus on a specific task.
43 * </p>
44 *
45 * @since 3.0
46 */
47 @StatelessCheck
48 public class MethodLengthCheck extends AbstractCheck {
49
50 /**
51 * A key is pointing to the warning message text in "messages.properties"
52 * file.
53 */
54 public static final String MSG_KEY = "maxLen.method";
55
56 /** Default maximum number of lines. */
57 private static final int DEFAULT_MAX_LINES = 150;
58
59 /** Control whether to count empty lines and comments. */
60 private boolean countEmpty = true;
61
62 /** Specify the maximum number of lines allowed. */
63 private int max = DEFAULT_MAX_LINES;
64
65 @Override
66 public int[] getDefaultTokens() {
67 return getAcceptableTokens();
68 }
69
70 @Override
71 public int[] getAcceptableTokens() {
72 return new int[] {
73 TokenTypes.METHOD_DEF,
74 TokenTypes.CTOR_DEF,
75 TokenTypes.COMPACT_CTOR_DEF,
76 };
77 }
78
79 @Override
80 public int[] getRequiredTokens() {
81 return CommonUtil.EMPTY_INT_ARRAY;
82 }
83
84 @Override
85 public void visitToken(DetailAST ast) {
86 final DetailAST openingBrace = ast.findFirstToken(TokenTypes.SLIST);
87 if (openingBrace != null) {
88 final int length;
89 if (countEmpty) {
90 final DetailAST closingBrace = openingBrace.findFirstToken(TokenTypes.RCURLY);
91 length = getLengthOfBlock(openingBrace, closingBrace);
92 }
93 else {
94 length = countUsedLines(openingBrace);
95 }
96 if (length > max) {
97 final String methodName = ast.findFirstToken(TokenTypes.IDENT).getText();
98 log(ast, MSG_KEY, length, max, methodName);
99 }
100 }
101 }
102
103 /**
104 * Returns length of code.
105 *
106 * @param openingBrace block opening brace
107 * @param closingBrace block closing brace
108 * @return number of lines with code for current block
109 */
110 private static int getLengthOfBlock(DetailAST openingBrace, DetailAST closingBrace) {
111 final int startLineNo = openingBrace.getLineNo();
112 final int endLineNo = closingBrace.getLineNo();
113 return endLineNo - startLineNo + 1;
114 }
115
116 /**
117 * Count number of used code lines without comments.
118 *
119 * @param ast start ast
120 * @return number of used lines of code
121 */
122 private static int countUsedLines(DetailAST ast) {
123 final Deque<DetailAST> nodes = new ArrayDeque<>();
124 nodes.add(ast);
125 final BitSet usedLines = new BitSet();
126 while (!nodes.isEmpty()) {
127 final DetailAST node = nodes.removeFirst();
128 final int lineIndex = node.getLineNo();
129 // text block requires special treatment,
130 // since it is the only non-comment token that can span more than one line
131 if (node.getType() == TokenTypes.TEXT_BLOCK_LITERAL_BEGIN) {
132 final int endLineIndex = node.getLastChild().getLineNo();
133 usedLines.set(lineIndex, endLineIndex + 1);
134 }
135 else {
136 usedLines.set(lineIndex);
137 Stream.iterate(
138 node.getLastChild(), Objects::nonNull, DetailAST::getPreviousSibling
139 ).forEach(nodes::addFirst);
140 }
141 }
142 return usedLines.cardinality();
143 }
144
145 /**
146 * Setter to specify the maximum number of lines allowed.
147 *
148 * @param length the maximum length of a method.
149 * @since 3.0
150 */
151 public void setMax(int length) {
152 max = length;
153 }
154
155 /**
156 * Setter to control whether to count empty lines and comments.
157 *
158 * @param countEmpty whether to count empty and comments.
159 * @since 3.2
160 */
161 public void setCountEmpty(boolean countEmpty) {
162 this.countEmpty = countEmpty;
163 }
164
165 }