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