1 | /////////////////////////////////////////////////////////////////////////////////////////////// | |
2 | // checkstyle: Checks Java source code and other text files for adherence to a set of rules. | |
3 | // Copyright (C) 2001-2022 the original author or authors. | |
4 | // | |
5 | // This library is free software; you can redistribute it and/or | |
6 | // modify it under the terms of the GNU Lesser General Public | |
7 | // License as published by the Free Software Foundation; either | |
8 | // version 2.1 of the License, or (at your option) any later version. | |
9 | // | |
10 | // This library is distributed in the hope that it will be useful, | |
11 | // but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 | // Lesser General Public License for more details. | |
14 | // | |
15 | // You should have received a copy of the GNU Lesser General Public | |
16 | // License along with this library; if not, write to the Free Software | |
17 | // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
18 | /////////////////////////////////////////////////////////////////////////////////////////////// | |
19 | ||
20 | package com.puppycrawl.tools.checkstyle.checks.sizes; | |
21 | ||
22 | import java.io.File; | |
23 | import java.util.regex.Pattern; | |
24 | ||
25 | import com.puppycrawl.tools.checkstyle.StatelessCheck; | |
26 | import com.puppycrawl.tools.checkstyle.api.AbstractFileSetCheck; | |
27 | import com.puppycrawl.tools.checkstyle.api.FileText; | |
28 | import com.puppycrawl.tools.checkstyle.utils.CommonUtil; | |
29 | ||
30 | /** | |
31 | * <p> | |
32 | * Checks for long lines. | |
33 | * </p> | |
34 | * <p> | |
35 | * Rationale: Long lines are hard to read in printouts or if developers | |
36 | * have limited screen space for the source code, e.g. if the IDE displays | |
37 | * additional information like project tree, class hierarchy, etc. | |
38 | * </p> | |
39 | * <ul> | |
40 | * <li> | |
41 | * The calculation of the length of a line takes into account the number of | |
42 | * expanded spaces for a tab character ({@code '\t'}). The default number of spaces is {@code 8}. | |
43 | * To specify a different number of spaces, the user can set | |
44 | * <a href="https://checkstyle.org/config.html#TreeWalker">{@code TreeWalker}</a> | |
45 | * property {@code tabWidth} which applies to all Checks, including {@code LineLength}; | |
46 | * or can set property {@code tabWidth} for {@code LineLength} alone. | |
47 | * </li> | |
48 | * <li> | |
49 | * By default, package and import statements (lines matching pattern {@code ^(package|import) .*}) | |
50 | * are not verified by this check. | |
51 | * </li> | |
52 | * <li> | |
53 | * Trailing comments are taken into consideration while calculating the line length. | |
54 | * <pre> | |
55 | * import java.util.regex.Pattern; // The length of this comment will be taken into consideration | |
56 | * </pre> | |
57 | * In the example above the length of the import statement is just 31 characters but total length | |
58 | * will be 94 characters. | |
59 | * </li> | |
60 | * </ul> | |
61 | * <ul> | |
62 | * <li> | |
63 | * Property {@code fileExtensions} - Specify file extensions that are accepted. | |
64 | * Type is {@code java.lang.String[]}. | |
65 | * Default value is {@code ""}. | |
66 | * </li> | |
67 | * <li> | |
68 | * Property {@code ignorePattern} - Specify pattern for lines to ignore. | |
69 | * Type is {@code java.util.regex.Pattern}. | |
70 | * Default value is {@code "^(package|import) .*"}. | |
71 | * </li> | |
72 | * <li> | |
73 | * Property {@code max} - Specify the maximum line length allowed. | |
74 | * Type is {@code int}. | |
75 | * Default value is {@code 80}. | |
76 | * </li> | |
77 | * </ul> | |
78 | * <p> | |
79 | * To configure the check to accept lines up to 80 characters long: | |
80 | * </p> | |
81 | * <pre> | |
82 | * <module name="LineLength"/> | |
83 | * </pre> | |
84 | * <p> | |
85 | * To configure the check to accept lines up to 120 characters long: | |
86 | * </p> | |
87 | * <pre> | |
88 | * <module name="LineLength"> | |
89 | * <property name="max" value="120"/> | |
90 | * </module> | |
91 | * </pre> | |
92 | * <p> | |
93 | * To configure the check to ignore lines that begin with {@code " * "} code, | |
94 | * followed by just one word, such as within a Javadoc comment: | |
95 | * </p> | |
96 | * <pre> | |
97 | * <module name="LineLength"> | |
98 | * <property name="ignorePattern" value="^ *\* *[^ ]+$"/> | |
99 | * </module> | |
100 | * </pre> | |
101 | * <p>To configure the check to only validate java files and ignore other extensions: | |
102 | * </p> | |
103 | * <pre> | |
104 | * <module name="LineLength"> | |
105 | * <property name="fileExtensions" value="java"/> | |
106 | * </module> | |
107 | * </pre> | |
108 | * <p>To configure the check to only validate xml and property files and ignore other extensions: | |
109 | * </p> | |
110 | * <pre> | |
111 | * <module name="LineLength"> | |
112 | * <property name="fileExtensions" value="xml, properties"/> | |
113 | * </module> | |
114 | * </pre> | |
115 | * <p>To configure check to validate {@code import} and {@code package} statements: | |
116 | * </p> | |
117 | * <pre> | |
118 | * <module name="LineLength"> | |
119 | * <property name="ignorePattern" value="^$"/> | |
120 | * <property name="max" value="50"/> | |
121 | * </module> | |
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 | |
150 | public 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 |
4
1. processFiltered : changed conditional boundary → KILLED 2. processFiltered : negated conditional → KILLED 3. processFiltered : removed conditional - replaced comparison check with false → KILLED 4. processFiltered : removed conditional - replaced comparison check with true → KILLED |
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 |
7
1. processFiltered : changed conditional boundary → KILLED 2. processFiltered : negated conditional → KILLED 3. processFiltered : negated conditional → KILLED 4. processFiltered : removed conditional - replaced equality check with false → KILLED 5. processFiltered : removed conditional - replaced equality check with true → KILLED 6. processFiltered : removed conditional - replaced comparison check with false → KILLED 7. processFiltered : removed conditional - replaced comparison check with true → KILLED |
if (realLength > max && !ignorePattern.matcher(line).find()) { |
175 |
2
1. processFiltered : Replaced integer addition with subtraction → KILLED 2. processFiltered : removed call to com/puppycrawl/tools/checkstyle/checks/sizes/LineLengthCheck::log → KILLED |
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 | } | |
Mutations | ||
169 |
1.1 2.2 3.3 4.4 |
|
174 |
1.1 2.2 3.3 4.4 5.5 6.6 7.7 |
|
175 |
1.1 2.2 |