001/////////////////////////////////////////////////////////////////////////////////////////////// 002// checkstyle: Checks Java source code and other text files for adherence to a set of rules. 003// Copyright (C) 2001-2025 the original author or authors. 004// 005// This library is free software; you can redistribute it and/or 006// modify it under the terms of the GNU Lesser General Public 007// License as published by the Free Software Foundation; either 008// version 2.1 of the License, or (at your option) any later version. 009// 010// This library is distributed in the hope that it will be useful, 011// but WITHOUT ANY WARRANTY; without even the implied warranty of 012// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 013// Lesser General Public License for more details. 014// 015// You should have received a copy of the GNU Lesser General Public 016// License along with this library; if not, write to the Free Software 017// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 018/////////////////////////////////////////////////////////////////////////////////////////////// 019 020package com.puppycrawl.tools.checkstyle.checks.sizes; 021 022import java.io.File; 023import java.util.regex.Pattern; 024 025import com.puppycrawl.tools.checkstyle.StatelessCheck; 026import com.puppycrawl.tools.checkstyle.api.AbstractFileSetCheck; 027import com.puppycrawl.tools.checkstyle.api.FileText; 028import com.puppycrawl.tools.checkstyle.utils.CommonUtil; 029 030/** 031 * <div> 032 * Checks for long lines. 033 * </div> 034 * 035 * <p> 036 * Rationale: Long lines are hard to read in printouts or if developers 037 * have limited screen space for the source code, e.g. if the IDE displays 038 * additional information like project tree, class hierarchy, etc. 039 * </p> 040 * <ul> 041 * <li> 042 * The calculation of the length of a line takes into account the number of 043 * expanded spaces for a tab character ({@code '\t'}). The default number of spaces is {@code 8}. 044 * To specify a different number of spaces, the user can set 045 * <a href="https://checkstyle.org/config.html#Checker">{@code Checker}</a> 046 * property {@code tabWidth} which applies to all Checks, including {@code LineLength}; 047 * or can set property {@code tabWidth} for {@code LineLength} alone. 048 * </li> 049 * <li> 050 * By default, package and import statements (lines matching pattern {@code ^(package|import) .*}) 051 * are not verified by this check. 052 * </li> 053 * <li> 054 * Trailing comments are taken into consideration while calculating the line length. 055 * <pre> 056 * import java.util.regex.Pattern; // The length of this comment will be taken into consideration 057 * </pre> 058 * In the example above the length of the import statement is just 31 characters but total length 059 * will be 94 characters. 060 * </li> 061 * </ul> 062 * <ul> 063 * <li> 064 * Property {@code fileExtensions} - Specify the file extensions of the files to process. 065 * Type is {@code java.lang.String[]}. 066 * Default value is {@code ""}. 067 * Since version 8.24 068 * </li> 069 * <li> 070 * Property {@code ignorePattern} - Specify pattern for lines to ignore. 071 * Type is {@code java.util.regex.Pattern}. 072 * Default value is {@code "^(package|import) .*"}. 073 * </li> 074 * <li> 075 * Property {@code max} - Specify the maximum line length allowed. 076 * Type is {@code int}. 077 * Default value is {@code 80}. 078 * </li> 079 * </ul> 080 * 081 * <p> 082 * Parent is {@code com.puppycrawl.tools.checkstyle.Checker} 083 * </p> 084 * 085 * <p> 086 * Violation Message Keys: 087 * </p> 088 * <ul> 089 * <li> 090 * {@code maxLineLen} 091 * </li> 092 * </ul> 093 * 094 * @since 3.0 095 */ 096@StatelessCheck 097public class LineLengthCheck extends AbstractFileSetCheck { 098 099 /** 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}