2.6.7. Imports

Controls the handling of import declarations.

Figure 2.58. Imports settings page

Imports settings page

2.6.7.1. General

Lets you control the general imports settings.

Sort imports

Enables or disables the sorting of import declarations. Enabling this option will sort all declarations. When disabled, the declarations are printed in their original order.

2.6.7.2. Sort Order

Lets you control the sorting order and grouping behavior of import declarations.

When sorting is enabled, import declarations will be sorted according to the positions of the package names as specified in the list component. To specify the order in which the declarations should appear, you can use the Up and Down buttons.

You can add/remove package names (e.g. javax, javax.swing or com.foo.sarah) to and from the list via the Add... and Remove buttons. A dialog appears that lets you add a new grouping definition.

Note that the star (*) represents all undefined packages and cannot be removed.

Figure 2.59. Add new Grouping Definition

Add new Grouping Definition

If you want to change an existing grouping definition, you can do so by selecting the definition you want to change in the list, and pressing the Change... button. A dialog appears that lets you change an existing grouping definition. Alternatively, you can invoke the dialog by just double-clicking with the mouse on the grouping definition you want to change.

Figure 2.60. Change existing Grouping Definition

Change existing Grouping Definition

Note that the star (*) that represents all undefined packages cannot be changed (you can only the grouping depth).

Grouping

In addition to sorting, associated declarations can be grouped together to reduce complexity by packing related information into a common unit. Grouping means that associated declarations are separated by one blank line. Grouping only happens if sorting is enabled.

Via the grouping depth you can control how many parts of a package name should be considered when determine whether two import declarations are to be grouped together. Grouping only happens when all relevant parts are equal. So via the grouping depth you can effectively specify how many package name parts are relevant.

Default grouping depth

This switch lets you define the default grouping depth that should be used when no grouping depth was defined for a specific package name (see below).

To disable grouping at all, set the default grouping depth to '0'.

Example 2.720. Grouping depth == 1

Only the first part of the package name will be used to determine grouping.

import java.awt.Color;
import java.awt.Component;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

Example 2.721. Grouping depth == 2

The first two parts of the package name will be used to determine grouping.

import java.awt.Color;
import java.awt.Component;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import java.util.ArrayList;
import java.util.List;

Example 2.722. Grouping depth == 3

The first three parts of the package name will be used to determine grouping.

import java.awt.Color;
import java.awt.Component;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import java.util.ArrayList;
import java.util.List;
Group static imports

To better differ between standard import declarations and the static imports introduced in J2SE 5.0, you can control whether static imports should be grouped separately.

You can select whether static import declarations should be printed together with the standard import declarations ("Never") or placed above ("Top") or below ("Bottom") all standard import declarations.

Since 1.4

Example 2.723. Mixed static/standard import declarations

import java.awt.BorderLayout;

import javax.swing.SwingUtilities;
import static javax.swing.WindowConstants;

import z.Foo;

Example 2.724. Static import declarations placed above

import static javax.swing.WindowConstants;

import java.awt.BorderLayout;

import javax.swing.SwingUtilities;

import z.Foo;

Example 2.725. Static import declarations placed below

import java.awt.BorderLayout;

import javax.swing.SwingUtilities;

import z.Foo;

import static javax.swing.WindowConstants;

2.6.7.3. Optimization

Lets you optimize the import declarations by either expanding or collapsing them, obsolete or unused imports are removed.

Please note that you have to explicitly enable type repository services for the Ant, Console and Maven Plug-ins to activate this feature. Please refer to the documentation of the individual Plug-ins to learn how one can accomplish this (see Part II, "Plug-ins").

Expand on-demand imports

If enabled, tries to expand all on-demand import declarations.

Expanding means to resolve all on-demand import declarations (sometimes called wildcard imports) and replace them with single-type import declarations (sometimes called explicit imports) of the types that are actually used in the source file.

So the following on-demand import declaration may be expanded into two single-type import declarations that reference the needed types for this package.

Example 2.726. On-demand import declaration

import java.util.*;

Example 2.727. Single-type import declarations

import java.util.ArrayList;
import java.util.List;
Use custom implementation

The IDE Plug-ins usually leverage the build-in import optimization facility, but sometimes these may lead to problems, especially when used during a file saving operation. Here, you can enable a custom Jalopy implementation that is used by the console Plug-ins (Ant, CLI, Maven) instead. Please note that this implementation only supports import expansion!

Since 1.7

Collapse single-type imports

If enabled, tries to collapse all single-type declarations.

Collapsing means to remove all single-type imports of a given package and replace them with one on-demand import declaration.