Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/puppycrawl/tools/checkstyle/checks/MethodLengthCheck.java


1   ////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code for adherence to a set of rules.
3   // Copyright (C) 2001-2002  Oliver Burn
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  package com.puppycrawl.tools.checkstyle.checks;
20  
21  import com.puppycrawl.tools.checkstyle.api.Check;
22  import com.puppycrawl.tools.checkstyle.api.DetailAST;
23  import com.puppycrawl.tools.checkstyle.api.TokenTypes;
24  
25  /**
26   * <p>
27   * Checks for long methods.
28   * </p>
29   * <p>
30   * Rationale: If a method becomes very long it is hard to understand.
31   * Therefore long methods should usually be refactored into several
32   * individual methods that focus on a specific task.
33   * </p>
34   *<p>
35   * The default maximum method length is 150 lines. To change the maximum
36   * number of lines, set property max.
37   * </p>
38   * <p>
39   * An example of how to configure the check is:
40   * </p>
41   * <pre>
42   * &lt;module name="MethodLength"/&gt;
43   * </pre>
44   * <p>
45   * An example of how to configure the check so that it accepts methods with at
46   * most 60 lines is:
47   * </p>
48   * <pre>
49   * &lt;module name="MethodLength"&gt;
50   *    &lt;property name="max" value="60"/&gt;
51   * &lt;/module&gt;
52   * </pre>
53   * @author Lars Kühne
54   */
55  public class MethodLengthCheck extends Check
56  {
57      /** the maximum number of lines */
58      private int mMax = 150;
59  
60      /** @see com.puppycrawl.tools.checkstyle.api.Check */
61      public int[] getDefaultTokens()
62      {
63          return new int[] {TokenTypes.METHOD_DEF, TokenTypes.CTOR_DEF};
64      }
65  
66      /** @see com.puppycrawl.tools.checkstyle.api.Check */
67      public void visitToken(DetailAST aAST)
68      {
69          final DetailAST openingBrace = aAST.findFirstToken(TokenTypes.SLIST);
70          if (openingBrace != null) {
71              final DetailAST closingBrace =
72                  openingBrace.findFirstToken(TokenTypes.RCURLY);
73              final int length =
74                  closingBrace.getLineNo() - openingBrace.getLineNo() + 1;
75              if (length > mMax) {
76                  log(aAST.getLineNo(),
77                      aAST.getColumnNo(),
78                      "maxLen.method",
79                      new Integer(length),
80                      new Integer(mMax));
81              }
82          }
83      }
84  
85      /**
86       * @param aLength the maximum length of a method.
87       */
88      public void setMax(int aLength)
89      {
90          mMax = aLength;
91      }
92  
93  }