001////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code for adherence to a set of rules.
003// Copyright (C) 2001-2021 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 * Type is {@code java.lang.String[]}.
057 * Default value is {@code ""}.
058 * </li>
059 * <li>
060 * Property {@code ignorePattern} - Specify pattern for lines to ignore.
061 * Type is {@code java.util.regex.Pattern}.
062 * Default value is {@code "^$"}.
063 * </li>
064 * <li>
065 * Property {@code max} - Specify the maximum line length allowed.
066 * Type is {@code int}.
067 * Default value is {@code 80}.
068 * </li>
069 * </ul>
070 * <p>
071 * To configure the check to accept lines up to 80 characters long:
072 * </p>
073 * <pre>
074 * &lt;module name="LineLength"/&gt;
075 * </pre>
076 * <p>
077 * To configure the check to accept lines up to 120 characters long:
078 * </p>
079 * <pre>
080 * &lt;module name="LineLength"&gt;
081 *   &lt;property name="max" value="120"/&gt;
082 * &lt;/module&gt;
083 * </pre>
084 * <p>
085 * To configure the check to ignore lines that begin with {@code " * "} code,
086 * followed by just one word, such as within a Javadoc comment:
087 * </p>
088 * <pre>
089 * &lt;module name="LineLength"&gt;
090 *   &lt;property name="ignorePattern" value="^ *\* *[^ ]+$"/&gt;
091 * &lt;/module&gt;
092 * </pre>
093 * <p>To configure the check to only validate java files and ignore other extensions:
094 * </p>
095 * <pre>
096 * &lt;module name="LineLength"&gt;
097 *   &lt;property name="fileExtensions" value="java"/&gt;
098 * &lt;/module&gt;
099 * </pre>
100 * <p>To configure the check to only validate xml and property files and ignore other extensions:
101 * </p>
102 * <pre>
103 * &lt;module name="LineLength"&gt;
104 *   &lt;property name="fileExtensions" value="xml, properties"/&gt;
105 * &lt;/module&gt;
106 * </pre>
107 * <p>
108 * Parent is {@code com.puppycrawl.tools.checkstyle.Checker}
109 * </p>
110 * <p>
111 * Violation Message Keys:
112 * </p>
113 * <ul>
114 * <li>
115 * {@code maxLineLen}
116 * </li>
117 * </ul>
118 *
119 * @since 3.0
120 */
121@StatelessCheck
122public class LineLengthCheck extends AbstractFileSetCheck {
123
124    /**
125     * A key is pointing to the warning message text in "messages.properties"
126     * file.
127     */
128    public static final String MSG_KEY = "maxLineLen";
129
130    /** Default maximum number of columns in a line. */
131    private static final int DEFAULT_MAX_COLUMNS = 80;
132
133    /** Patterns matching package, import, and import static statements. */
134    private static final Pattern IGNORE_PATTERN = Pattern.compile("^(package|import) .*");
135
136    /** Specify the maximum line length allowed. */
137    private int max = DEFAULT_MAX_COLUMNS;
138
139    /** Specify pattern for lines to ignore. */
140    private Pattern ignorePattern = Pattern.compile("^$");
141
142    @Override
143    protected void processFiltered(File file, FileText fileText) {
144        for (int i = 0; i < fileText.size(); i++) {
145            final String line = fileText.get(i);
146            final int realLength = CommonUtil.lengthExpandedTabs(
147                line, line.codePointCount(0, line.length()), getTabWidth());
148
149            if (realLength > max && !IGNORE_PATTERN.matcher(line).find()
150                && !ignorePattern.matcher(line).find()) {
151                log(i + 1, MSG_KEY, max, realLength);
152            }
153        }
154    }
155
156    /**
157     * Setter to specify the maximum line length allowed.
158     *
159     * @param length the maximum length of a line
160     */
161    public void setMax(int length) {
162        max = length;
163    }
164
165    /**
166     * Setter to specify pattern for lines to ignore.
167     *
168     * @param pattern a pattern.
169     */
170    public final void setIgnorePattern(Pattern pattern) {
171        ignorePattern = pattern;
172    }
173
174}