KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Hammurapi
3  * Automated Java code review system.
4  * Copyright (C) 2004 Pavel Vlasov
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: vlasov@pavelvlasov.com
22
23  */

24 package org.hammurapi.inspectors;
25
26 import org.hammurapi.InspectorBase;
27
28 import com.pavelvlasov.config.ConfigurationException;
29 import com.pavelvlasov.config.Parameterizable;
30 import com.pavelvlasov.jsel.Operation;
31
32 /**
33  * Cyclomatic complexity exceeds specified maximum.
34  * @author Pavel Vlasov
35  * @version $Revision: 1.5 $
36  */

37 public class CyclomaticComplexityRule extends InspectorBase implements Parameterizable {
38     /**
39      * Stores the setting form the configuration for the maximum allowed
40      * complexity for an operation.
41      */

42     private Integer JavaDoc operationMaxComplexity;
43     /**
44      * Stores the setting form the configuration for the maximum allowed
45      * complexity for a class.
46      */

47     private Integer JavaDoc classMaxComplexity;
48
49     //Anu 24th May 05 :added for setting the upper limit for ER-011(Yellow) inspector
50
/**
51      * Stores the setting form the configuration for the maximum allowed
52      * complexity for the operation ER-011 (Red) .This is the upper range check for
53      * ER-011(Yellow).
54      */

55     
56     private Integer JavaDoc operationMaxRedComplexity;
57     /**
58      * Stores the setting form the configuration for the maximum allowed
59      * complexity for a class ER-011 (Red) .This is the upper range check for
60      * ER-011(Yellow).
61      */

62     private Integer JavaDoc classMaxRedComplexity;
63
64     //Anu 24th May 05 :End
65
/**
66      * Reviews the methods and calculates their complexity.
67      *
68      * @param operation the method to be reviewed.
69      */

70     public void visit(Operation operation) {
71         int complexity=operation.getComplexity();
72         context.addMetric(operation, "Operation complexity", complexity);
73         //Anu 24th May 05 :added check for the upper limit for ER-011(Yellow) inspector
74
if(operationMaxRedComplexity!=null ){
75             if (operationMaxComplexity!=null && complexity>operationMaxComplexity.intValue() &&
76                 complexity < operationMaxRedComplexity.intValue() ) {
77                 context.reportViolation(operation, new Object JavaDoc[] {operationMaxComplexity, new Integer JavaDoc(complexity)});
78             }
79         }
80         else {
81             if (operationMaxComplexity!=null && complexity>operationMaxComplexity.intValue()) {
82                 context.reportViolation(operation, new Object JavaDoc[] {operationMaxComplexity, new Integer JavaDoc(complexity)});
83             }
84         }
85     }
86     
87     /**
88      * Reviews the class definition and calculates its complexity.
89      *
90      * @param clazz the class definition to be reviewed.
91      */

92     public void visit(com.pavelvlasov.jsel.Class clazz) {
93         int complexity=clazz.getComplexity();
94         context.addMetric(clazz, "Class complexity", complexity);
95         //Anu 24th May 05 :added check for the upper limit for ER-011(Yellow) inspector
96
if(classMaxRedComplexity!=null ){
97             if (classMaxComplexity!=null && complexity>classMaxComplexity.intValue() &&
98                 complexity < classMaxRedComplexity.intValue() ) {
99                 context.reportViolation(clazz, new Object JavaDoc[] {classMaxComplexity, new Integer JavaDoc(complexity)});
100             }
101         }
102         else {
103             if (classMaxComplexity!=null && complexity>classMaxComplexity.intValue()) {
104                 context.reportViolation(clazz, new Object JavaDoc[] {classMaxComplexity, new Integer JavaDoc(complexity)});
105              }
106        }
107     }
108     
109     /**
110      * Configures the rule. Reads in the values of the parameters operation-max-complexity and
111      * class-max-complexity.
112      *
113      * @param name the name of the parameter being loaded from Hammurapi configuration
114      * @param value the value of the parameter being loaded from Hammurapi configuration
115      * @exception ConfigurationException in case of a not supported parameter
116      */

117     public boolean setParameter(String JavaDoc name, Object JavaDoc value) throws ConfigurationException {
118         if ("operation-max-complexity".equals(name)) {
119             operationMaxComplexity= (Integer JavaDoc) value;
120         } else if ("class-max-complexity".equals(name)) {
121             classMaxComplexity= (Integer JavaDoc) value;
122 // Anu 24th May 05 :added check for the upper limit for ER-011(Yellow) inspector
123
} else if ("class-max-redcomplexity".equals(name)) {
124             classMaxRedComplexity= (Integer JavaDoc) value;
125         } else if ("operation-max-redcomplexity".equals(name)) {
126             operationMaxRedComplexity= (Integer JavaDoc) value;
127         } else {
128             throw new ConfigurationException("Parameter '"+name+"' is not supported");
129         }
130 // Anu 24th May 05 :added check for the upper limit for ER-011(Yellow) inspector should
131
// be more than the max limit
132
if(operationMaxRedComplexity!=null && operationMaxComplexity!=null)
133         {
134             if (operationMaxRedComplexity.intValue()< operationMaxComplexity.intValue() ){
135                 throw new ConfigurationException(
136                 "operationMaxRedComplexity should be higher than operationMaxComplexity" );
137             }
138         }
139         if(classMaxRedComplexity!=null && classMaxComplexity!=null)
140         {
141             if (classMaxRedComplexity.intValue()< classMaxComplexity.intValue() ){
142                 throw new ConfigurationException(
143                 "classMaxRedComplexity should be higher than classMaxComplexity" );
144             }
145         }
146         return true;
147     }
148
149     /**
150      * Gives back the preconfigured values.
151      */

152     public String JavaDoc getConfigInfo() {
153         StringBuffer JavaDoc ret=new StringBuffer JavaDoc("Allowed cyclematic complexity:\n");
154         ret.append("operation: " + operationMaxComplexity + "\t");
155         ret.append("class: " + classMaxComplexity + "\n");
156         return ret.toString();
157     }
158 }
159
Popular Tags