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