KickJava   Java API By Example, From Geeks To Geeks.

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


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.TypeSpecification;
31 import com.pavelvlasov.jsel.statements.Handler;
32
33 /**
34  * ER-036
35  * Catching too general exception type.
36  * @author Janos Czako
37  * @version $Revision: 1.3 $
38  */

39 public class CatchTooGeneralExceptionTypeRule
40     extends InspectorBase implements Parameterizable {
41
42     /**
43      * Reviews the exception handlers, if they catch too general exceptions.
44      *
45      * @param expHandler the handler to be reviewed.
46      */

47     public void visit(Handler expHandler) {
48         TypeSpecification ts = expHandler.getParameter().getTypeSpecification();
49         try {
50             if (tooGeneralExceptions.contains(ts.getName())) {
51                 context.reportViolation(expHandler);
52             }
53         } catch (JselException e) {
54             context.warn(expHandler, e);
55         }
56     }
57
58     /**
59      * Stores the setting form the configuration for the exceptions which
60      * are treated as to general to be in a throws claus of a method.
61      */

62     private java.util.Set JavaDoc tooGeneralExceptions = new java.util.HashSet JavaDoc();
63     
64     /**
65      * Configures the rule. Reads in the values of the parameter general-exceptions.
66      *
67      * @param name the name of the parameter being loaded from Hammurapi configuration
68      * @param value the value of the parameter being loaded from Hammurapi configuration
69      * @exception ConfigurationException in case of a not supported parameter
70      */

71     public boolean setParameter(String JavaDoc name, Object JavaDoc parameter) throws ConfigurationException {
72         if ("general-exceptions".equals(name)) {
73             tooGeneralExceptions.add(parameter.toString());
74             return true;
75         } else {
76             throw new ConfigurationException("Parameter '"+name+"' is not supported by "+getClass().getName());
77         }
78     }
79
80     /**
81      * Gives back the preconfigured values.
82      */

83     public String JavaDoc getConfigInfo() {
84         StringBuffer JavaDoc ret=new StringBuffer JavaDoc("Too general exceptions to be catched:\n");
85         java.util.Iterator JavaDoc iter = tooGeneralExceptions.iterator();
86         while (iter.hasNext()) {
87             ret.append("general-exception: " + (String JavaDoc) iter.next() + "\t");
88         }
89         ret.append("\n");
90         return ret.toString();
91     }
92 }
93
Popular Tags