Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get Checkstyle CustomImportOrder to work with IntelliJ properly?

I'm trying to get Checkstyle (via maven-checkstyle-plugin) to have my IntelliJ imports checked by using the Checkstyle CustomImportOrder module. Despite having ordered my imports according to IntelliJ's default rules, Checkstyle still says the import order is wrong.

Here's my imports (ordered according to IntelliJ rules (ctrl+o):

import org.codehaus.jackson.JsonNode;

import javax.sql.rowset.serial.SQLOutputImpl;
import java.util.ArrayList;
import java.util.List;

Here's the warning message from Checkstyle:

[WARNING] src\main\java\com\example\hej\EnKlass.java[5] (imports) CustomImportOrder: Import statement is in the wrong order. Should be in the 'SPECIAL_IMPORTS' group.
[WARNING] src\main\java\com\example\hej\EnKlass.java[6] (imports) CustomImportOrder: Import statement is in the wrong order. Should be in the 'STANDARD_JAVA_PACKAGE' group.
[WARNING] src\main\java\com\example\hej\EnKlass.java[7] (imports) CustomImportOrder: Import statement is in the wrong order. Should be in the 'STANDARD_JAVA_PACKAGE' group.

Here's my checkstyle.xml CustomImportOrder rules (as recommended for IntelliJ by the Checkstyle site):

<module name="CustomImportOrder">
    <property name="customImportOrderRules" value="THIRD_PARTY_PACKAGE###SPECIAL_IMPORTS###STANDARD_JAVA_PACKAGE###STATIC"/>
    <property name="specialImportsRegExp" value="^javax\."/>
    <property name="standardPackageRegExp" value="^java\."/>
    <property name="sortImportsInGroupAlphabetically" value="true"/>
    <property name="separateLineBetweenGroups" value="false"/>
</module>

What might be the problem here? I have been trying to switch the rules around, with no luck. I have also tried to remove/manipulate the regexes with no luck.

like image 259
Roger Avatar asked Sep 21 '15 08:09

Roger


People also ask

How do I download checkstyle plugin for IntelliJ?

Install the Checkstyle-IDEA plugin by going to File > Settings (Windows/Linux), or IntelliJ IDEA > Preferences… ​ (macOS). Select Plugins , press Browse Repository , and find the plugin. Restart the IDE to complete the installation.

How optimize import in IntelliJ?

To optimize imports in a file, you can also press Ctrl+Alt+Shift+L , select Optimize imports, and click Run.

How do I change the import order in IntelliJ?

Open the 'Settings' (or 'Preferences' in mac) window and goto Editor > Code Style > Java. Click on 'Imports' tab. In 'Import Layout' area, you can arrange the import order by selecting the import type and clicking on the arrow (see image below). Once the changes are done apply the new settings.


2 Answers

Don't split java and javax in separate groups - just let javax stand before java packages.

checkstyle.xml:

...
    <module name="CustomImportOrder">
        <property name="customImportOrderRules" value="THIRD_PARTY_PACKAGE###SPECIAL_IMPORTS###STANDARD_JAVA_PACKAGE###STATIC"/>
        <property name="sortImportsInGroupAlphabetically" value="true"/>
        <property name="standardPackageRegExp" value="^(java|javax)\."/>
        <property name="separateLineBetweenGroups" value="true"/>
    </module>
...

checkstyleSuppressions.xml

...
<!--  Let javax.* stand before java.* - that's default Idea settings  -->
<suppress checks="CustomImportOrder"
          message="Wrong lexicographical order for 'java\.[^']+' import\. Should be before 'javax\.[^']+'\."/>
...

You can also omit properties that match default values (standardPackageRegExp and separateLineBetweenGroups for today) - please check actual documentation.

like image 155
Valeriy Avatar answered Oct 10 '22 22:10

Valeriy


Default formatting in IntelliJ looks like as follows:

all other imports
<blank line>
javax.* in alphabetical order
java.* in alphabetical order
<blank line>
static imports in alphabetical order

Right now, it is not possible to sort java and javax separately without blank line in-between and that's why you have the violations.

I've raised issue on GitHub to solve that and it will require changes in Checkstyle code.

As a workaround you may add blank line between javax and java in IntelliJ IDEA configuration and then it should be easy to tune Checkstyle to work with that.

like image 24
Michal Kordas Avatar answered Oct 10 '22 22:10

Michal Kordas