KickJava   Java API By Example, From Geeks To Geeks.

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


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.Scope;
30 import com.pavelvlasov.jsel.TypeBody;
31 import com.pavelvlasov.jsel.statements.CompoundStatement;
32 import com.pavelvlasov.jsel.statements.ForStatement;
33 import com.pavelvlasov.jsel.statements.WhileStatement;
34 import com.pavelvlasov.review.SourceMarker;
35
36 /**
37  * ER-030
38  * Logical nesting limit exceeded
39  * @author Janos Czako
40  * @version $Revision: 1.7 $
41  */

42 public class LogicalNestingRule extends InspectorBase implements Parameterizable {
43     
44     /**
45      * Reviews the statements, if their nesting is too deep.
46      *
47      * @param element the statement to be reviewed.
48      */

49     public void visit(CompoundStatement element) {
50         if (!isEmpty(element)) {
51 // Collection levels=new ArrayList();
52
Scope prevScope=null;
53             int actNesting = -1;
54             for (Scope enclosingScope = element.getEnclosingScope(); !(enclosingScope==null || enclosingScope instanceof TypeBody); enclosingScope=enclosingScope.getEnclosingScope()) {
55                 if (!((enclosingScope instanceof ForStatement || enclosingScope instanceof WhileStatement) && prevScope instanceof CompoundStatement)) {
56                     actNesting++;
57 // levels.add(enclosingScope);
58
}
59                 prevScope=enclosingScope;
60             }
61             
62             if (actNesting>maxNesting.intValue()) {
63                 context.reportViolation((SourceMarker) element, new Object JavaDoc[] {maxNesting, new Integer JavaDoc(actNesting)});
64 // Iterator it=levels.iterator();
65
// while (it.hasNext()) {
66
// LanguageElement le=(LanguageElement) it.next();
67
// System.out.println("***\t"+le.getClass()+" "+le.getLocation());
68
// }
69
}
70         }
71     }
72
73     /**
74      * Stores the setting form the configuration for the allowed
75      * maximal logical nesting.
76      */

77     private Integer JavaDoc maxNesting;
78
79     
80     /**
81      * Configures rule. Reads in the values of the parameter max-nesting.
82      *
83      * @param name the name of the parameter being loaded from Hammurapi configuration
84      * @param value the value of the parameter being loaded from Hammurapi configuration
85      * @exception ConfigurationException in case of a not supported parameter name, or value.
86      */

87     public boolean setParameter(String JavaDoc name, Object JavaDoc parameter) throws ConfigurationException {
88         if ("max-nesting".equals(name)) {
89             maxNesting = (Integer JavaDoc) parameter;
90         } else {
91             throw new ConfigurationException("Parameter '"+name+"' is not supported by "+getClass().getName());
92         }
93         return true;
94     }
95
96     /**
97      * Gives back the preconfigured values.
98      */

99     public String JavaDoc getConfigInfo() {
100         if (maxNesting!=null) {
101             StringBuffer JavaDoc ret=new StringBuffer JavaDoc("Allowed maximum logical nesting:\n");
102             ret.append("max-nesting: " + maxNesting + "\n");
103             return ret.toString();
104         } else {
105             return "";
106         }
107     }
108 }
109
Popular Tags