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