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