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
c3e98e5
commit 68e4af5
Showing
5 changed files
with
234 additions
and
0 deletions.
There are no files selected for viewing
88 changes: 88 additions & 0 deletions
88
src/main/java/com/puppycrawl/tools/checkstyle/checks/whitespace/SeparatorWrapCheck.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,88 @@ | ||
//////////////////////////////////////////////////////////////////////////////// | ||
// 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 line wrapping for separators. | ||
* </p> | ||
* <p> By default the check will check the following operators: | ||
* {@link TokenTypes#TYPE_EXTENSION_AND}, | ||
* {@link TokenTypes#DOT}, | ||
* {@link TokenTypes#COMMA}, | ||
* </p> | ||
* <p> | ||
* An example of how to configure the check is: | ||
* </p> | ||
* <pre> | ||
* <module name="SeparatorWrap"/> | ||
* </pre> | ||
* <p> | ||
* Good wrapping example: | ||
* </p> | ||
* <pre><code>s | ||
* .isEmpty(); | ||
* </code></pre> | ||
* <p> | ||
* Bad wrapping example: | ||
* </p> | ||
* <pre><code>s. | ||
* isEmpty(); | ||
* </code></pre> | ||
* | ||
* @author maxvetrenko | ||
*/ | ||
public class SeparatorWrapCheck extends Check | ||
{ | ||
|
||
@Override | ||
public int[] getDefaultTokens() | ||
{ | ||
return new int[] { | ||
TokenTypes.TYPE_EXTENSION_AND, | ||
TokenTypes.DOT, | ||
TokenTypes.COMMA, | ||
}; | ||
} | ||
|
||
@Override | ||
public void visitToken(DetailAST aAST) | ||
{ | ||
final String text = aAST.getText(); | ||
final int colNo = aAST.getColumnNo(); | ||
final int lineNo = aAST.getLineNo(); | ||
final String currentLine = getLines()[lineNo - 1]; | ||
|
||
if (aAST.getType() == TokenTypes.COMMA) { | ||
if ((currentLine.substring(0, colNo).trim().length() == 0)) { | ||
log(lineNo, colNo, "line.previous", text); | ||
} | ||
} | ||
|
||
else if (currentLine.substring(colNo + text.length()) | ||
.trim().length() == 0) | ||
{ | ||
log(lineNo, colNo, "line.new", text); | ||
} | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
src/test/java/com/puppycrawl/tools/checkstyle/checks/whitespace/SeparatorWrapCheckTest.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,48 @@ | ||
//////////////////////////////////////////////////////////////////////////////// | ||
// 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.Before; | ||
import org.junit.Test; | ||
|
||
public class SeparatorWrapCheckTest | ||
extends BaseCheckTestSupport | ||
{ | ||
private DefaultConfiguration checkConfig; | ||
|
||
@Before | ||
public void setUp() | ||
{ | ||
checkConfig = createCheckConfig(SeparatorWrapCheck.class); | ||
} | ||
|
||
@Test | ||
public void testDefault() | ||
throws Exception | ||
{ | ||
final String[] expected = { | ||
"25:29: '&' should be on a new line.", | ||
"30:10: '.' should be on a new line.", | ||
"38:25: ',' should be on the previous line.", | ||
}; | ||
verify(checkConfig, getPath("whitespace/InputSeparatorWrap.java"), expected); | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
src/test/resources/com/puppycrawl/tools/checkstyle/whitespace/InputSeparatorWrap.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,47 @@ | ||
package com.puppycrawl.tools.checkstyle.whitespace; | ||
|
||
public class InputSeparatorWrap<T extends Foo | ||
& Bar> { //good wrapping | ||
public void goodCase() | ||
{ | ||
int i = 0; | ||
String s = "ffffooooString"; | ||
s | ||
.isEmpty(); //good wrapping | ||
s.isEmpty(); | ||
try { | ||
|
||
} catch (FooException | | ||
BarException e) {} | ||
foo(i, | ||
s); //good wrapping | ||
} | ||
public static void foo(int i, String s) | ||
{ | ||
|
||
} | ||
} | ||
|
||
class badCase<T extends Foo & | ||
Bar> { //bad wrapping | ||
public void goodCase() | ||
{ | ||
String s = "ffffooooString"; | ||
s. | ||
isEmpty(); //bad wrapping | ||
try { | ||
|
||
} catch (FooException | ||
| BarException e) {} | ||
|
||
foo(i | ||
,s); //bad wrapping | ||
} | ||
public static void foo(int i, String s) | ||
{ | ||
return new StringBuilder(maxLength) | ||
.append(seq, 0, truncationLength) | ||
.append(truncationIndicator) | ||
.toString(); | ||
} | ||
} |
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