001//////////////////////////////////////////////////////////////////////////////// 002// checkstyle: Checks Java source code for adherence to a set of rules. 003// Copyright (C) 2001-2016 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.util.regex.Pattern; 023 024import com.puppycrawl.tools.checkstyle.api.AbstractCheck; 025import com.puppycrawl.tools.checkstyle.api.DetailAST; 026import com.puppycrawl.tools.checkstyle.utils.CommonUtils; 027 028/** 029 * Checks for long lines. 030 * 031 * <p> 032 * Rationale: Long lines are hard to read in printouts or if developers 033 * have limited screen space for the source code, e.g. if the IDE displays 034 * additional information like project tree, class hierarchy, etc. 035 * </p> 036 * 037 * <p> 038 * Package statements and import statements (lines matching pattern 039 * {@code ^(package|import) .*}), and are not verified by this check. 040 * </p> 041 * <p> 042 * The default maximum allowable line length is 80 characters. To change the 043 * maximum, set property max. 044 * </p> 045 * <p> 046 * To ignore lines in the check, set property ignorePattern to a regular 047 * expression for the lines to ignore. 048 * </p> 049 * <p> 050 * An example of how to configure the check is: 051 * </p> 052 * <pre> 053 * <module name="LineLength"/> 054 * </pre> 055 * <p> An example of how to configure the check to accept lines up to 120 056 * characters long is: 057 *</p> 058 * <pre> 059 * <module name="LineLength"> 060 * <property name="max" value="120"/> 061 * </module> 062 * </pre> 063 * <p> An example of how to configure the check to ignore lines that begin with 064 * " * ", followed by just one word, such as within a Javadoc comment, 065 * is: 066 * </p> 067 * <pre> 068 * <module name="LineLength"> 069 * <property name="ignorePattern" value="^ *\* *[^ ]+$"/> 070 * </module> 071 * </pre> 072 * 073 * @author Lars Kühne 074 */ 075public class LineLengthCheck extends AbstractCheck { 076 077 /** 078 * A key is pointing to the warning message text in "messages.properties" 079 * file. 080 */ 081 public static final String MSG_KEY = "maxLineLen"; 082 083 /** Default maximum number of columns in a line. */ 084 private static final int DEFAULT_MAX_COLUMNS = 80; 085 086 /** Patterns matching package, import, and import static statements. */ 087 private static final Pattern IGNORE_PATTERN = Pattern.compile("^(package|import) .*"); 088 089 /** The maximum number of columns in a line. */ 090 private int max = DEFAULT_MAX_COLUMNS; 091 092 /** The regexp when long lines are ignored. */ 093 private Pattern ignorePattern; 094 095 /** 096 * Creates a new {@code LineLengthCheck} instance. 097 */ 098 public LineLengthCheck() { 099 setIgnorePattern("^$"); 100 } 101 102 @Override 103 public int[] getDefaultTokens() { 104 return CommonUtils.EMPTY_INT_ARRAY; 105 } 106 107 @Override 108 public int[] getAcceptableTokens() { 109 return CommonUtils.EMPTY_INT_ARRAY; 110 } 111 112 @Override 113 public int[] getRequiredTokens() { 114 return CommonUtils.EMPTY_INT_ARRAY; 115 } 116 117 @Override 118 public void beginTree(DetailAST rootAST) { 119 final String[] lines = getLines(); 120 for (int i = 0; i < lines.length; i++) { 121 122 final String line = lines[i]; 123 final int realLength = CommonUtils.lengthExpandedTabs( 124 line, line.length(), getTabWidth()); 125 126 if (realLength > max && !IGNORE_PATTERN.matcher(line).find() 127 && !ignorePattern.matcher(line).find()) { 128 log(i + 1, MSG_KEY, max, realLength); 129 } 130 } 131 } 132 133 /** 134 * Sets the maximum length of a line. 135 * @param length the maximum length of a line 136 */ 137 public void setMax(int length) { 138 max = length; 139 } 140 141 /** 142 * Set the ignore pattern. 143 * @param format a {@code String} value 144 */ 145 public final void setIgnorePattern(String format) { 146 ignorePattern = CommonUtils.createPattern(format); 147 } 148}