View Javadoc
1   ////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code for adherence to a set of rules.
3   // Copyright (C) 2001-2018 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.io.File;
23  
24  import com.puppycrawl.tools.checkstyle.StatelessCheck;
25  import com.puppycrawl.tools.checkstyle.api.AbstractFileSetCheck;
26  import com.puppycrawl.tools.checkstyle.api.FileText;
27  
28  /**
29   * <p>
30   * Checks for long source files.
31   * </p>
32   * <p>
33   * Rationale: If a source file becomes very long it is hard to understand.
34   * Therefore long classes should usually be refactored into several
35   * individual classes that focus on a specific task.
36   * </p>
37   * <p>
38   * The default maximum file length is 2000 lines. To change the maximum
39   * number of lines, set property max.
40   * </p>
41   * <p>
42   * An example of how to configure the check is:
43   * </p>
44   * <pre>
45   * &lt;module name="FileLength"/&gt;
46   * </pre>
47   * <p>
48   * An example of how to configure the check so that it accepts files with at
49   * most 1500 lines is:
50   * </p>
51   * <pre>
52   * &lt;module name="FileLength"&gt;
53   *    &lt;property name="max" value="1500"/&gt;
54   * &lt;/module&gt;
55   * </pre>
56   * @author Lars K├╝hne
57   */
58  @StatelessCheck
59  public class FileLengthCheck extends AbstractFileSetCheck {
60  
61      /**
62       * A key is pointing to the warning message text in "messages.properties"
63       * file.
64       */
65      public static final String MSG_KEY = "maxLen.file";
66  
67      /** Default maximum number of lines. */
68      private static final int DEFAULT_MAX_LINES = 2000;
69  
70      /** The maximum number of lines. */
71      private int max = DEFAULT_MAX_LINES;
72  
73      @Override
74      protected void processFiltered(File file, FileText fileText) {
75          if (fileText.size() > max) {
76              log(1, MSG_KEY, fileText.size(), max);
77          }
78      }
79  
80      /**
81       * Sets the maximum length of a Java source file.
82       * @param length the maximum length of a Java source file
83       */
84      public void setMax(int length) {
85          max = length;
86      }
87  
88  }