View Javadoc

1   package net.sourceforge.pmd.rules;
2   
3   import java.util.Stack;
4   
5   import net.sourceforge.pmd.AbstractRule;
6   import net.sourceforge.pmd.ast.ASTAnnotationTypeDeclaration;
7   import net.sourceforge.pmd.ast.ASTClassOrInterfaceDeclaration;
8   import net.sourceforge.pmd.ast.ASTClassOrInterfaceType;
9   import net.sourceforge.pmd.ast.ASTEnumDeclaration;
10  import net.sourceforge.pmd.ast.ASTReferenceType;
11  import net.sourceforge.pmd.ast.ASTType;
12  import net.sourceforge.pmd.ast.ASTVariableDeclarator;
13  import net.sourceforge.pmd.ast.SimpleJavaNode;
14  import net.sourceforge.pmd.ast.SimpleNode;
15  import net.sourceforge.pmd.util.NumericConstants;
16  
17  public class MoreThanOneLogger extends AbstractRule {
18      
19      private static Class log4jLogger = null;
20  
21      private static Class javaLogger = null;
22  
23      static {
24          try {
25              log4jLogger = Class.forName("org.apache.log4j.Logger");
26          } catch (Throwable t) { //NOPMD somewhat legitmate catch all
27              log4jLogger = null;
28          }
29          try {
30              javaLogger = Class.forName("java.util.logging.Logger");
31          } catch (Throwable t) { //NOPMD somewhat legitmate catch all
32              javaLogger = null;
33          }
34      }
35  
36  	private Stack<Integer> stack = new Stack<Integer>();
37  
38  	private Integer count;
39  
40  	public Object visit(ASTClassOrInterfaceDeclaration node, Object data) {
41  		return init (node, data);
42  	}	
43  
44  	public Object visit(ASTEnumDeclaration node, Object data) {
45  		return init (node, data);
46  	}	
47  
48  	public Object visit(ASTAnnotationTypeDeclaration node, Object data) {
49  		return init (node, data);
50  	}	
51  
52  	private Object init(SimpleJavaNode node, Object data) {
53  		stack.push(count);
54  		count = NumericConstants.ZERO;
55  
56  		node.childrenAccept(this, data);
57  
58  		if (count > 1) {
59  			addViolation(data, node);
60  		}
61  		count = stack.pop();
62  
63  		return data;
64  	}
65  
66  	public Object visit(ASTVariableDeclarator node, Object data) {
67  		if (count > 1) {
68  			return super.visit(node, data);
69  		}
70  		SimpleNode type = ((SimpleNode) node.jjtGetParent()).getFirstChildOfType(ASTType.class);
71  		if (type != null) {
72  			SimpleNode reftypeNode = (SimpleNode) type.jjtGetChild(0);
73  			if (reftypeNode instanceof ASTReferenceType) {
74                  SimpleNode classOrIntType = (SimpleNode) reftypeNode.jjtGetChild(0);
75                  if (classOrIntType instanceof ASTClassOrInterfaceType){
76                      Class clazzType = ((ASTClassOrInterfaceType)classOrIntType).getType();
77                      if((clazzType != null && (clazzType.equals(log4jLogger) || clazzType.equals(javaLogger))|| (clazzType == null&& "Logger".equals(classOrIntType.getImage())))) {
78                          ++count;
79                      }
80                  }
81  			}
82  		}
83  
84  		return super.visit(node, data);
85  	}
86  
87  }