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
7dc94ba
commit b8f41e2
Showing
4 changed files
with
217 additions
and
0 deletions.
There are no files selected for viewing
83 changes: 83 additions & 0 deletions
83
src/main/java/com/puppycrawl/tools/checkstyle/checks/whitespace/EmptyLineSeparatorCheck.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,83 @@ | ||
//////////////////////////////////////////////////////////////////////////////// | ||
// 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.FullIdent; | ||
import com.puppycrawl.tools.checkstyle.api.TokenTypes; | ||
|
||
/* | ||
* | ||
*/ | ||
public class EmptyLineSeparatorCheck extends Check | ||
{ | ||
|
||
@Override | ||
public int[] getDefaultTokens() | ||
{ | ||
return new int[] { | ||
TokenTypes.IMPORT, | ||
}; | ||
} | ||
|
||
@Override | ||
public void visitToken(DetailAST aAST) | ||
{ | ||
final DetailAST nextAst = aAST.getNextSibling(); | ||
|
||
if (nextAst.getType() == aAST.getType()) { | ||
|
||
final String currentImportGroup = getImportGroup(aAST); | ||
final String nextImportGroup = getImportGroup(nextAst); | ||
|
||
if (!currentImportGroup.equals(nextImportGroup) | ||
&& !hasBlankLineAfter(aAST)) | ||
{ | ||
log(nextAst.getLineNo(), | ||
"empty.line.separator", nextAst.getText()); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Checks if token have blank line after. | ||
* @param aToken token. | ||
* @return if token have blank line after. | ||
*/ | ||
private boolean hasBlankLineAfter(DetailAST aToken) | ||
{ | ||
DetailAST lastToken = aToken.getLastChild().getLastChild(); | ||
lastToken = null == lastToken ? aToken.getLastChild() : lastToken; | ||
return aToken.getNextSibling().getLineNo() - lastToken.getLineNo() > 1; | ||
} | ||
|
||
/** | ||
* Returns import group. | ||
* @param aImport import AST. | ||
* @return import group. | ||
*/ | ||
private String getImportGroup(DetailAST aImport) | ||
{ | ||
final String importPath = | ||
FullIdent.createFullIdent(aImport.getFirstChild()).getText(); | ||
return importPath.substring(0, importPath.indexOf(".")); | ||
} | ||
} |
2 changes: 2 additions & 0 deletions
2
src/main/resources/com/puppycrawl/tools/checkstyle/checks/whitespace/messages.properties
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
45 changes: 45 additions & 0 deletions
45
...t/java/com/puppycrawl/tools/checkstyle/checks/whitespace/EmptyLineSeparatorCheckTest.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,45 @@ | ||
//////////////////////////////////////////////////////////////////////////////// | ||
// 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 EmptyLineSeparatorCheckTest | ||
extends BaseCheckTestSupport | ||
{ | ||
private DefaultConfiguration mCheckConfig; | ||
|
||
@Before | ||
public void setUp() | ||
{ | ||
mCheckConfig = createCheckConfig(EmptyLineSeparatorCheck.class); | ||
} | ||
|
||
@Test | ||
public void testDefault() throws Exception | ||
{ | ||
final String[] expected = { | ||
"10: 'import' have to be separated by single line.", | ||
}; | ||
verify(mCheckConfig, getPath("whitespace/InputEmptyLineSeparatorCheck.java"), expected); | ||
} | ||
} |
87 changes: 87 additions & 0 deletions
87
...st/resources/com/puppycrawl/tools/checkstyle/whitespace/InputEmptyLineSeparatorCheck.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,87 @@ | ||
package com.puppycrawl.tools.checkstyle.whitespace; | ||
|
||
import java.io.Serializable; | ||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.concurrent.Callable; | ||
import java.util.Collections; | ||
import com.google.common.base.CharMatcher; | ||
import com.google.common.io.CharSource; | ||
//separator blank line | ||
import javax.swing.AbstractAction; | ||
//separator blank line | ||
import org.apache.commons.beanutils.locale.converters.ByteLocaleConverter; | ||
import org.apache.commons.io.FilenameUtils; | ||
//separator blank line | ||
class InputEmptyLineSeparatorCheck | ||
{ | ||
public static final double FOO_PI = 3.1415; | ||
//separator blank line | ||
private boolean flag = true; | ||
//separator blank line | ||
static { | ||
//empty static initializer | ||
} | ||
//separator blank line | ||
{ | ||
//empty instance initializer | ||
} | ||
//separator blank line | ||
/** | ||
* | ||
*/ | ||
private InputEmptyLineSeparatorCheck() | ||
{ | ||
//empty | ||
} | ||
//separator blank line | ||
public int compareTo(InputGenericWhitespaceCheck aObject) | ||
{ | ||
int number = 0; | ||
return 0; | ||
} | ||
/** | ||
* | ||
* @param task | ||
* @param result | ||
* @return | ||
*/ | ||
public static <T> Callable<T> callable(Runnable task, T result) | ||
{ | ||
return null; | ||
} | ||
//separator blank line | ||
public int getConstructor() | ||
{ | ||
return 666; | ||
} | ||
//separator blank line | ||
public <T> InputGenericWhitespaceCheck(List<T> things) | ||
{ | ||
} | ||
//separator blank line | ||
interface IntEnum { | ||
} | ||
//separator blank line | ||
class InnerClass { | ||
|
||
public static final double FOO_PI_INNER = 3.1415; | ||
//separator blank line | ||
private boolean flagInner = true; | ||
//separator blank line | ||
static { | ||
//empty static initializer | ||
} | ||
//separator blank line | ||
{ | ||
//empty instance initializer | ||
} | ||
//separator blank line | ||
private InnerClass() | ||
{ | ||
//empty | ||
} | ||
} | ||
} |