001////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code for adherence to a set of rules.
003// Copyright (C) 2001-2022 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 * By default package and import statements (lines matching pattern {@code ^(package|import) .*})
050 * are not verified by this check.
051 * </li>
052 * <li>
053 * Trailing comments are taken into consideration while calculating the line length.
054 * <pre>
055 * import java.util.regex.Pattern; // The length of this comment will be taken into consideration
056 * </pre>
057 * In the example above the length of the import statement is just 31 characters but total length
058 * will be 94 characters.
059 * </li>
060 * </ul>
061 * <ul>
062 * <li>
063 * Property {@code fileExtensions} - Specify file extensions that are accepted.
064 * Type is {@code java.lang.String[]}.
065 * Default value is {@code ""}.
066 * </li>
067 * <li>
068 * Property {@code ignorePattern} - Specify pattern for lines to ignore.
069 * Type is {@code java.util.regex.Pattern}.
070 * Default value is {@code "^(package|import) .*"}.
071 * </li>
072 * <li>
073 * Property {@code max} - Specify the maximum line length allowed.
074 * Type is {@code int}.
075 * Default value is {@code 80}.
076 * </li>
077 * </ul>
078 * <p>
079 * To configure the check to accept lines up to 80 characters long:
080 * </p>
081 * <pre>
082 * &lt;module name="LineLength"/&gt;
083 * </pre>
084 * <p>
085 * To configure the check to accept lines up to 120 characters long:
086 * </p>
087 * <pre>
088 * &lt;module name="LineLength"&gt;
089 *   &lt;property name="max" value="120"/&gt;
090 * &lt;/module&gt;
091 * </pre>
092 * <p>
093 * To configure the check to ignore lines that begin with {@code " * "} code,
094 * followed by just one word, such as within a Javadoc comment:
095 * </p>
096 * <pre>
097 * &lt;module name="LineLength"&gt;
098 *   &lt;property name="ignorePattern" value="^ *\* *[^ ]+$"/&gt;
099 * &lt;/module&gt;
100 * </pre>
101 * <p>To configure the check to only validate java files and ignore other extensions:
102 * </p>
103 * <pre>
104 * &lt;module name="LineLength"&gt;
105 *   &lt;property name="fileExtensions" value="java"/&gt;
106 * &lt;/module&gt;
107 * </pre>
108 * <p>To configure the check to only validate xml and property files and ignore other extensions:
109 * </p>
110 * <pre>
111 * &lt;module name="LineLength"&gt;
112 *   &lt;property name="fileExtensions" value="xml, properties"/&gt;
113 * &lt;/module&gt;
114 * </pre>
115 * <p>To configure check to validate {@code import} and {@code package} statements:
116 * </p>
117 * <pre>
118 * &lt;module name="LineLength"&gt;
119 *   &lt;property name="ignorePattern" value="^$"/&gt;
120 *   &lt;property name="max" value="50"/&gt;
121 * &lt;/module&gt;
122 * </pre>
123 * <p>
124 * Example:
125 * </p>
126 * <pre>
127 * // violation below 'Line is longer than 50 characters (found 54)'
128 * package com.puppycrawl.tools.checkstyle.checks.design;
129 *
130 * // violation below 'Line is longer than 50 characters (found 86)'
131 * import com.puppycrawl.tools.checkstyle.grammar.comments.InputFullOfSinglelineComments;
132 *
133 * import java.util.Arrays; // ok
134 * </pre>
135 * <p>
136 * Parent is {@code com.puppycrawl.tools.checkstyle.Checker}
137 * </p>
138 * <p>
139 * Violation Message Keys:
140 * </p>
141 * <ul>
142 * <li>
143 * {@code maxLineLen}
144 * </li>
145 * </ul>
146 *
147 * @since 3.0
148 */
149@StatelessCheck
150public class LineLengthCheck extends AbstractFileSetCheck {
151
152    /**
153     * A key is pointing to the warning message text in "messages.properties"
154     * file.
155     */
156    public static final String MSG_KEY = "maxLineLen";
157
158    /** Default maximum number of columns in a line. */
159    private static final int DEFAULT_MAX_COLUMNS = 80;
160
161    /** Specify the maximum line length allowed. */
162    private int max = DEFAULT_MAX_COLUMNS;
163
164    /** Specify pattern for lines to ignore. */
165    private Pattern ignorePattern = Pattern.compile("^(package|import) .*");
166
167    @Override
168    protected void processFiltered(File file, FileText fileText) {
169        for (int i = 0; i < fileText.size(); i++) {
170            final String line = fileText.get(i);
171            final int realLength = CommonUtil.lengthExpandedTabs(
172                line, line.codePointCount(0, line.length()), getTabWidth());
173
174            if (realLength > max && !ignorePattern.matcher(line).find()) {
175                log(i + 1, MSG_KEY, max, realLength);
176            }
177        }
178    }
179
180    /**
181     * Setter to specify the maximum line length allowed.
182     *
183     * @param length the maximum length of a line
184     */
185    public void setMax(int length) {
186        max = length;
187    }
188
189    /**
190     * Setter to specify pattern for lines to ignore.
191     *
192     * @param pattern a pattern.
193     */
194    public final void setIgnorePattern(Pattern pattern) {
195        ignorePattern = pattern;
196    }
197
198}