View Javadoc
1   ///////////////////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3   // Copyright (C) 2001-2025 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  import java.util.regex.Pattern;
24  
25  import com.puppycrawl.tools.checkstyle.StatelessCheck;
26  import com.puppycrawl.tools.checkstyle.api.AbstractFileSetCheck;
27  import com.puppycrawl.tools.checkstyle.api.FileText;
28  import com.puppycrawl.tools.checkstyle.utils.CommonUtil;
29  
30  /**
31   * <div>
32   * Checks for long lines.
33   * </div>
34   *
35   * <p>
36   * Rationale: Long lines are hard to read in printouts or if developers
37   * have limited screen space for the source code, e.g. if the IDE displays
38   * additional information like project tree, class hierarchy, etc.
39   * </p>
40   * <ul>
41   * <li>
42   * Notes:
43   * The calculation of the length of a line takes into account the number of
44   * expanded spaces for a tab character ({@code '\t'}). The default number of spaces is {@code 8}.
45   * To specify a different number of spaces, the user can set
46   * <a href="https://checkstyle.org/config.html#Checker">{@code Checker}</a>
47   * property {@code tabWidth} which applies to all Checks, including {@code LineLength};
48   * or can set property {@code tabWidth} for {@code LineLength} alone.
49   * </li>
50   * <li>
51   * By default, package and import statements (lines matching pattern {@code ^(package|import) .*})
52   * are not verified by this check.
53   * </li>
54   * <li>
55   * Trailing comments are taken into consideration while calculating the line length.
56   * <div class="wrapper"><pre class="prettyprint"><code class="language-java">
57   * import java.util.regex.Pattern; // The length of this comment will be taken into consideration
58   * </code></pre></div>
59   * In the example above the length of the import statement is just 31 characters but total length
60   * will be 94 characters.
61   * </li>
62   * </ul>
63   *
64   * @since 3.0
65   */
66  @StatelessCheck
67  public class LineLengthCheck extends AbstractFileSetCheck {
68  
69      /**
70       * A key is pointing to the warning message text in "messages.properties"
71       * file.
72       */
73      public static final String MSG_KEY = "maxLineLen";
74  
75      /** Default maximum number of columns in a line. */
76      private static final int DEFAULT_MAX_COLUMNS = 80;
77  
78      /** Specify the maximum line length allowed. */
79      private int max = DEFAULT_MAX_COLUMNS;
80  
81      /** Specify pattern for lines to ignore. */
82      private Pattern ignorePattern = Pattern.compile("^(package|import) .*");
83  
84      @Override
85      protected void processFiltered(File file, FileText fileText) {
86          for (int i = 0; i < fileText.size(); i++) {
87              final String line = fileText.get(i);
88              final int realLength = CommonUtil.lengthExpandedTabs(
89                  line, line.codePointCount(0, line.length()), getTabWidth());
90  
91              if (realLength > max && !ignorePattern.matcher(line).find()) {
92                  log(i + 1, MSG_KEY, max, realLength);
93              }
94          }
95      }
96  
97      /**
98       * Setter to specify the maximum line length allowed.
99       *
100      * @param length the maximum length of a line
101      * @since 3.0
102      */
103     public void setMax(int length) {
104         max = length;
105     }
106 
107     /**
108      * Setter to specify pattern for lines to ignore.
109      *
110      * @param pattern a pattern.
111      * @since 3.0
112      */
113     public final void setIgnorePattern(Pattern pattern) {
114         ignorePattern = pattern;
115     }
116 
117     /**
118      * Setter to specify the file extensions of the files to process.
119      *
120      * @param extensions the set of file extensions. A missing
121      *         initial '.' character of an extension is automatically added.
122      * @throws IllegalArgumentException is argument is null
123      * @propertySince 8.24
124      */
125     @Override
126     public final void setFileExtensions(String... extensions) {
127         super.setFileExtensions(extensions);
128     }
129 
130 }