Skip to content

Commit

Permalink
SONARJAVA-5089 S1312 should not report on interfaces
Browse files Browse the repository at this point in the history
  • Loading branch information
pauloreilly-sonarsource authored Sep 13, 2024
1 parent 2d5d294 commit 8db836f
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 3 deletions.
3 changes: 0 additions & 3 deletions its/ruling/src/test/resources/eclipse-jetty/java-S1312.json
Original file line number Diff line number Diff line change
Expand Up @@ -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
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

}

0 comments on commit 8db836f

Please sign in to comment.