Source code: com/puppycrawl/tools/checkstyle/checks/LineLengthCheck.java
1 ////////////////////////////////////////////////////////////////////////////////
2 // checkstyle: Checks Java source code for adherence to a set of rules.
3 // Copyright (C) 2001-2002 Oliver Burn
4 //
5 // This library is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU Lesser General Public
7 // License as published by the Free Software Foundation; either
8 // version 2.1 of the License, or (at your option) any later version.
9 //
10 // This library is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // Lesser General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public
16 // License along with this library; if not, write to the Free Software
17 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 ////////////////////////////////////////////////////////////////////////////////
19
20 package com.puppycrawl.tools.checkstyle.checks;
21
22 import com.puppycrawl.tools.checkstyle.api.Check;
23 import com.puppycrawl.tools.checkstyle.api.Utils;
24 import org.apache.regexp.RE;
25 import org.apache.regexp.RESyntaxException;
26 import org.apache.commons.beanutils.ConversionException;
27
28 /**
29 * Checks for long lines.
30 *
31 * <p>
32 * Rationale: Long lines are hard to read in printouts or if developers
33 * have limited screen space for the source code, e.g. if the IDE displays
34 * additional information like project tree, class hierarchy, etc.
35 * </p>
36 *
37 * <p>
38 * Note: Support for the special handling of imports in CheckStyle Version 2
39 * has been dropped as it is a special case of regexp: The user can set
40 * the ignorePattern to "^import" and achieve the same effect.
41 * </p>
42 * <p>
43 * The default maximum allowable line length is 80 characters. To change the
44 * maximum, set property max.
45 * </p>
46 * <p>
47 * To ignore lines in the check, set property ignorePattern to a regular
48 * expression for the lines to ignore.
49 * </p>
50 * <p>
51 * An example of how to configure the check is:
52 * </p>
53 * <pre>
54 * <module name="LineLength"/>
55 * </pre>
56 * <p> An example of how to configure the check to accept lines up to 120
57 * characters long is:
58 *</p>
59 * <pre>
60 * <module name="LineLength">
61 * <property name="max" value="120"/>
62 * </module>
63 * </pre>
64 * <p> An example of how to configure the check to ignore lines that begin with
65 * " * ", followed by just one word, such as within a Javadoc comment,
66 * is:
67 * </p>
68 * <pre>
69 * <module name="LineLength">
70 * <property name="ignorePattern" value="^ *\* *[^ ]+$"/>
71 * </module>
72 * </pre>
73 *
74 * @author Lars Kühne
75 */
76 public class LineLengthCheck extends Check
77 {
78 /** the maximum number of columns in a line */
79 private int mMax = 80;
80
81 /** the regexp when long lines are ignored */
82 private RE mIgnorePattern;
83
84 /**
85 * Creates a new <code>LineLengthCheck</code> instance.
86 */
87 public LineLengthCheck()
88 {
89 setIgnorePattern("^$");
90 }
91
92 /** @see com.puppycrawl.tools.checkstyle.api.Check */
93 public int[] getDefaultTokens()
94 {
95 return new int[0];
96 }
97
98 /** @see com.puppycrawl.tools.checkstyle.api.Check */
99 public void beginTree()
100 {
101 final String[] lines = getLines();
102 for (int i = 0; i < lines.length; i++) {
103
104 final String line = lines[i];
105 final int realLength = Utils.lengthExpandedTabs(
106 line, line.length(), getTabWidth());
107
108
109 if (realLength > mMax && !mIgnorePattern.match(line)) {
110 log(i + 1, "maxLineLen", new Integer(mMax));
111 }
112 }
113 }
114
115 /**
116 * @param aLength the maximum length of a line
117 */
118 public void setMax(int aLength)
119 {
120 mMax = aLength;
121 }
122
123 /**
124 * Set the ignore pattern.
125 * @param aFormat a <code>String</code> value
126 * @throws ConversionException unable to parse aFormat
127 */
128 public void setIgnorePattern(String aFormat)
129 throws ConversionException
130 {
131 try {
132 mIgnorePattern = Utils.getRE(aFormat);
133 }
134 catch (RESyntaxException e) {
135 throw new ConversionException("unable to parse " + aFormat, e);
136 }
137 }
138
139 }