Skip to content

Commit 9a39d19

Browse files
rnveachromani
authored andcommitted
Issue #2482: Exclude lines with package in LineLength check
1 parent f30512c commit 9a39d19

File tree

4 files changed

+31
-5
lines changed

4 files changed

+31
-5
lines changed

src/main/java/com/puppycrawl/tools/checkstyle/checks/sizes/LineLengthCheck.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@
3737
* </p>
3838
*
3939
* <p>
40-
* Import statements (lines matching pattern {@code ^import .*}) are not verified by this check.
40+
* Package statements and import statements (lines matching pattern
41+
* {@code ^(package|import) .*}), and are not verified by this check.
4142
* </p>
4243
* <p>
4344
* The default maximum allowable line length is 80 characters. To change the
@@ -84,8 +85,8 @@ public class LineLengthCheck extends Check {
8485
/** Default maximum number of columns in a line. */
8586
private static final int DEFAULT_MAX_COLUMNS = 80;
8687

87-
/** Pattern matching import and import static statements. */
88-
private static final Pattern IMPORT_PATTERN = Pattern.compile("^import .*");
88+
/** Patterns matching package, import, and import static statements. */
89+
private static final Pattern IGNORE_PATTERN = Pattern.compile("^(package|import) .*");
8990

9091
/** The maximum number of columns in a line. */
9192
private int max = DEFAULT_MAX_COLUMNS;
@@ -124,7 +125,7 @@ public void beginTree(DetailAST rootAST) {
124125
final int realLength = CommonUtils.lengthExpandedTabs(
125126
line, line.length(), getTabWidth());
126127

127-
if (realLength > max && !IMPORT_PATTERN.matcher(line).find()
128+
if (realLength > max && !IGNORE_PATTERN.matcher(line).find()
128129
&& !ignorePattern.matcher(line).find()) {
129130
log(i + 1, MSG_KEY, max, realLength);
130131
}

src/test/java/com/puppycrawl/tools/checkstyle/checks/sizes/LineLengthCheckTest.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,12 @@ protected String getPath(String filename) throws IOException {
3838
+ "sizes" + File.separator + filename);
3939
}
4040

41+
@Override
42+
protected String getNonCompilablePath(String filename) throws IOException {
43+
return super.getNonCompilablePath("checks" + File.separator
44+
+ "sizes" + File.separator + filename);
45+
}
46+
4147
@Test
4248
public void testGetRequiredTokens() {
4349
final LineLengthCheck checkObj = new LineLengthCheck();
@@ -89,4 +95,15 @@ public void shouldNotLogLongImportStatements() throws Exception {
8995
};
9096
verify(checkConfig, getPath("InputLongImportStatements.java"), expected);
9197
}
98+
99+
@Test
100+
public void shouldNotLogLongPackageStatements() throws Exception {
101+
final DefaultConfiguration checkConfig =
102+
createCheckConfig(LineLengthCheck.class);
103+
checkConfig.addAttribute("max", "80");
104+
final String[] expected = {
105+
"6: " + getCheckMessage(MSG_KEY, 80, 88),
106+
};
107+
verify(checkConfig, getNonCompilablePath("InputLongPackageStatement.java"), expected);
108+
}
92109
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.nameofcompany.nameofdivision.nameofproject.systemtests.parallel.areaoftest.featuretested.flowtested;
2+
3+
public class InputLongImportStatements {
4+
@Override
5+
public String toString() {
6+
return "This is very long line that should be logged because it is not package";
7+
}
8+
}

src/xdocs/config_sizes.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@
245245
property <code>tabWidth</code> for <code>LineLength</code> alone.
246246
</li>
247247
<li>
248-
Import statements (lines matching pattern <code>^import .*</code>) are not verified by
248+
Package and import statements (lines matching pattern <code>^(package|import) .*</code>) are not verified by
249249
this check.
250250
</li>
251251
</ul>

0 commit comments

Comments
 (0)