001//////////////////////////////////////////////////////////////////////////////// 002// checkstyle: Checks Java source code for adherence to a set of rules. 003// Copyright (C) 2001-2014 Oliver Burn 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 com.puppycrawl.tools.checkstyle.api.Check; 023import com.puppycrawl.tools.checkstyle.api.DetailAST; 024import com.puppycrawl.tools.checkstyle.api.Utils; 025import java.util.regex.Pattern; 026import java.util.regex.PatternSyntaxException; 027import org.apache.commons.beanutils.ConversionException; 028 029/** 030 * Checks for long lines. 031 * 032 * <p> 033 * Rationale: Long lines are hard to read in printouts or if developers 034 * have limited screen space for the source code, e.g. if the IDE displays 035 * additional information like project tree, class hierarchy, etc. 036 * </p> 037 * 038 * <p> 039 * Note: Support for the special handling of imports in CheckStyle Version 2 040 * has been dropped as it is a special case of regexp: The user can set 041 * the ignorePattern to "^import" and achieve the same effect. 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 /** default maximum number of columns in a line */ 080 private static final int DEFAULT_MAX_COLUMNS = 80; 081 082 /** the maximum number of columns in a line */ 083 private int max = DEFAULT_MAX_COLUMNS; 084 085 /** the regexp when long lines are ignored */ 086 private Pattern ignorePattern; 087 088 /** 089 * Creates a new <code>LineLengthCheck</code> instance. 090 */ 091 public LineLengthCheck() 092 { 093 setIgnorePattern("^$"); 094 } 095 096 @Override 097 public int[] getDefaultTokens() 098 { 099 return new int[0]; 100 } 101 102 @Override 103 public void beginTree(DetailAST rootAST) 104 { 105 final String[] lines = getLines(); 106 for (int i = 0; i < lines.length; i++) { 107 108 final String line = lines[i]; 109 final int realLength = Utils.lengthExpandedTabs( 110 line, line.length(), getTabWidth()); 111 112 113 if ((realLength > max) 114 && !ignorePattern.matcher(line).find()) 115 { 116 log(i + 1, "maxLineLen", max, realLength); 117 } 118 } 119 } 120 121 /** 122 * @param length the maximum length of a line 123 */ 124 public void setMax(int length) 125 { 126 max = length; 127 } 128 129 /** 130 * Set the ignore pattern. 131 * @param format a <code>String</code> value 132 * @throws ConversionException unable to parse format 133 */ 134 public void setIgnorePattern(String format) 135 throws ConversionException 136 { 137 try { 138 ignorePattern = Utils.getPattern(format); 139 } 140 catch (final PatternSyntaxException e) { 141 throw new ConversionException("unable to parse " + format, e); 142 } 143 } 144}