checkstyle icon indicating copy to clipboard operation
checkstyle copied to clipboard

CustomImportOrder check: first import which does not belong to any group causes incorrect behavior

Open ivanov-alex opened this issue 11 years ago • 12 comments

When first import in a file does not belong to any of the groups, Checksyle reports a problem with other imports, not with first one. It should be at the end of the list according to CustomImportOrder documentation)

package java.util.concurrent.locks;
// first import is not from SAME_PACKAGE
import com.puppycrawl.tools.checkstyle.Utils;
import java.util.*;
import java.util.List;

public class InputCustomImportOrderSamePackage2 {
}

Output:

Starting audit...
/home/alex/test.java:4: warning: Import statement is in the wrong order. Should be in the SAME_PACKAGE' group.
/home/alex/test.java:5: warning: Import statement is in the wrong order. Should be in the SAME_PACKAGE' group.
Audit done.

Configuration file

<?xml version="1.0"?>
<!DOCTYPE module PUBLIC "-//Puppy Crawl//DTD Check Configuration 1.3//EN" "http://www.puppycrawl.com/dtds/configuration_1_3.dtd">
<module name = "Checker">
    <property name="charset" value="UTF-8"/>
    <property name="severity" value="warning"/>
    <module name="TreeWalker">
        <module name="CustomImportOrder">
            <property name="customImportOrderRules" value="SAME_PACKAGE(2)"/>
            <property name="sortImportsInGroupAlphabetically" value="true"/>
            <property name="separateLineBetweenGroups" value="true"/>
        </module>
    </module>
</module>

ivanov-alex avatar Jun 29 '15 20:06 ivanov-alex

@ivanov-alex, do you plan to finish work on this issue?

mkordas avatar Oct 31 '15 20:10 mkordas

A bit later, slightly overloaded on work. I'm going to return to checkstyle in several weeks

ivanov-alex avatar Oct 31 '15 20:10 ivanov-alex

Incomplete PR is located at Pull #1422

rnveach avatar Jan 06 '17 03:01 rnveach

pre-assigned to @anudeepti2004

romani avatar Mar 18 '17 17:03 romani

I am on it

anudeepti2004 avatar Mar 18 '17 18:03 anudeepti2004

i am on it

vivek-0509 avatar Mar 05 '26 09:03 vivek-0509

@vivek-0509 Are you working on this issue?? If not can i take over the work?

DakshRJain737 avatar May 21 '26 11:05 DakshRJain737

@DakshRJain737 You can take over

vivek-0509 avatar May 21 '26 12:05 vivek-0509

I am on it.

DakshRJain737 avatar May 22 '26 16:05 DakshRJain737

@romani @Zopsss current design of https://github.com/checkstyle/checkstyle/blob/c5f2dbd9f891792e80c2209dc6b5dffe1da2e9bd/src/main/java/com/puppycrawl/tools/checkstyle/checks/imports/CustomImportOrderCheck.java#L363

works in a way

How first expected group is decided

String currentGroup = getFirstGroup();
int currentGroupNumber = customImportOrderRules.lastIndexOf(currentGroup);

How next expected group is decided (only when current is not NON_GROUP_RULE_GROUP(its always last) and group of current importDetail is not same as previous group

        while (customImportOrderRules.size() > nextGroupNumber + 1) {
            if (hasAnyImportInCurrentGroup(customImportOrderRules.get(nextGroupNumber))) {
                break;
            }
            nextGroupNumber++;
        }

for config

<?xml version="1.0"?>
<!DOCTYPE module PUBLIC "-//Puppy Crawl//DTD Check Configuration 1.3//EN" "http://www.puppycrawl.com/dtds/configuration_1_3.dtd">
<module name = "Checker">
    <property name="charset" value="UTF-8"/>
    <property name="severity" value="warning"/>
    <module name="TreeWalker">
        <module name="CustomImportOrder">
            <property name="customImportOrderRules" value="SAME_PACKAGE(2)"/>
            <property name="sortImportsInGroupAlphabetically" value="true"/>
            <property name="separateLineBetweenGroups" value="true"/>
        </module>
    </module>
</module>

and file

package java.util.concurrent.locks;
// first import is not from SAME_PACKAGE
import com.puppycrawl.tools.checkstyle.Utils;
import java.util.*;
import java.util.List;

public class InputCustomImportOrderSamePackage2 {
}

first import statetement is import com.puppycrawl.tools.checkstyle.Utils;

so current expected group is treated as NON_GROUP_RULE_GROUP, so there is no violation for import com.puppycrawl.tools.checkstyle.Utils;

It may be design decision how check recognize the initial group

If first group initialization changed to bestPossibleGroupAvailable which will find best group which should be treated as first based on customImportOrderRules and https://github.com/checkstyle/checkstyle/blob/c5f2dbd9f891792e80c2209dc6b5dffe1da2e9bd/src/main/java/com/puppycrawl/tools/checkstyle/checks/imports/CustomImportOrderCheck.java#L219

In this case first group for above example will be SAME_PACKAGE and violation will be

package java.util.concurrent.locks;
// first import is not from SAME_PACKAGE
import com.puppycrawl.tools.checkstyle.Utils; // violation Imports without groups should be placed at the end of the import list
import java.util.*;
import java.util.List;

public class InputCustomImportOrderSamePackage2 {
}

This is completely design decision in above example the first group can be decided as NON_GROUP_RULE_GROUP or SAME_PACKAGE and can't be both at same time something like

package java.util.concurrent.locks;
// first import is not from SAME_PACKAGE
import com.puppycrawl.tools.checkstyle.Utils; // violation 'Imports without groups should be placed at the end of the import list'
import java.util.*; // violation 'Should be in the SAME_PACKAGE' group.'
import java.util.List;  // violation 'Should be in the SAME_PACKAGE' group.'

public class InputCustomImportOrderSamePackage2 {
}

will be incorrect

Atharv3221 avatar Jun 12 '26 14:06 Atharv3221

One more similar example

atharv@atharv-IdeaPad-3-15IIL05:~/test$ cat -n Test3.java
     1	package com.puppycrawl.tools.checkstyle.checks.imports.customimportorder;
     2	
     3	import org.apache.commons.io.FileUtils;
     4	import com.puppycrawl.tools.checkstyle.checks.imports.CustomImportOrderCheck;
     5	import com.puppycrawl.tools.checkstyle.checks.imports.ImportOrderCheck;
     6	
     7	import static java.util.Collections.*;
     8	import static java.io.File.separator;
     9	
    10	import java.time.*;
    11	import javax.net.*;
    12	
    13	public class Test3 {
    14	}

atharv@atharv-IdeaPad-3-15IIL05:~/test$ cat config3.xml
<?xml version="1.0"?>
<!DOCTYPE module PUBLIC
  "-//Puppy Crawl//DTD Check Configuration 1.3//EN"
  "https://checkstyle.org/dtds/configuration_1_3.dtd">
<module name="Checker">
  <module name="TreeWalker">
    <module name="CustomImportOrder">
      <property name="customImportOrderRules"
        value="STATIC, STANDARD_JAVA_PACKAGE, THIRD_PARTY_PACKAGE"/>
      <property name="standardPackageRegExp" value="^java\."/>
    </module>
  </module>
</module>
atharv@atharv-IdeaPad-3-15IIL05:~/test$ java -jar checkstyle-13.5.0-all.jar -c config3.xml Test3.java
Starting audit...
[ERROR] /home/atharv/test/Test3.java:7:1: Import statement for 'java.util.Collections.*' is in the wrong order. Should be in the 'STATIC' group, expecting not assigned imports on this line. [CustomImportOrder]
[ERROR] /home/atharv/test/Test3.java:8:1: Import statement for 'java.io.File.separator' is in the wrong order. Should be in the 'STATIC' group, expecting not assigned imports on this line. [CustomImportOrder]
[ERROR] /home/atharv/test/Test3.java:10:1: Import statement for 'java.time.*' is in the wrong order. Should be in the 'STANDARD_JAVA_PACKAGE' group, expecting not assigned imports on this line. [CustomImportOrder]
Audit done.
Checkstyle ends with 3 errors.

Atharv3221 avatar Jun 12 '26 15:06 Atharv3221

We should stay simple in implementation, what ever user placed on top we start from it. If user got too much violations , user thinks on them and figure-out a solution . Ideally user should just configure IDE, and let IDE to auto fixes and manage imports.

Checkstyle is just CI tool that can detect that IDE settings are not set and some contributor going to make mess in imports. User get violation, think twice, fix IDE, IDE fix imports and all will be good.

romani avatar Jul 25 '26 02:07 romani