Checkstyle: Add ImportOrder check to default configuration on Checkstyle

Created on 22 Jul 2015  路  6Comments  路  Source: checkstyle/checkstyle

ImportOrder check is not used in default configuration on Checkstyle.
It should be added.

We should also try to minimize amount of conflicts with default configurations of formatters in most common IDEs (Eclipse, IDEA, NetBeans) or advise how formatter settings should be configured to minimize (optimally avoid) conflicts with ImportOrder.

approved

All 6 comments

That would be right. For those, who use IntelliJ IDEA, required settings (after we'll decide how to configure ImportOrder) will be:
1) Deselect optimize imports
optimize imports
2) Select Use single class import and Use fully qualified class names. Section Import Layout will help to organize the order of imports for ImportOrderCheck.
screenshot

That is also required to forbid Idea to use star imports.

"Organize Imports" in Eclipse (Kepler, Luna & Mars, other versions are likely to work the same way).
To change settings of "Organize Imports" feature, please go to Window -> Preferences.
In Preferences window select Java->Code Style->Organize Imports.
Default configuration should look as following:
EclipseDefaults
We are going to configure ImportOrder to work with default configuration of Eclipse because it works not perfect and sometimes sorts imports in a not obvious way. So we would like to minimize changes to this configuration because we suppose that changed configuration may have even greater flows.

ImportOrder configuration:

<module name="ImportOrder">
    <property name="groups" value="/^javax?\./,org"/>
    <property name="ordered" value="true"/>
    <property name="separated" value="true"/>
    <property name="option" value="top"/>
    <property name="sortStaticImportsAlphabetically" value="true"/>
</module>

Known issues:
1) it looks like Eclipse pays no attention to presence of "com" in configuration. We expect that all non-mentioned imports will go after "com", but quite often Eclipse inserts them before. That's why we don't have "com" on a list of groups.
2) Eclipse inserts imports from "nl.jqno.equalsverifier" package before "org" section. It require manual adjustment.
There is a defect in Eclipse: https://bugs.eclipse.org/bugs/show_bug.cgi?id=352891

Based on that we would recommend do not change Eclipse default configuration or reset it to default to minimize violations raised by Checkstyle.

Intellij IDEA settings:
Go to: File->Settings
then in the tree go to: Editor->Code Style->Java
open Import tab and make following settings:
IDEA settings

In "Import Layout" on the bottom configure as following.
Note: pay attention to order of static and non-static imports, people often mix them.
IDEA Import Order

Source: http://stackoverflow.com/questions/14716283/is-it-possible-for-intellij-to-organize-imports-the-same-way-as-in-eclipse

NetBeans settings:
On menu select Tools->Options
On Options page go to Editor->Formatting->Java->Imports (see image with markers) and set properties as marked in magenta rectangles.
NetBeans properties

merged.

Was this page helpful?
0 / 5 - 0 ratings