KickJava   Java API By Example, From Geeks To Geeks.

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


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.LanguageElement;
30 import com.pavelvlasov.jsel.TypeDefinition;
31
32
33 /**
34  * ER-094
35  * Avoid more than two levels of nested inner classes
36  * @author Pavel Vlasov
37  * @version $Revision: 1.3 $
38  */

39 public class InnerClassNestingRule extends InspectorBase implements Parameterizable {
40     /**
41      * Stores the setting form the configuration for the allowed
42      * maximal nesting of the inner classes.
43      */

44     private Integer JavaDoc maxNesting;
45     
46     /**
47      * Reviews the type definition if it is an inner class which nesting
48      * is deaper than the configured value.
49      *
50      * @param element the type definition to be reviewed.
51      */

52     public void visit(TypeDefinition element) {
53         if (maxNesting!=null) {
54             int i=0;
55             for (LanguageElement parent=element.getParent(); parent!=null && parent instanceof TypeDefinition; parent=parent.getParent()) {
56                 i++;
57             }
58             if (i>maxNesting.intValue()) {
59                 context.reportViolation(element);
60             }
61         }
62     }
63     
64     /**
65      * Configures rule. Reads in the values of the parameter max-nesting.
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 name, or value.
70      */

71     public boolean setParameter(String JavaDoc name, Object JavaDoc parameter) throws ConfigurationException {
72         if ("max-nesting".equals(name)) {
73             maxNesting = (Integer JavaDoc) parameter;
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("Allowed maximum nesting for inner classes:\n");
85         ret.append("max-nesting: " + maxNesting + "\n");
86         return ret.toString();
87     }
88 }
89
Popular Tags