Javac configuration options for unused include warnings?

Is there a Sun command-line option to provide warnings (or errors) regarding unused import statements? The eclipse javac built-in compiler provides such warnings, but if the Sun / Oracle compiler has it as one of its arguments, -Xlint:XXX , it is poorly documented.

I want to clear an existing Java codebase that builds from the command line using Ant, and I would like to integrate tracking and reporting of such statements into nightly builds.

Some have suggested that import does not affect the compilation process, however, viewing 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. Thus, removing unused imports seems to have more advantages than just understanding the code at a glance.

+4
source share
2 answers

Use checkstyle and configure the UnusedImports module to be used (or select the default configuration that already uses it).

Checkstyle defines unused imports as:

  • Import without wildcards that is not specified in the file.
  • An import that is a duplicate of another import.
  • Import from java.lang .
  • Import from the same package as the class.
  • (optional) import, which is only needed to resolve the JavaDoc link.

In practice, a report is created in which java.awt.Component not used (along with the line number).

 import java.awt.Component; class FooBar { ... } 

It has some limitations, in the sense that they can be confused by members with the same name as the import; but this error rarely finds its practice for most developers.

From the documentation regarding the restriction

 import java.awt.Component; class FooBar { private Object Component; // IMHO bad practice ... } 

You cannot use the Component flag.

+1
source

I recommend that you take a look at FindBugs instead of reporting code cleanliness as part of your nightly compilations. FindBugs can detect much more potential problems than the JDK Java compiler or the one in Eclipse.

+2
source

Source: https://habr.com/ru/post/1346796/


All Articles