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 f57617f
Showing
8 changed files
with
299 additions
and
0 deletions.
There are no files selected for viewing
97 changes: 97 additions & 0 deletions
97
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,97 @@ | ||
//////////////////////////////////////////////////////////////////////////////// | ||
// 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 restricts wrapping import and package statements, | ||
* but it's possible to check any statement. | ||
* </p> | ||
* <h4>Examples</h4> | ||
* <p class="body"> | ||
* | ||
* Examples of line-wrapped statements (bad case): | ||
* <pre><code> package com.puppycrawl. | ||
* tools.checkstyle.checks; | ||
* | ||
* import com.puppycrawl.tools. | ||
* checkstyle.api.Check; | ||
* </code></pre> | ||
* | ||
* <p> | ||
* To configure the check to force no line-wrapping | ||
* in package and import statements (default values): | ||
* </p> | ||
* <pre class="body"> | ||
* <module name="NoLineWrap"/> | ||
* </pre> | ||
* | ||
* <p> | ||
* To configure the check to force no line-wrapping only | ||
* in import statements: | ||
* </p> | ||
* <pre class="body"> | ||
* <module name="NoLineWrap"> | ||
* <property name="tokens" value="IMPORT"> | ||
* </module> | ||
* </pre> | ||
* | ||
* Examples of not line-wrapped statements (good case): | ||
* <pre><code> package com.puppycrawl.tools.checkstyle.checks; | ||
* | ||
* import com.puppycrawl.tools.checkstyle.api.Check; | ||
* </code></pre> | ||
* | ||
* @author maxvetrenko | ||
*/ | ||
public class NoLineWrapCheck extends Check | ||
{ | ||
|
||
@Override | ||
public int[] getDefaultTokens() | ||
{ | ||
return new int[] {TokenTypes.PACKAGE_DEF, TokenTypes.IMPORT}; | ||
} | ||
|
||
@Override | ||
public int[] getAcceptableTokens() | ||
{ | ||
return new int[] { | ||
TokenTypes.IMPORT, | ||
TokenTypes.PACKAGE_DEF, | ||
TokenTypes.CLASS_DEF, | ||
TokenTypes.METHOD_DEF, | ||
TokenTypes.CTOR_DEF, | ||
TokenTypes.ENUM_DEF, | ||
TokenTypes.INTERFACE_DEF, | ||
}; | ||
} | ||
|
||
@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
61 changes: 61 additions & 0 deletions
61
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,61 @@ | ||
//////////////////////////////////////////////////////////////////////////////// | ||
// 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 testCaseWithoutLineWrapping() throws Exception | ||
{ | ||
final DefaultConfiguration checkConfig = createCheckConfig(NoLineWrapCheck.class); | ||
final String[] expected = {}; | ||
verify(checkConfig, getPath("whitespace/NoLineWrapGoodInput.java"), expected); | ||
} | ||
|
||
@Test | ||
public void testDefaultTokensLineWrapping() 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 testCustomTokensLineWrapping() | ||
throws Exception | ||
{ | ||
final DefaultConfiguration checkConfig = createCheckConfig(NoLineWrapCheck.class); | ||
checkConfig.addAttribute("tokens", "IMPORT, CLASS_DEF, METHOD_DEF, ENUM_DEF"); | ||
final String[] expected = { | ||
"6: import statement should not be line-wrapped.", | ||
"10: CLASS_DEF statement should not be line-wrapped.", | ||
"13: METHOD_DEF statement should not be line-wrapped.", | ||
"20: ENUM_DEF statement should not be line-wrapped.", | ||
}; | ||
verify(checkConfig, getPath("whitespace/NoLineWrapBadInput.java"), expected); | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
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,27 @@ | ||
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() { | ||
final int | ||
foo = 0; | ||
} | ||
} | ||
|
||
enum | ||
FooFoo { | ||
} | ||
|
||
interface | ||
InterFoo {} | ||
|
||
|
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