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 * Note: Support for the special handling of imports in CheckStyle Version 2 041 * has been dropped as it is a special case of regexp: The user can set 042 * the ignorePattern to "^import" and achieve the same effect. 043 * </p> 044 * <p> 045 * The default maximum allowable line length is 80 characters. To change the 046 * maximum, set property max. 047 * </p> 048 * <p> 049 * To ignore lines in the check, set property ignorePattern to a regular 050 * expression for the lines to ignore. 051 * </p> 052 * <p> 053 * An example of how to configure the check is: 054 * </p> 055 * <pre> 056 * <module name="LineLength"/> 057 * </pre> 058 * <p> An example of how to configure the check to accept lines up to 120 059 * characters long is: 060 *</p> 061 * <pre> 062 * <module name="LineLength"> 063 * <property name="max" value="120"/> 064 * </module> 065 * </pre> 066 * <p> An example of how to configure the check to ignore lines that begin with 067 * " * ", followed by just one word, such as within a Javadoc comment, 068 * is: 069 * </p> 070 * <pre> 071 * <module name="LineLength"> 072 * <property name="ignorePattern" value="^ *\* *[^ ]+$"/> 073 * </module> 074 * </pre> 075 * 076 * @author Lars Kühne 077 */ 078public class LineLengthCheck extends Check { 079 080 /** 081 * A key is pointing to the warning message text in "messages.properties" 082 * file. 083 */ 084 public static final String MSG_KEY = "maxLineLen"; 085 086 /** Default maximum number of columns in a line. */ 087 private static final int DEFAULT_MAX_COLUMNS = 80; 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 ArrayUtils.EMPTY_INT_ARRAY; 105 } 106 107 @Override 108 public int[] getAcceptableTokens() { 109 return ArrayUtils.EMPTY_INT_ARRAY; 110 } 111 112 @Override 113 public int[] getRequiredTokens() { 114 return ArrayUtils.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 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}