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