forked from checkstyle/checkstyle
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bf8ebef
commit f2e9f4d
Showing
8 changed files
with
218 additions
and
0 deletions.
There are no files selected for viewing
74 changes: 74 additions & 0 deletions
74
src/main/java/com/puppycrawl/tools/checkstyle/checks/whitespace/NoLineWrapCheck.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
//////////////////////////////////////////////////////////////////////////////// | ||
// checkstyle: Checks Java source code for adherence to a set of rules. | ||
// Copyright (C) 2001-2014 Oliver Burn | ||
// | ||
// This library is free software; you can redistribute it and/or | ||
// modify it under the terms of the GNU Lesser General Public | ||
// License as published by the Free Software Foundation; either | ||
// version 2.1 of the License, or (at your option) any later version. | ||
// | ||
// This library is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
// Lesser General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU Lesser General Public | ||
// License along with this library; if not, write to the Free Software | ||
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | ||
//////////////////////////////////////////////////////////////////////////////// | ||
package com.puppycrawl.tools.checkstyle.checks.whitespace; | ||
|
||
import com.puppycrawl.tools.checkstyle.api.Check; | ||
import com.puppycrawl.tools.checkstyle.api.DetailAST; | ||
import com.puppycrawl.tools.checkstyle.api.TokenTypes; | ||
|
||
/** | ||
* <p>Checks that chosen statements are not line-wrapped. By default this Check | ||
* restrict to wrap import and package statements, but it's possible to check | ||
* any statement. | ||
* </p> | ||
* <h4>Examples</h4> | ||
* <p class="body"> | ||
* | ||
* To configure the check only for import statements: | ||
* | ||
* </p> | ||
* <pre class="body"> | ||
* <module name="LineWrap"> | ||
* <property name="tokens" value="IMPORT"> | ||
* </module> | ||
* </pre> | ||
* | ||
* Examples of line-wrapped import and package: | ||
* <pre><code> package com.puppycrawl. | ||
* tools.checkstyle.checks; | ||
* | ||
* import com.puppycrawl.tools. | ||
* checkstyle.api.Check; | ||
* </code></pre> | ||
* | ||
* Examples without line-wrapping import and package statements: | ||
* <pre><code> package com.puppycrawl.tools.checkstyle.checks; | ||
* | ||
* import com.puppycrawl.tools.checkstyle.api.Check; | ||
* </code></pre> | ||
* | ||
* @author Max Vetrenko | ||
*/ | ||
public class NoLineWrapCheck extends Check | ||
{ | ||
|
||
@Override | ||
public int[] getDefaultTokens() | ||
{ | ||
return new int[] {TokenTypes.PACKAGE_DEF, TokenTypes.IMPORT}; | ||
} | ||
|
||
@Override | ||
public void visitToken(DetailAST aAST) | ||
{ | ||
if (aAST.getLineNo() != aAST.getLastChild().getLineNo()) { | ||
log(aAST.getLineNo(), "no.line.wrap", aAST.getText()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
src/test/java/com/puppycrawl/tools/checkstyle/checks/whitespace/NoLineWrapCheckTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
//////////////////////////////////////////////////////////////////////////////// | ||
// checkstyle: Checks Java source code for adherence to a set of rules. | ||
// Copyright (C) 2001-2014 Oliver Burn | ||
// | ||
// This library is free software; you can redistribute it and/or | ||
// modify it under the terms of the GNU Lesser General Public | ||
// License as published by the Free Software Foundation; either | ||
// version 2.1 of the License, or (at your option) any later version. | ||
// | ||
// This library is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
// Lesser General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU Lesser General Public | ||
// License along with this library; if not, write to the Free Software | ||
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | ||
//////////////////////////////////////////////////////////////////////////////// | ||
package com.puppycrawl.tools.checkstyle.checks.whitespace; | ||
|
||
import com.puppycrawl.tools.checkstyle.BaseCheckTestSupport; | ||
import com.puppycrawl.tools.checkstyle.DefaultConfiguration; | ||
import org.junit.Test; | ||
|
||
public class NoLineWrapCheckTest | ||
extends BaseCheckTestSupport | ||
{ | ||
@Test | ||
public void testGoodCase() throws Exception | ||
{ | ||
final DefaultConfiguration checkConfig = createCheckConfig(NoLineWrapCheck.class); | ||
final String[] expected = {}; | ||
verify(checkConfig, getPath("whitespace/NoLineWrapGoodInput.java"), expected); | ||
} | ||
|
||
@Test | ||
public void testBadCase() throws Exception | ||
{ | ||
final DefaultConfiguration checkConfig = createCheckConfig(NoLineWrapCheck.class); | ||
final String[] expected = { | ||
"1: package statement should not be line-wrapped.", | ||
"6: import statement should not be line-wrapped.", | ||
}; | ||
verify(checkConfig, getPath("whitespace/NoLineWrapBadInput.java"), expected); | ||
} | ||
|
||
@Test | ||
public void testBadCaseWithTokenConfig() throws Exception | ||
{ | ||
final DefaultConfiguration checkConfig = createCheckConfig(NoLineWrapCheck.class); | ||
checkConfig.addAttribute("tokens", "IMPORT"); | ||
final String[] expected = { | ||
"6: import statement should not be line-wrapped.", | ||
}; | ||
verify(checkConfig, getPath("whitespace/NoLineWrapBadInput.java"), expected); | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
src/test/resources/com/puppycrawl/tools/checkstyle/whitespace/NoLineWrapBadInput.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package com.puppycrawl.tools. | ||
checkstyle; | ||
|
||
import com.google.common.annotations.Beta; | ||
|
||
import javax.accessibility. | ||
AccessibleAttributeSequence; | ||
import java.util.concurrent.atomic.AtomicIntegerFieldUpdater; | ||
|
||
public class BadLineWrapInput { | ||
|
||
public void fooMethod() { | ||
// | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
src/test/resources/com/puppycrawl/tools/checkstyle/whitespace/NoLineWrapGoodInput.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package com.puppycrawl.tools.checkstyle; | ||
|
||
import com.google.common.annotations.Beta; | ||
|
||
import javax.accessibility.AccessibleAttributeSequence; | ||
import java.util.concurrent.atomic.AtomicIntegerFieldUpdater; | ||
|
||
public class GoodLineWrapInput { | ||
|
||
public void fooMethod() { | ||
// | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters