Skip to content

Commit

Permalink
EmptyLineSeparatorCheck #13
Browse files Browse the repository at this point in the history
  • Loading branch information
maxvetrenko committed Jul 10, 2014
1 parent 7dc94ba commit b8f41e2
Show file tree
Hide file tree
Showing 4 changed files with 217 additions and 0 deletions.
@@ -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("."));
}
}
@@ -1,3 +1,5 @@
empty.line.separator=''{0}'' have to be separated by single line.

containsTab=Line contains a tab character.
file.containsTab=File contains tab characters (this is the first instance).

Expand Down
@@ -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);
}
}
@@ -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
}
}
}

0 comments on commit b8f41e2

Please sign in to comment.