KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hammurapi > inspectors > LogExceptionsRule


1 /*
2  * Hammurapi
3  * Automated Java code review system.
4  * Copyright (C) 2004 Hammurapi Group
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  *
20  * URL: http://www.hammurapi.org
21  * e-Mail: support@hammurapi.biz
22  */

23 package org.hammurapi.inspectors;
24
25 import org.hammurapi.InspectorBase;
26
27 import com.pavelvlasov.config.ConfigurationException;
28 import com.pavelvlasov.config.Parameterizable;
29 import com.pavelvlasov.jsel.JselException;
30 import com.pavelvlasov.jsel.JselRuntimeException;
31 import com.pavelvlasov.jsel.LanguageElement;
32 import com.pavelvlasov.jsel.OperationInfo;
33 import com.pavelvlasov.jsel.expressions.MethodCall;
34 import com.pavelvlasov.jsel.statements.Handler;
35 import com.pavelvlasov.review.SourceMarker;
36 import com.pavelvlasov.util.Visitor;
37
38
39 /**
40  * ER-120
41  * Catch-blocks should log the exeption with Log4J.fatal( "Context String" , exception )
42  * @author Pavel Vlasov
43  * @version $Revision: 1.6 $
44  */

45 public class LogExceptionsRule extends InspectorBase implements Parameterizable {
46
47     /**
48      * Reviews the exception handlers if they violate against the rule
49      *
50      * @param handler the exception handler
51      */

52     public void visit(Handler handler) {
53         class LoggedException extends com.pavelvlasov.RuntimeException {
54
55             /**
56              * Comment for <code>serialVersionUID</code>
57              */

58             private static final long serialVersionUID = -1481467503705295388L;
59                 
60         };
61
62         try {
63             handler.accept(new Visitor() {
64                 public boolean visit(Object JavaDoc target) {
65                     if (target instanceof MethodCall) {
66                         MethodCall mc = (MethodCall) target;
67                         try {
68                             OperationInfo operationInfo = mc.getProvider();
69                             if (operationInfo==null) {
70                                 context.warn((SourceMarker) mc, "Provider is null for "+mc+" at "+((LanguageElement) mc).getLocation());
71                             } else if (loggerMethods.contains(operationInfo.getName()) && operationInfo.getDeclaringType().isKindOf(loggerClass) && operationInfo.getParameterTypes().length>1) {
72                                 throw new LoggedException();
73                             }
74                         } catch (JselException e) {
75                             context.warn((SourceMarker) mc, e);
76                         } catch (JselRuntimeException e) {
77                             context.warn((SourceMarker) mc, e);
78                         }
79                     }
80                     return true;
81                 }
82             });
83             context.reportViolation((SourceMarker) handler);
84         } catch (LoggedException e) {
85             // OK exception has been logged
86
}
87     }
88
89     /**
90      * Stores the setting form the configuration for the type of the logger class
91      */

92     private String JavaDoc loggerClass;
93
94     /**
95      * Stores the setting form the configuration for the logger class methods
96      */

97     private java.util.Set JavaDoc loggerMethods = new java.util.HashSet JavaDoc();
98     
99     /**
100      * Configures the rule. Reads in the values of the parameters logger_class and
101      * logger_method.
102      *
103      * @param name the name of the parameter being loaded from Hammurapi configuration
104      * @param value the value of the parameter being loaded from Hammurapi configuration
105      * @exception ConfigurationException in case of a not supported parameter
106      */

107     public boolean setParameter(String JavaDoc name, Object JavaDoc value) throws ConfigurationException {
108         if ("logger_class".equals(name)) {
109             loggerClass= value.toString();
110         } else if ("logger_method".equals(name)) {
111             loggerMethods.add(value.toString());
112         } else {
113             throw new ConfigurationException("Parameter '"+name+"' is not supported");
114         }
115         return true;
116     }
117     
118     /**
119      * Gives back the preconfigured values.
120      */

121     public String JavaDoc getConfigInfo() {
122         StringBuffer JavaDoc ret=new StringBuffer JavaDoc("Used logger class: " + loggerClass + "\n");
123         java.util.Iterator JavaDoc iter = loggerMethods.iterator();
124         while (iter.hasNext()) {
125             ret.append("operation: " + iter.next().toString() + "\t");
126         }
127         ret.append("\n");
128         return ret.toString();
129     }
130 }
131
Popular Tags