How to have Checkstlye groups by 2 levels of package declaration?

102 Views Asked by At

I want to have my imports grouped by 2 levels of package declaration, and then separated by a line between those groups.

Here is my checkstyle config for CustomImportOrder:

<module name="CustomImportOrder">
    <property name="sortImportsInGroupAlphabetically" value="true"/>
    <property name="separateLineBetweenGroups" value="true"/>
    <property name="customImportOrderRules" value="STATIC###STANDARD_JAVA_PACKAGE###THIRD_PARTY_PACKAGE###SPECIAL_IMPORTS###SAME_PACKAGE(2)"/>
    <property name="thirdPartyPackageRegExp" value="^(com|org|io)\."/>
    <property name="specialImportsRegExp" value="^de\."/>
</module>

Here is an example that I am trying.

import static java.lang.String.format;
import static java.time.ZoneOffset.UTC;
import static java.util.Collections.emptyList;
import static java.util.stream.Collectors.toList;
import static java.util.stream.Collectors.toMap;

import java.time.OffsetDateTime;

import java.util.Arrays;              // Checkstyle complains here for extra line
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.springframework.data.util.Pair; // Checkstyle complains here for extra line
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

Here is the execution output:

-> java -Duser.language=en -Duser.country=US -jar ~/Downloads/checkstyle-10.5.0-all.jar -c checkstyle.xml TestClass.java
Starting audit...
[WARN] /home/dshakya/test/TestClass.java:11:1: Extra separation in import group before 'java.util.Arrays' [CustomImportOrder]
[WARN] /home/dshakya/test/TestClass.java:22:1: Extra separation in import group before 'org.springframework.data.util.Pair' [CustomImportOrder]

I was referring to this Documentation page, but not sure what I am missing. Can anybody point me on what configuration would work for my case?

0

There are 0 best solutions below