Is there a Sun command line parameter to provide warnings (or errors) in reference to unused import statements? The embedded eclipse javac compiler provides such warnings, but if the Sun / Oracle compiler has it as one of their -Xlint:XXX arguments, it is not well documented.
I’m looking to clean up an existing Java code base, which builds from the command line using Ant, and I would like integrate tracking and reporting of such statements into the nightly builds.
Some have suggested that imports have no effect on the compilation process, however looking at the compiler operations (with the -verbose flag) indicates that the compiler loads the imported classes unconditionally, even if they are not used in the written output. So, removing unused imports seems to have more benefits than just code comprehension at a glance.
Use checkstyle, and configure the
UnusedImportsmodule to be used (or pick a default configuration that already uses it).Checkstyle defines an unused import as:
java.langpackage.In practice, it creates a report that
java.awt.Componentis not used (along with the line number).It does have some limitations, in the sense it can be confused by members with the same name as an import; but, this failure rarely finds it’s way into practice for most developers.
From the documentation as to the limitation
Would not flag
Componentas unused.