View Javadoc

1   /**
2    * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
3    */
4   package net.sourceforge.pmd.lang.java.rule.logging;
5   
6   import java.util.logging.Level;
7   
8   import net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit;
9   
10  public class GuardLogStatementJavaUtilRule extends GuardLogStatementRule {
11  
12  	private static final String GUARD_METHOD_NAME = "isLoggable";
13  
14  	private static String extendedXPath = "//PrimaryPrefix[ends-with(Name/@Image, '.log')]\n" + 
15  	        "[following-sibling::PrimarySuffix\n" + 
16  	        "    [ends-with(.//PrimaryPrefix/Name/@Image, 'LOG_LEVEL_UPPERCASE')]\n" + 
17  	        "    [count(../descendant::AdditiveExpression) > 0]\n" + 
18  	        "]\n" + 
19  	        "[count(ancestor::IfStatement/Expression/descendant::PrimaryExpression\n" + 
20  	        "    [ends-with(descendant::PrimaryPrefix[1]/Name/@Image,'GUARD')]) = 0\n" + 
21  	        "or\n" + 
22  	        "count(ancestor::IfStatement/Expression/descendant::PrimaryExpression\n" + 
23  	        "    [ends-with(descendant::PrimaryPrefix[2]/Name/@Image,'LOG_LEVEL_UPPERCASE')]) = 0]";
24  	
25  	@Override
26  	public Object visit(ASTCompilationUnit unit, Object data) {
27  	    String[] logLevels = getProperty(LOG_LEVELS);
28  	    String[] guardMethods = getProperty(GUARD_METHODS);
29  
30          if (super.guardStmtByLogLevel.isEmpty() && logLevels.length > 0 && guardMethods.length > 0) {
31              configureGuards(logLevels, guardMethods);
32          } else if ( super.guardStmtByLogLevel.isEmpty() ) {
33              configureDefaultGuards();
34          }
35  
36          findViolationForEachLogStatement(unit, data, extendedXPath);
37  		return super.visit(unit,data);
38  	}
39  
40      private void configureGuards(String[] logLevels, String[] guardMethods) {
41          String[] methods = guardMethods;
42          if (methods.length != logLevels.length) {
43              String firstMethodName = guardMethods[0];
44              methods = new String[logLevels.length];
45              for (int i = 0; i < logLevels.length; i++) {
46                  methods[i] = firstMethodName;
47              }
48          }
49          for (int i = 0; i < logLevels.length; i++) {
50              super.guardStmtByLogLevel.put("." + logLevels[i], methods[i]);
51          }
52      }
53  
54      private void configureDefaultGuards() {
55          super.guardStmtByLogLevel.put(formatLogLevelString(Level.FINEST), GUARD_METHOD_NAME);
56          super.guardStmtByLogLevel.put(formatLogLevelString(Level.FINER), GUARD_METHOD_NAME);
57          super.guardStmtByLogLevel.put(formatLogLevelString(Level.FINE), GUARD_METHOD_NAME);
58          super.guardStmtByLogLevel.put(formatLogLevelString(Level.INFO), GUARD_METHOD_NAME);
59          super.guardStmtByLogLevel.put(formatLogLevelString(Level.WARNING), GUARD_METHOD_NAME);
60          super.guardStmtByLogLevel.put(formatLogLevelString(Level.SEVERE), GUARD_METHOD_NAME);
61      }
62  
63  	private String formatLogLevelString(Level logLevel) {
64  		return "." + logLevel.toString().toLowerCase();
65  	}
66  }