KickJava   Java API By Example, From Geeks To Geeks.

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


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.Operation;
31 import com.pavelvlasov.jsel.TypeIdentifier;
32
33 /**
34  * ER-114
35  * Declare only predefined set of exceptions in throws clause (application layer specific)
36  * @author Janos Czako
37  * @version $Revision: 1.3 $
38  */

39 public class ThrowsClauseRule extends InspectorBase implements Parameterizable {
40     
41     /**
42      * Reviews the operations, if they have items in their throws clause which
43      * is nor predefined.
44      *
45      * @param element the operation to be reviewed.
46      */

47     public void visit(Operation element) {
48         java.util.Iterator JavaDoc throwsList = element.getThrows().iterator();
49         
50         while (throwsList.hasNext()) {
51             TypeIdentifier item = (TypeIdentifier) throwsList.next();
52             try {
53                 if (!allowedThrows.contains(item.getName())) {
54                     context.reportViolation(element, new Object JavaDoc[] {item});
55                 }
56             } catch (JselException e) {
57                 context.warn(item, e);
58             }
59     }
60     }
61     
62     /**
63      * Stores the setting form the configuration for the allowed
64      * exceptions in the trows clause of the operations.
65      */

66     private java.util.Set JavaDoc allowedThrows = new java.util.HashSet JavaDoc();
67     
68     /**
69      * Configures the rule. Reads in the values of the parameter allowed-throws.
70      *
71      * @param name the name of the parameter being loaded from Hammurapi configuration
72      * @param value the value of the parameter being loaded from Hammurapi configuration
73      * @exception ConfigurationException in case of a not supported parameter
74      */

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

87     public String JavaDoc getConfigInfo() {
88         StringBuffer JavaDoc ret=new StringBuffer JavaDoc("Allowed exceptions in the thorws clauses:\n");
89         java.util.Iterator JavaDoc iter = allowedThrows.iterator();
90         while (iter.hasNext()) {
91             ret.append("allowed-throw: " + (String JavaDoc) iter.next() + "\t");
92         }
93         ret.append("\n");
94         return ret.toString();
95     }
96 }
97
Popular Tags