ImportOrderCheck. Fix separation for static imports #1398
This commit is contained in:
parent
89f4e3c8b1
commit
c09131defe
|
|
@ -337,17 +337,15 @@ public class ImportOrderCheck
|
|||
final int groupIdx = getGroupNumber(name);
|
||||
final int line = ident.getLineNo();
|
||||
|
||||
if (groupIdx > lastGroup) {
|
||||
// This check should be made more robust to handle
|
||||
// comments and imports that span more than one line.
|
||||
if (!beforeFirstImport && isAlphabeticallySortableStaticImport(isStatic)
|
||||
|| groupIdx == lastGroup) {
|
||||
doVisitTokenInSameGroup(isStatic, previous, name, line);
|
||||
}
|
||||
else if (groupIdx > lastGroup) {
|
||||
if (!beforeFirstImport && separated && line - lastImportLine < 2) {
|
||||
log(line, MSG_SEPARATION, name);
|
||||
}
|
||||
}
|
||||
else if (groupIdx == lastGroup || sortStaticImportsAlphabetically
|
||||
&& isAlphabeticallySortableStaticImport(isStatic)) {
|
||||
doVisitTokenInSameGroup(isStatic, previous, name, line);
|
||||
}
|
||||
else {
|
||||
log(line, MSG_ORDERING, name);
|
||||
}
|
||||
|
|
@ -363,12 +361,9 @@ public class ImportOrderCheck
|
|||
* @return true if static imports should be sorted alphabetically.
|
||||
*/
|
||||
private boolean isAlphabeticallySortableStaticImport(boolean isStatic) {
|
||||
boolean result = false;
|
||||
if (isStatic && (getAbstractOption() == ImportOrderOption.TOP
|
||||
|| getAbstractOption() == ImportOrderOption.BOTTOM)) {
|
||||
result = true;
|
||||
}
|
||||
return result;
|
||||
return isStatic && sortStaticImportsAlphabetically
|
||||
&& (getAbstractOption() == ImportOrderOption.TOP
|
||||
|| getAbstractOption() == ImportOrderOption.BOTTOM);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -442,4 +442,32 @@ public class ImportOrderCheckTest extends BaseCheckTestSupport {
|
|||
return astSemi;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEclipseDefaultPositive() throws Exception {
|
||||
final DefaultConfiguration checkConfig = createCheckConfig(ImportOrderCheck.class);
|
||||
checkConfig.addAttribute("groups", "java,javax,org,com");
|
||||
checkConfig.addAttribute("ordered", "true");
|
||||
checkConfig.addAttribute("separated", "true");
|
||||
checkConfig.addAttribute("option", "top");
|
||||
checkConfig.addAttribute("sortStaticImportsAlphabetically", "true");
|
||||
final String[] expected = {};
|
||||
|
||||
verify(checkConfig, getPath("imports" + File.separator + "InputImportOrder_EclipseDefaultPositive.java"), expected);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEclipseDefaultNegative() throws Exception {
|
||||
final DefaultConfiguration checkConfig = createCheckConfig(ImportOrderCheck.class);
|
||||
checkConfig.addAttribute("groups", "java,javax,org,com");
|
||||
checkConfig.addAttribute("ordered", "true");
|
||||
checkConfig.addAttribute("separated", "true");
|
||||
checkConfig.addAttribute("option", "top");
|
||||
checkConfig.addAttribute("sortStaticImportsAlphabetically", "true");
|
||||
final String[] expected = {
|
||||
"12: " + getCheckMessage(MSG_SEPARATION, "javax.swing.JComponent"),
|
||||
"17: " + getCheckMessage(MSG_ORDERING, "org.junit.Test"),
|
||||
};
|
||||
|
||||
verify(checkConfig, getPath("imports" + File.separator + "InputImportOrder_EclipseDefaultNegative.java"), expected);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,23 @@
|
|||
package com.puppycrawl.tools.checkstyle.imports;
|
||||
|
||||
import static com.puppycrawl.tools.checkstyle.checks.imports.ImportOrderCheck.MSG_ORDERING;
|
||||
import static java.awt.Button.ABORT;
|
||||
import static java.io.File.createTempFile;
|
||||
import static javax.swing.WindowConstants.*;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.awt.Button;
|
||||
import java.awt.Dialog;
|
||||
import java.io.InputStream;
|
||||
import javax.swing.JComponent;
|
||||
import javax.swing.JTable;
|
||||
|
||||
import sun.tools.java.ArrayType;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.powermock.api.mockito.PowerMockito;
|
||||
|
||||
import com.puppycrawl.tools.checkstyle.api.DetailAST;
|
||||
|
||||
public class InputImportOrder_EclipseDefaultNegative {
|
||||
}
|
||||
|
|
@ -0,0 +1,24 @@
|
|||
package com.puppycrawl.tools.checkstyle.imports;
|
||||
|
||||
import static com.puppycrawl.tools.checkstyle.checks.imports.ImportOrderCheck.MSG_ORDERING;
|
||||
import static java.awt.Button.ABORT;
|
||||
import static java.io.File.createTempFile;
|
||||
import static javax.swing.WindowConstants.*;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.awt.Button;
|
||||
import java.awt.Dialog;
|
||||
import java.io.InputStream;
|
||||
|
||||
import javax.swing.JComponent;
|
||||
import javax.swing.JTable;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.powermock.api.mockito.PowerMockito;
|
||||
|
||||
import com.puppycrawl.tools.checkstyle.api.DetailAST;
|
||||
|
||||
import sun.tools.java.ArrayType;
|
||||
|
||||
public class InputImportOrder_EclipseDefaultPositive {
|
||||
}
|
||||
Loading…
Reference in New Issue