1 ///////////////////////////////////////////////////////////////////////////////////////////////
2 // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3 // Copyright (C) 2001-2024 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 com.puppycrawl.tools.checkstyle.StatelessCheck;
23 import com.puppycrawl.tools.checkstyle.api.AbstractCheck;
24 import com.puppycrawl.tools.checkstyle.api.DetailAST;
25 import com.puppycrawl.tools.checkstyle.api.TokenTypes;
26
27 /**
28 * <div>
29 * Checks for long anonymous inner classes.
30 * </div>
31 *
32 * <p>
33 * Rationale: If an anonymous inner class becomes very long
34 * it is hard to understand and to see the flow of the method
35 * where the class is defined. Therefore, long anonymous inner
36 * classes should usually be refactored into a named inner class.
37 * See also Bloch, Effective Java, p. 93.
38 * </p>
39 * <ul>
40 * <li>
41 * Property {@code max} - Specify the maximum number of lines allowed.
42 * Type is {@code int}.
43 * Default value is {@code 20}.
44 * </li>
45 * </ul>
46 *
47 * <p>
48 * Parent is {@code com.puppycrawl.tools.checkstyle.TreeWalker}
49 * </p>
50 *
51 * <p>
52 * Violation Message Keys:
53 * </p>
54 * <ul>
55 * <li>
56 * {@code maxLen.anonInner}
57 * </li>
58 * </ul>
59 *
60 * @since 3.2
61 */
62 @StatelessCheck
63 public class AnonInnerLengthCheck extends AbstractCheck {
64
65 /**
66 * A key is pointing to the warning message text in "messages.properties"
67 * file.
68 */
69 public static final String MSG_KEY = "maxLen.anonInner";
70
71 /** Default maximum number of lines. */
72 private static final int DEFAULT_MAX = 20;
73
74 /** Specify the maximum number of lines allowed. */
75 private int max = DEFAULT_MAX;
76
77 @Override
78 public int[] getDefaultTokens() {
79 return getRequiredTokens();
80 }
81
82 @Override
83 public int[] getAcceptableTokens() {
84 return getRequiredTokens();
85 }
86
87 @Override
88 public int[] getRequiredTokens() {
89 return new int[] {TokenTypes.LITERAL_NEW};
90 }
91
92 @Override
93 public void visitToken(DetailAST ast) {
94 final DetailAST openingBrace = ast.findFirstToken(TokenTypes.OBJBLOCK);
95 if (openingBrace != null) {
96 final DetailAST closingBrace =
97 openingBrace.findFirstToken(TokenTypes.RCURLY);
98 final int length =
99 closingBrace.getLineNo() - openingBrace.getLineNo() + 1;
100 if (length > max) {
101 log(ast, MSG_KEY, length, max);
102 }
103 }
104 }
105
106 /**
107 * Setter to specify the maximum number of lines allowed.
108 *
109 * @param length the maximum length of an anonymous inner class.
110 * @since 3.2
111 */
112 public void setMax(int length) {
113 max = length;
114 }
115
116 }