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