View Javadoc
1   ////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code for adherence to a set of rules.
3   // Copyright (C) 2001-2017 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   * <p>
39   * The default maximum anonymous inner class length is 20 lines.
40   * To change the maximum number of lines, set property max.
41   * </p>
42   * <p>
43   * An example of how to configure the check is:
44   * </p>
45   * <pre>
46   * &lt;module name="AnonInnerLength"/&gt;
47   * </pre>
48   * <p>
49   * An example of how to configure the check so that it accepts anonymous
50   * inner classes with up to 60 lines is:
51   * </p>
52   * <pre>
53   * &lt;module name="AnonInnerLength"&gt;
54   *    &lt;property name="max" value="60"/&gt;
55   * &lt;/module&gt;
56   * </pre>
57   *
58   * @author Rob Worth
59   */
60  @StatelessCheck
61  public class AnonInnerLengthCheck extends AbstractCheck {
62  
63      /**
64       * A key is pointing to the warning message text in "messages.properties"
65       * file.
66       */
67      public static final String MSG_KEY = "maxLen.anonInner";
68  
69      /** Default maximum number of lines. */
70      private static final int DEFAULT_MAX = 20;
71  
72      /** Maximum number of lines. */
73      private int max = DEFAULT_MAX;
74  
75      @Override
76      public int[] getDefaultTokens() {
77          return getRequiredTokens();
78      }
79  
80      @Override
81      public int[] getAcceptableTokens() {
82          return getRequiredTokens();
83      }
84  
85      @Override
86      public int[] getRequiredTokens() {
87          return new int[] {TokenTypes.LITERAL_NEW};
88      }
89  
90      @Override
91      public void visitToken(DetailAST ast) {
92          final DetailAST openingBrace = ast.findFirstToken(TokenTypes.OBJBLOCK);
93          if (openingBrace != null) {
94              final DetailAST closingBrace =
95                  openingBrace.findFirstToken(TokenTypes.RCURLY);
96              final int length =
97                  closingBrace.getLineNo() - openingBrace.getLineNo() + 1;
98              if (length > max) {
99                  log(ast.getLineNo(), ast.getColumnNo(), MSG_KEY,
100                         length, max);
101             }
102         }
103     }
104 
105     /**
106      * Sets maximum length of an anonymous inner class.
107      * @param length the maximum length of an anonymous inner class.
108      */
109     public void setMax(int length) {
110         max = length;
111     }
112 }