KickJava   Java API By Example, From Geeks To Geeks.

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


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.Code;
30
31 /**
32  * ER-048
33  * Method is too long
34  * @author Pavel Vlasov
35  * @version $Revision: 1.4 $
36  */

37 public class CodeTooLongRule extends InspectorBase implements Parameterizable {
38     
39     /**
40      * Reviews the method definitions if they are longer than the preconfigured
41      * maximum.
42      *
43      * @param element the method definition to be reviewed.
44      */

45     public void visit(Code element) {
46         int firstLine = element.getAst().getFirstToken().getLine();
47         int lastLine = element.getAst().getLastToken().getLine();
48         
49         int length = lastLine-firstLine;
50         context.addMetric(element, "Code length", length);
51         
52         if (maxLine!=null && length>maxLine.intValue()) {
53             context.reportViolation(element);
54         }
55     }
56     
57     /**
58      * Stores the setting form the configuration for the maximum allowed
59      * linenumber inside of a method.
60      */

61     private Integer JavaDoc maxLine;
62     
63     /**
64      * Configures the rule. Reads in the values of the parameter maximum-line.
65      *
66      * @param name the name of the parameter being loaded from Hammurapi configuration
67      * @param value the value of the parameter being loaded from Hammurapi configuration
68      * @exception ConfigurationException in case of a not supported parameter
69      */

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

82     public String JavaDoc getConfigInfo() {
83         if (maxLine==null) {
84             return super.getConfigInfo();
85         } else {
86             StringBuffer JavaDoc ret=new StringBuffer JavaDoc("Allowed maximum line length of the methods:\n");
87             ret.append("max-lines: " + maxLine + "\n");
88             return ret.toString();
89         }
90     }
91 }
92
Popular Tags