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.io.File;
023import java.util.regex.Pattern;
024
025import com.puppycrawl.tools.checkstyle.StatelessCheck;
026import com.puppycrawl.tools.checkstyle.api.AbstractFileSetCheck;
027import com.puppycrawl.tools.checkstyle.api.FileText;
028import com.puppycrawl.tools.checkstyle.utils.CommonUtil;
029
030/**
031 * <p>
032 * Checks for long lines.
033 * </p>
034 * <p>
035 * Rationale: Long lines are hard to read in printouts or if developers
036 * have limited screen space for the source code, e.g. if the IDE displays
037 * additional information like project tree, class hierarchy, etc.
038 * </p>
039 * <ul>
040 * <li>
041 * The calculation of the length of a line takes into account the number of
042 * expanded spaces for a tab character ({@code '\t'}). The default number of spaces is {@code 8}.
043 * To specify a different number of spaces, the user can set
044 * <a href="https://checkstyle.org/config.html#TreeWalker">{@code TreeWalker}</a>
045 * property {@code tabWidth} which applies to all Checks, including {@code LineLength};
046 * or can set property {@code tabWidth} for {@code LineLength} alone.
047 * </li>
048 * <li>
049 * Package and import statements (lines matching pattern {@code ^(package|import) .*})
050 * are not verified by this check.
051 * </li>
052 * </ul>
053 * <ul>
054 * <li>
055 * Property {@code fileExtensions} - Specify file extensions that are accepted.
056 * Default value is {@code all files}.
057 * </li>
058 * <li>
059 * Property {@code ignorePattern} - Specify pattern for lines to ignore.
060 * Default value is {@code "^$" (empty)}.
061 * </li>
062 * <li>
063 * Property {@code max} - Specify the maximum line length allowed.
064 * Default value is {@code 80}.
065 * </li>
066 * </ul>
067 * <p>
068 * To configure the check to accept lines up to 80 characters long:
069 * </p>
070 * <pre>
071 * &lt;module name="LineLength"/&gt;
072 * </pre>
073 * <p>
074 * To configure the check to accept lines up to 120 characters long:
075 * </p>
076 * <pre>
077 * &lt;module name="LineLength"&gt;
078 *   &lt;property name="max" value="120"/&gt;
079 * &lt;/module&gt;
080 * </pre>
081 * <p>
082 * To configure the check to ignore lines that begin with {@code " * "} code,
083 * followed by just one word, such as within a Javadoc comment:
084 * </p>
085 * <pre>
086 * &lt;module name="LineLength"&gt;
087 *   &lt;property name="ignorePattern" value="^ *\* *[^ ]+$"/&gt;
088 * &lt;/module&gt;
089 * </pre>
090 * <p>To configure the check to only validate java files and ignore other extensions:
091 * </p>
092 * <pre>
093 * &lt;module name="LineLength"&gt;
094 *   &lt;property name="fileExtensions" value="java"/&gt;
095 * &lt;/module&gt;
096 * </pre>
097 * <p>To configure the check to only validate xml and property files and ignore other extensions:
098 * </p>
099 * <pre>
100 * &lt;module name="LineLength"&gt;
101 *   &lt;property name="fileExtensions" value="xml, properties"/&gt;
102 * &lt;/module&gt;
103 * </pre>
104 *
105 * @since 3.0
106 */
107@StatelessCheck
108public class LineLengthCheck extends AbstractFileSetCheck {
109
110    /**
111     * A key is pointing to the warning message text in "messages.properties"
112     * file.
113     */
114    public static final String MSG_KEY = "maxLineLen";
115
116    /** Default maximum number of columns in a line. */
117    private static final int DEFAULT_MAX_COLUMNS = 80;
118
119    /** Patterns matching package, import, and import static statements. */
120    private static final Pattern IGNORE_PATTERN = Pattern.compile("^(package|import) .*");
121
122    /** Specify the maximum line length allowed. */
123    private int max = DEFAULT_MAX_COLUMNS;
124
125    /** Specify pattern for lines to ignore. */
126    private Pattern ignorePattern = Pattern.compile("^$");
127
128    @Override
129    protected void processFiltered(File file, FileText fileText) {
130        for (int i = 0; i < fileText.size(); i++) {
131            final String line = fileText.get(i);
132            final int realLength = CommonUtil.lengthExpandedTabs(
133                line, line.length(), getTabWidth());
134
135            if (realLength > max && !IGNORE_PATTERN.matcher(line).find()
136                && !ignorePattern.matcher(line).find()) {
137                log(i + 1, MSG_KEY, max, realLength);
138            }
139        }
140    }
141
142    /**
143     * Setter to specify the maximum line length allowed.
144     *
145     * @param length the maximum length of a line
146     */
147    public void setMax(int length) {
148        max = length;
149    }
150
151    /**
152     * Setter to specify pattern for lines to ignore.
153     *
154     * @param pattern a pattern.
155     */
156    public final void setIgnorePattern(Pattern pattern) {
157        ignorePattern = pattern;
158    }
159
160}