| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| LineLengthCheck |
|
| 2.0;2 |
| 1 | //////////////////////////////////////////////////////////////////////////////// | |
| 2 | // checkstyle: Checks Java source code for adherence to a set of rules. | |
| 3 | // Copyright (C) 2001-2014 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.sizes; | |
| 21 | ||
| 22 | import com.puppycrawl.tools.checkstyle.api.Check; | |
| 23 | import com.puppycrawl.tools.checkstyle.api.DetailAST; | |
| 24 | import com.puppycrawl.tools.checkstyle.api.Utils; | |
| 25 | import java.util.regex.Pattern; | |
| 26 | import java.util.regex.PatternSyntaxException; | |
| 27 | import org.apache.commons.beanutils.ConversionException; | |
| 28 | ||
| 29 | /** | |
| 30 | * Checks for long lines. | |
| 31 | * | |
| 32 | * <p> | |
| 33 | * Rationale: Long lines are hard to read in printouts or if developers | |
| 34 | * have limited screen space for the source code, e.g. if the IDE displays | |
| 35 | * additional information like project tree, class hierarchy, etc. | |
| 36 | * </p> | |
| 37 | * | |
| 38 | * <p> | |
| 39 | * Note: Support for the special handling of imports in CheckStyle Version 2 | |
| 40 | * has been dropped as it is a special case of regexp: The user can set | |
| 41 | * the ignorePattern to "^import" and achieve the same effect. | |
| 42 | * </p> | |
| 43 | * <p> | |
| 44 | * The default maximum allowable line length is 80 characters. To change the | |
| 45 | * maximum, set property max. | |
| 46 | * </p> | |
| 47 | * <p> | |
| 48 | * To ignore lines in the check, set property ignorePattern to a regular | |
| 49 | * expression for the lines to ignore. | |
| 50 | * </p> | |
| 51 | * <p> | |
| 52 | * An example of how to configure the check is: | |
| 53 | * </p> | |
| 54 | * <pre> | |
| 55 | * <module name="LineLength"/> | |
| 56 | * </pre> | |
| 57 | * <p> An example of how to configure the check to accept lines up to 120 | |
| 58 | * characters long is: | |
| 59 | *</p> | |
| 60 | * <pre> | |
| 61 | * <module name="LineLength"> | |
| 62 | * <property name="max" value="120"/> | |
| 63 | * </module> | |
| 64 | * </pre> | |
| 65 | * <p> An example of how to configure the check to ignore lines that begin with | |
| 66 | * " * ", followed by just one word, such as within a Javadoc comment, | |
| 67 | * is: | |
| 68 | * </p> | |
| 69 | * <pre> | |
| 70 | * <module name="LineLength"> | |
| 71 | * <property name="ignorePattern" value="^ *\* *[^ ]+$"/> | |
| 72 | * </module> | |
| 73 | * </pre> | |
| 74 | * | |
| 75 | * @author Lars Kühne | |
| 76 | */ | |
| 77 | public class LineLengthCheck extends Check | |
| 78 | { | |
| 79 | /** default maximum number of columns in a line */ | |
| 80 | private static final int DEFAULT_MAX_COLUMNS = 80; | |
| 81 | ||
| 82 | /** the maximum number of columns in a line */ | |
| 83 | 2 | private int mMax = DEFAULT_MAX_COLUMNS; |
| 84 | ||
| 85 | /** the regexp when long lines are ignored */ | |
| 86 | private Pattern mIgnorePattern; | |
| 87 | ||
| 88 | /** | |
| 89 | * Creates a new <code>LineLengthCheck</code> instance. | |
| 90 | */ | |
| 91 | public LineLengthCheck() | |
| 92 | 2 | { |
| 93 | 2 | setIgnorePattern("^$"); |
| 94 | 2 | } |
| 95 | ||
| 96 | @Override | |
| 97 | public int[] getDefaultTokens() | |
| 98 | { | |
| 99 | 2 | return new int[0]; |
| 100 | } | |
| 101 | ||
| 102 | @Override | |
| 103 | public void beginTree(DetailAST aRootAST) | |
| 104 | { | |
| 105 | 2 | final String[] lines = getLines(); |
| 106 | 452 | for (int i = 0; i < lines.length; i++) { |
| 107 | ||
| 108 | 450 | final String line = lines[i]; |
| 109 | 450 | final int realLength = Utils.lengthExpandedTabs( |
| 110 | line, line.length(), getTabWidth()); | |
| 111 | ||
| 112 | ||
| 113 | 450 | if ((realLength > mMax) |
| 114 | && !mIgnorePattern.matcher(line).find()) | |
| 115 | { | |
| 116 | 4 | log(i + 1, "maxLineLen", mMax, realLength); |
| 117 | } | |
| 118 | } | |
| 119 | 2 | } |
| 120 | ||
| 121 | /** | |
| 122 | * @param aLength the maximum length of a line | |
| 123 | */ | |
| 124 | public void setMax(int aLength) | |
| 125 | { | |
| 126 | 2 | mMax = aLength; |
| 127 | 2 | } |
| 128 | ||
| 129 | /** | |
| 130 | * Set the ignore pattern. | |
| 131 | * @param aFormat a <code>String</code> value | |
| 132 | * @throws ConversionException unable to parse aFormat | |
| 133 | */ | |
| 134 | public void setIgnorePattern(String aFormat) | |
| 135 | throws ConversionException | |
| 136 | { | |
| 137 | try { | |
| 138 | 4 | mIgnorePattern = Utils.getPattern(aFormat); |
| 139 | } | |
| 140 | 0 | catch (final PatternSyntaxException e) { |
| 141 | 0 | throw new ConversionException("unable to parse " + aFormat, e); |
| 142 | 4 | } |
| 143 | 4 | } |
| 144 | } |