copy/paste error in MoreThanOneLogger
code cleanup for static fields initialization git-svn-id: https://pmd.svn.sourceforge.net/svnroot/pmd/trunk@6076 51baf565-9d33-0410-a72c-fc3788e3496d
This commit is contained in:
1 parent
6f9b9045f0
commit
f72ca9cf8d
1 file changed
+61
-53
@@ -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<Integer> stack = new Stack<Integer>();
|
||||
private Stack<Integer> stack = new Stack<Integer>();
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user