Skip to content

Commit

Permalink
try fixing UseSLF4JLoggerRule.java
Browse files Browse the repository at this point in the history
Signed-off-by: Holger Friedrich <mail@holger-friedrich.de>
  • Loading branch information
holgerfriedrich committed Mar 1, 2024
1 parent aa18887 commit ae5f15d
Showing 1 changed file with 9 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public UseSLF4JLoggerRule() {
public Object visit(ASTImportDeclaration node, Object data) {
String fullImportName = node.getImportedName();
if (forbiddenLoggers.contains(fullImportName)) {
addViolation(data, node);
asCtx(data).addViolation(node);
} else if ("org.slf4j.Logger".equals(fullImportName)
|| ("org.slf4j".equals(fullImportName) && node.isImportOnDemand())) {
isSlf4jPackageImported = true;
Expand All @@ -55,17 +55,18 @@ public Object visit(ASTImportDeclaration node, Object data) {

@Override
public Object visit(ASTVariableDeclarator node, Object data) {
ASTType typeNode = node.getParent().getFirstChildOfType(ASTType.class);
ASTType typeNode = node.getParent().firstChild(ASTType.class);
if (typeNode != null) {
Node reftypeNode = typeNode.getChild(0);
// getChild(0) returns out of bounds if no child exists, getFirstChild returns null
Node reftypeNode = typeNode.getFirstChild();
if (reftypeNode instanceof ASTReferenceType) {
ASTClassOrInterfaceType classOrInterfaceType = reftypeNode
.getFirstChildOfType(ASTClassOrInterfaceType.class);
ASTClassOrInterfaceType classOrInterfaceType = reftypeNode.firstChild(ASTClassOrInterfaceType.class);
if (classOrInterfaceType != null) {
String className = classOrInterfaceType.getImage();
// getImage will now return null, not sure if getSimpleName is the correct replacement
String className = classOrInterfaceType.getSimpleName();

if (isClassNameForbidden(className)) {
addViolation(data, typeNode);
asCtx(data).addViolation(typeNode);
}
}
}
Expand All @@ -77,7 +78,7 @@ private boolean isClassNameForbidden(String className) {
if (forbiddenLoggers.contains(className)) {
return true;
}
// If the classname is Logger but org.slf4j is not in the imports,
// If the className is Logger but org.slf4j is not in the imports,
// that means the current Logger literal is not a sfl4j.Logger
return LOGGER_LITERAL.equals(className) && !isSlf4jPackageImported;
}
Expand Down

0 comments on commit ae5f15d

Please sign in to comment.