Lets you optimize the import declarations by either expanding or collapsing them, obsolete or unused imports are removed.
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”)
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.
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.
They avoid any class path conflicts that could break your code when a class is added to a package you import
They make dependencies explicit, so that anyone who has to read your code later knows what you meant to import and what you didn’t mean to import
They can make some compilation faster, because the compiler doesn’t have to search the whole package to identify dependencies, though this is usually not a huge deal with modern compilers
could become
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.
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.
Please note that the custom import optimization implementation only supports expanding on-demand imports
Since 1.7
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
Please note that there might be collisions that prevent collapsing when two types have the same name.
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).
The NetBeans Plug-in currently does not support import collapsing.