Optimization

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

Note

When using either one of the Ant, Console or Maven Plug-ins, you have to explicitly configure the class path for this feature to work. 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

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

Tip

Sorting (see the section called “Sort imports”) should be enabled when different IDEs are used, as otherwise implementation details may yield to differences regarding import placement.

Single-type imports have several advantages and should be preferred over on-demand imports.

Example 2.744. On-demand import declaration

import java.util.*;

could become

Example 2.745. Single-type import declarations

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

In the examples above, the on-demand import declaration has been expanded into two single-type import declarations that reference the needed types for this package.

Use custom implementation

The IDE Plug-ins usually leverage the build-in import optimization facility, but sometimes these may contain bugs that prevent their usage. Here, you can enable a custom implementation instead. It’s the same implementation that is used by the non-IDE Plug-ins.

Note

Please note that the custom import optimization implementation only supports expanding on-demand imports

Since 1.7

Collapse single-type imports

When 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.

Example 2.746. Single-type import declarations

import java.awt.event.MouseEvent;
import javax.swing.JButton;
import javax.swing.JTable;
import javax.swing.JTextField;

could become

Example 2.747. On-demand import declarations

import java.awt.event.*;
import javax.swing.*;

Please note that there might be collisions that prevent collapsing when two types have the same name.

Example 2.748. Single-type import declarations

import java.awt.Color;
import java.util.List;


In the example above, collapsing both packages is not possible because this would lead to invalid code, as both java.awt and java.util contain a type named List. In such cases only one package will be collapsed (if no further conflicts are detected).

Note

The NetBeans Plug-in currently does not support import collapsing.