001/////////////////////////////////////////////////////////////////////////////////////////////// 002// checkstyle: Checks Java source code and other text files for adherence to a set of rules. 003// Copyright (C) 2001-2026 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 * Notes: 043 * The calculation of the length of a line takes into account the number of 044 * expanded spaces for a tab character ({@code '\t'}). The default number of spaces is {@code 8}. 045 * To specify a different number of spaces, the user can set 046 * <a href="https://checkstyle.org/config.html#Checker">{@code Checker}</a> 047 * property {@code tabWidth} which applies to all Checks, including {@code LineLength}; 048 * or can set property {@code tabWidth} for {@code LineLength} alone. 049 * </li> 050 * <li> 051 * By default, package and import statements (lines matching pattern {@code ^(package|import) .*}) 052 * are not verified by this check. 053 * </li> 054 * <li> 055 * Trailing comments are taken into consideration while calculating the line length. 056 * {@snippet : 057 * import java.util.regex.Pattern; // The length of this comment will be taken into consideration 058 * } 059 * In the example above the length of the import statement is just 31 characters but total length 060 * will be 94 characters. 061 * </li> 062 * </ul> 063 * 064 * @since 3.0 065 * @noinspection EmptyClass 066 * @noinspectionreason EmptyClass - until IDEA-394242 067 */ 068@StatelessCheck 069public class LineLengthCheck extends AbstractFileSetCheck { 070 071 /** 072 * A key is pointing to the warning message text in "messages.properties" file. 073 */ 074 public static final String MSG_KEY = "maxLineLen"; 075 076 /** Default maximum number of columns in a line. */ 077 private static final int DEFAULT_MAX_COLUMNS = 80; 078 079 /** Specify the maximum line length allowed. */ 080 private int max = DEFAULT_MAX_COLUMNS; 081 082 /** Specify pattern for lines to ignore. */ 083 private Pattern ignorePattern = Pattern.compile("^(package|import) .*"); 084 085 /** 086 * Creates a new {@code LineLengthCheck} instance. 087 */ 088 public LineLengthCheck() { 089 // no code by default 090 } 091 092 @Override 093 protected void processFiltered(File file, FileText fileText) { 094 for (int index = 0; index < fileText.size(); index++) { 095 final String line = fileText.get(index); 096 final int realLength = CommonUtil.lengthExpandedTabs( 097 line, line.codePointCount(0, line.length()), getTabWidth()); 098 099 if (realLength > max && !ignorePattern.matcher(line).find()) { 100 log(index + 1, MSG_KEY, max, realLength); 101 } 102 } 103 } 104 105 /** 106 * Setter to specify the maximum line length allowed. 107 * 108 * @param length the maximum length of a line 109 * @since 3.0 110 */ 111 public void setMax(int length) { 112 max = length; 113 } 114 115 /** 116 * Setter to specify pattern for lines to ignore. 117 * 118 * @param pattern a pattern. 119 * @since 3.0 120 */ 121 public final void setIgnorePattern(Pattern pattern) { 122 ignorePattern = pattern; 123 } 124 125 /** 126 * Setter to specify the file extensions of the files to process. 127 * 128 * @param extensions the set of file extensions. A missing 129 * initial '.' character of an extension is automatically added. 130 * @throws IllegalArgumentException is argument is null 131 * @propertySince 8.24 132 */ 133 @Override 134 public final void setFileExtensions(String... extensions) { 135 super.setFileExtensions(extensions); 136 } 137 138}