From 8db836f25d22085efe957c0d3b19b439948ceaaa Mon Sep 17 00:00:00 2001 From: Paul O'Reilly Date: Fri, 13 Sep 2024 16:40:01 +0100 Subject: [PATCH] SONARJAVA-5089 S1312 should not report on interfaces --- .../src/test/resources/eclipse-jetty/java-S1312.json | 3 --- .../org/sonar/java/checks/LoggersDeclarationCheck.java | 10 ++++++++++ .../files/checks/LoggersDeclarationCheckInterface.java | 6 ++++++ .../sonar/java/checks/LoggersDeclarationCheckTest.java | 10 ++++++++++ 4 files changed, 26 insertions(+), 3 deletions(-) create mode 100644 java-checks/src/test/files/checks/LoggersDeclarationCheckInterface.java diff --git a/its/ruling/src/test/resources/eclipse-jetty/java-S1312.json b/its/ruling/src/test/resources/eclipse-jetty/java-S1312.json index b09dd65497f..fed5e3a3848 100644 --- a/its/ruling/src/test/resources/eclipse-jetty/java-S1312.json +++ b/its/ruling/src/test/resources/eclipse-jetty/java-S1312.json @@ -59,9 +59,6 @@ "org.eclipse.jetty:jetty-project:jetty-util/src/main/java/org/eclipse/jetty/util/Utf8Appendable.java": [ 52 ], -"org.eclipse.jetty:jetty-project:jetty-util/src/main/java/org/eclipse/jetty/util/component/Graceful.java": [ -127 -], "org.eclipse.jetty:jetty-project:jetty-util/src/main/java/org/eclipse/jetty/util/log/Slf4jLogger.java": [ 26 ], diff --git a/java-checks/src/main/java/org/sonar/java/checks/LoggersDeclarationCheck.java b/java-checks/src/main/java/org/sonar/java/checks/LoggersDeclarationCheck.java index fb178612406..fd5a1ca6524 100644 --- a/java-checks/src/main/java/org/sonar/java/checks/LoggersDeclarationCheck.java +++ b/java-checks/src/main/java/org/sonar/java/checks/LoggersDeclarationCheck.java @@ -25,6 +25,7 @@ import org.sonar.plugins.java.api.JavaFileScanner; import org.sonar.plugins.java.api.JavaFileScannerContext; import org.sonar.plugins.java.api.tree.BaseTreeVisitor; +import org.sonar.plugins.java.api.tree.ClassTree; import org.sonar.plugins.java.api.tree.IdentifierTree; import org.sonar.plugins.java.api.tree.MemberSelectExpressionTree; import org.sonar.plugins.java.api.tree.MethodTree; @@ -68,6 +69,15 @@ private boolean isValidLoggerName(String name) { return pattern.matcher(name).matches(); } + @Override + public void visitClass(ClassTree tree) { + // don't report on interfaces, since fields cannot be private and static final is redundant + if (tree.kind().equals(Tree.Kind.INTERFACE)) { + return; + } + super.visitClass(tree); + } + @Override public void visitMethod(MethodTree tree) { // only scan body of the method and avoid looking at parameters diff --git a/java-checks/src/test/files/checks/LoggersDeclarationCheckInterface.java b/java-checks/src/test/files/checks/LoggersDeclarationCheckInterface.java new file mode 100644 index 00000000000..d5f8db13448 --- /dev/null +++ b/java-checks/src/test/files/checks/LoggersDeclarationCheckInterface.java @@ -0,0 +1,6 @@ +public interface Graceful { + static void shutdown(Object component) { + // test no FP raised on interface + Logger LOG = LoggerFactory.getLogger(component.getClass()); // Compliant + } +} diff --git a/java-checks/src/test/java/org/sonar/java/checks/LoggersDeclarationCheckTest.java b/java-checks/src/test/java/org/sonar/java/checks/LoggersDeclarationCheckTest.java index 81f5e084fa3..b868b1e8640 100644 --- a/java-checks/src/test/java/org/sonar/java/checks/LoggersDeclarationCheckTest.java +++ b/java-checks/src/test/java/org/sonar/java/checks/LoggersDeclarationCheckTest.java @@ -42,4 +42,14 @@ void custom() { .verifyIssues(); } + @Test + void test_interface() { + LoggersDeclarationCheck check = new LoggersDeclarationCheck(); + check.format = ".*"; + CheckVerifier.newVerifier() + .onFile("src/test/files/checks/LoggersDeclarationCheckInterface.java") + .withCheck(check) + .verifyNoIssues(); + } + }