diff --git a/pmd/src/net/sourceforge/pmd/lang/java/rule/logging/MoreThanOneLoggerRule.java b/pmd/src/net/sourceforge/pmd/lang/java/rule/logging/MoreThanOneLoggerRule.java index 5b4b1441b7..5545b9eca8 100644 --- a/pmd/src/net/sourceforge/pmd/lang/java/rule/logging/MoreThanOneLoggerRule.java +++ b/pmd/src/net/sourceforge/pmd/lang/java/rule/logging/MoreThanOneLoggerRule.java @@ -15,73 +15,81 @@ import net.sourceforge.pmd.lang.java.rule.AbstractJavaRule; import net.sourceforge.pmd.util.NumericConstants; public class MoreThanOneLoggerRule extends AbstractJavaRule { - - private static Class log4jLogger = null; - private static Class javaLogger = null; + private static final Class LOG4J_LOGGER; + + private static final Class JAVA_LOGGER; static { - try { - log4jLogger = Class.forName("org.apache.log4j.Logger"); - } catch (Throwable t) { - log4jLogger = null; - } - try { - javaLogger = Class.forName("java.util.logging.Logger"); - } catch (Throwable t) { - log4jLogger = null; - } + Class c; + try { + c = Class.forName("org.apache.log4j.Logger"); + } catch (Throwable t) { + c = null; + } + LOG4J_LOGGER = c; + try { + c = Class.forName("java.util.logging.Logger"); + } catch (Throwable t) { + c = null; + } + JAVA_LOGGER = c; } - private Stack stack = new Stack(); + private Stack stack = new Stack(); - private Integer count; + private Integer count; - public Object visit(ASTClassOrInterfaceDeclaration node, Object data) { - return init (node, data); - } + @Override + public Object visit(ASTClassOrInterfaceDeclaration node, Object data) { + return init(node, data); + } - public Object visit(ASTEnumDeclaration node, Object data) { - return init (node, data); - } + @Override + public Object visit(ASTEnumDeclaration node, Object data) { + return init(node, data); + } - public Object visit(ASTAnnotationTypeDeclaration node, Object data) { - return init (node, data); - } + @Override + public Object visit(ASTAnnotationTypeDeclaration node, Object data) { + return init(node, data); + } - private Object init(JavaNode node, Object data) { - stack.push(count); - count = NumericConstants.ZERO; + private Object init(JavaNode node, Object data) { + stack.push(count); + count = NumericConstants.ZERO; - node.childrenAccept(this, data); + node.childrenAccept(this, data); - if (count > 1) { - addViolation(data, node); + if (count > 1) { + addViolation(data, node); + } + count = stack.pop(); + + return data; + } + + @Override + public Object visit(ASTVariableDeclarator node, Object data) { + if (count > 1) { + return super.visit(node, data); + } + Node type = node.jjtGetParent().getFirstChildOfType(ASTType.class); + if (type != null) { + Node reftypeNode = type.jjtGetChild(0); + if (reftypeNode instanceof ASTReferenceType) { + Node classOrIntType = reftypeNode.jjtGetChild(0); + if (classOrIntType instanceof ASTClassOrInterfaceType) { + Class clazzType = ((ASTClassOrInterfaceType) classOrIntType).getType(); + if (clazzType != null && (clazzType.equals(LOG4J_LOGGER) || clazzType.equals(JAVA_LOGGER)) + || clazzType == null && "Logger".equals(classOrIntType.getImage())) { + ++count; + } } - count = stack.pop(); - - return data; + } } - public Object visit(ASTVariableDeclarator node, Object data) { - if (count > 1) { - return super.visit(node, data); - } - Node type = node.jjtGetParent().getFirstChildOfType(ASTType.class); - if (type != null) { - Node reftypeNode = type.jjtGetChild(0); - if (reftypeNode instanceof ASTReferenceType) { - Node classOrIntType = reftypeNode.jjtGetChild(0); - if (classOrIntType instanceof ASTClassOrInterfaceType){ - Class clazzType = ((ASTClassOrInterfaceType)classOrIntType).getType(); - if((clazzType != null && (clazzType.equals(log4jLogger) || clazzType.equals(javaLogger))|| (clazzType == null&& "Logger".equals(classOrIntType.getImage())))) { - ++count; - } - } - } - } - - return super.visit(node, data); - } + return super.visit(node, data); + } }