@@ -2,10 +2,10 @@ <h2>Why is this an issue?</h2>
22< p > Unnecessary imports refer to importing types that are not used or referenced anywhere in the code.</ p >
33< p > Although they don’t affect the runtime behavior of the application after compilation, removing them will:</ p >
44< ul >
5- < li > Improve the readability and maintainability of the code. </ li >
6- < li > Help avoid potential naming conflicts. </ li >
7- < li > Improve the build time, as the compiler has fewer lines to read and fewer types to resolve. </ li >
8- < li > Reduce the number of items the code editor will show for auto-completion, thereby showing fewer irrelevant suggestions. </ li >
5+ < li > Improve the readability and maintainability of the code.</ li >
6+ < li > Help avoid potential naming conflicts.</ li >
7+ < li > Improve the build time, as the compiler has fewer lines to read and fewer types to resolve.</ li >
8+ < li > Reduce the number of items the code editor will show for auto-completion, thereby showing fewer irrelevant suggestions.</ li >
99</ ul >
1010< h3 > Exceptions</ h3 >
1111< p > Imports for types mentioned in Javadocs are ignored.</ p >
@@ -17,13 +17,19 @@ <h4>Noncompliant code example</h4>
1717< pre data-diff-id ="1 " data-diff-type ="noncompliant ">
1818package myapp.helpers;
1919
20+ import module java.logging; // Module imports are a Java 25 feature
21+ import module java.logging; // Noncompliant - module is imported twice
22+
2023import java.io.IOException;
2124import java.nio.file.*;
2225import java.nio.file.*; // Noncompliant - package is imported twice
2326import java.lang.Runnable; // Noncompliant - java.lang is imported by default
2427
2528public class FileHelper {
29+ private static final Logger LOGGER = Logger.getLogger("FileLogger");
30+
2631 public static String readFirstLine(String filePath) throws IOException {
32+ LOGGER.log(Level.INFO, "Reading first line from: " + filePath);
2733 return Files.readAllLines(Paths.get(filePath)).get(0);
2834 }
2935}
@@ -32,23 +38,28 @@ <h4>Compliant solution</h4>
3238< pre data-diff-id ="1 " data-diff-type ="compliant ">
3339package myapp.helpers;
3440
41+ import module java.logging;
42+
3543import java.io.IOException;
3644import java.nio.file.*;
3745
3846public class FileHelper {
47+ private static final Logger LOGGER = Logger.getLogger("FileLogger");
48+
3949 public static String readFirstLine(String filePath) throws IOException {
50+ LOGGER.log(Level.INFO, "Reading first line from: " + filePath);
4051 return Files.readAllLines(Paths.get(filePath)).get(0);
4152 }
4253}
4354</ pre >
4455< h2 > Resources</ h2 >
4556< h3 > Documentation</ h3 >
4657< ul >
47- < li > < a href ="https://docs.oracle.com/javase/tutorial/java/package/usepkgs.html "> Java packages</ a > </ li >
58+ < li > < a href ="https://docs.oracle.com/javase/tutorial/java/package/usepkgs.html "> Java packages</ a > </ li >
4859</ ul >
4960< h3 > Related rules</ h3 >
5061< ul >
51- < li > {rule:java:S1144} - Unused "private" methods should be removed </ li >
52- < li > {rule:java:S1481} - Unused local variables should be removed </ li >
62+ < li > {rule:java:S1144} - Unused "private" methods should be removed</ li >
63+ < li > {rule:java:S1481} - Unused local variables should be removed</ li >
5364</ ul >
5465
0 commit comments