Since Checkstyle 3.0
import
java.io.*;
. Most IDE's provide very sophisticated checks
for imports that handle wild-card imports.
java.lang
package. For example importing java.lang.String
.
java.util.List
would be
considered referenced with the Javadoc comment
{@link List}
. The alternative to avoid introducing a
compile-time dependency would be to write the Javadoc comment as
{@link java.util.List}
.
The main limitation of this check is handling the cases where:
name | description | type | default value | since |
---|---|---|---|---|
processJavadoc | Control whether to process Javadoc comments. | boolean | true |
5.4 |
To configure the check:
<module name="Checker"> <module name="TreeWalker"> <module name="UnusedImports"/> </module> </module>
Example:
// limitation as it match field name in code import java.awt.Component; //OK // no ability to recognize what import is not used import static java.util.Map.copyOf; //OK import static java.util.Arrays.copyOf; //OK import java.lang.String; // violation 'Unused import - java.lang.String.' import java.util.Stack; import java.util.Map; // violation 'Unused import - java.util.Map.' import java.util.List; import java.util.function.Function; import static java.lang.Integer.parseInt; // violation 'Unused import - java.lang.Integer.parseInt.' /** * {@link List} */ class Example1{ Stack stack = new Stack(); private Object Component; int[] arr = {0,0}; int[] array = copyOf(arr , 1); Function <String, Integer> stringToInteger = Integer::parseInt; }
To configure the check so that it ignores the imports referenced in Javadoc comments:
<module name="Checker"> <module name="TreeWalker"> <module name="UnusedImports"> <property name="processJavadoc" value="false"/> </module> </module> </module>
Example:
// limitation as it match field name in code import java.awt.Component; //OK // no ability to recognize what import is not used import static java.util.Map.copyOf; //OK import static java.util.Arrays.copyOf; //OK import java.lang.String; // violation 'Unused import - java.lang.String.' import java.util.Stack; import java.util.Map; // violation 'Unused import - java.util.Map.' import java.util.List; // violation 'Unused import - java.util.List.' import java.util.function.Function; import static java.lang.Integer.parseInt; // violation 'Unused import - java.lang.Integer.parseInt.' /** * {@link List} */ class Example2{ Stack stack = new Stack(); private Object Component; int[] arr = {0,0}; int[] array = copyOf(arr , 1); Function <String, Integer> stringToInteger = Integer::parseInt; }
All messages can be customized if the default message doesn't suit you. Please see the documentation to learn how to.
com.puppycrawl.tools.checkstyle.checks.imports