Skip to content

Improve violation message of ImportOrder #8315

@romani

Description

@romani

Check documentation:
https://checkstyle.org/config_imports.html#CustomImportOrder
https://checkstyle.org/config_imports.html#ImportOrder

detected at #8241 (comment)

$ cat config.xml 
<?xml version="1.0"?>
<!DOCTYPE module PUBLIC
        "-//Checkstyle//DTD Checkstyle Configuration 1.3//EN"
        "https://checkstyle.org/dtds/configuration_1_3.dtd">
<module name="Checker">
    <module name="TreeWalker">
      <module name="ImportOrder">
        <property name="sortStaticImportsAlphabetically" value="true"/>
        <property name="option" value="top"/>
      </module>
      <module name="CustomImportOrder">
        <property name="customImportOrderRules"
          value="STATIC###STANDARD_JAVA_PACKAGE###SPECIAL_IMPORTS"/>
        <property name="specialImportsRegExp" value="^org\."/>
        <property name="sortImportsInGroupAlphabetically" value="true"/>
        <property name="separateLineBetweenGroups" value="true"/>
      </module>
    </module>
</module>

$ cat Test.java 
import static java.lang.Math.abs; // violation
import static java.lang.Math.PI;

public class SomeClass { }

$ java $RUN_LOCALE -jar /var/tmp/checkstyle-8.33-all.jar -c config.xml Test.java
Starting audit...
[ERROR] /var/tmp/Test.java:2:1: Wrong lexicographical order for 'java.lang.Math.PI' import.
 Should be before 'java.lang.Math.abs'. [CustomImportOrder]
[ERROR] /var/tmp/Test.java:2:1: Wrong order for 'java.lang.Math.PI' import. [ImportOrder]
Audit done.
Checkstyle ends with 2 errors.

ImportOrder message is very obscure. Should be close/similar to CustomImportOrder message.

Migration notes:
any suppression that uses message content should be updated, to use new wording.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions