001////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code for adherence to a set of rules.
003// Copyright (C) 2001-2019 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.CommonUtil;
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 */
075@StatelessCheck
076public class LineLengthCheck extends AbstractCheck {
077
078    /**
079     * A key is pointing to the warning message text in "messages.properties"
080     * file.
081     */
082    public static final String MSG_KEY = "maxLineLen";
083
084    /** Default maximum number of columns in a line. */
085    private static final int DEFAULT_MAX_COLUMNS = 80;
086
087    /** Patterns matching package, import, and import static statements. */
088    private static final Pattern IGNORE_PATTERN = Pattern.compile("^(package|import) .*");
089
090    /** The maximum number of columns in a line. */
091    private int max = DEFAULT_MAX_COLUMNS;
092
093    /** The regexp when long lines are ignored. */
094    private Pattern ignorePattern = Pattern.compile("^$");
095
096    @Override
097    public int[] getDefaultTokens() {
098        return getRequiredTokens();
099    }
100
101    @Override
102    public int[] getAcceptableTokens() {
103        return getRequiredTokens();
104    }
105
106    @Override
107    public int[] getRequiredTokens() {
108        return CommonUtil.EMPTY_INT_ARRAY;
109    }
110
111    @Override
112    public void beginTree(DetailAST rootAST) {
113        final String[] lines = getLines();
114        for (int i = 0; i < lines.length; i++) {
115            final String line = lines[i];
116            final int realLength = CommonUtil.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
142}