KickJava   Java API By Example, From Geeks To Geeks.

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


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.Package;
30 import com.pavelvlasov.review.SimpleSourceMarker;
31
32 /**
33  * ER-059
34  * Packages should be neither too lean nor too fat.
35  * @author Janos Czako
36  * @version $Revision: 1.3 $
37  */

38 public class FilesPerPackage extends InspectorBase implements Parameterizable {
39     
40     /**
41      * Reviews the package, if it contains more files, than the allowed max.
42      *
43      * @param element the package to be reviewed.
44      */

45     public void visit(Package JavaDoc element) {
46         SimpleSourceMarker ssm=new SimpleSourceMarker(0, 0, (element.getName().length()==0 ? "." : element.getName().replace('.','/'))+"/", null);
47         
48         int packageSize=element.getCompilationUnits().size();
49         if (maxPackageSize!=null && packageSize>maxPackageSize.intValue()) {
50             context.reportViolationEx(ssm, new Object JavaDoc[] {maxPackageSize, new Integer JavaDoc(packageSize)}, "MAX");
51         }
52
53         if (minPackageSize!=null && packageSize<minPackageSize.intValue()) {
54             context.reportViolationEx(ssm, new Object JavaDoc[] {minPackageSize, new Integer JavaDoc(packageSize)}, "MIN");
55         }
56     }
57         
58     /**
59      * Stores the setting form the configuration for the maximum allowed
60      * number of files in a package.
61      */

62     private Integer JavaDoc maxPackageSize;
63
64     /**
65      * Stores the setting form the configuration for the maximum allowed
66      * number of files in a package.
67      */

68     private Integer JavaDoc minPackageSize;
69
70     /**
71      * Configures rule. Reads in the values of the parameters containing the
72      * allowed number of files in a package.
73      *
74      * @param name the name of the parameter being loaded from Hammurapi configuration
75      * @param value the value of the parameter being loaded from Hammurapi configuration
76      * @exception ConfigurationException in case of a not supported parameter name, or value.
77      */

78     public boolean setParameter(String JavaDoc name, Object JavaDoc parameter) throws ConfigurationException {
79         if ("max-files".equals(name)) {
80             maxPackageSize = (Integer JavaDoc) parameter;
81         } else if ("min-files".equals(name)) {
82             minPackageSize = (Integer JavaDoc) parameter;
83         } else {
84             throw new ConfigurationException("Parameter '"+name+"' is not supported by "+getClass().getName());
85         }
86         return true;
87     }
88
89     /**
90      * Gives back the preconfigured values.
91      */

92     public String JavaDoc getConfigInfo() {
93         StringBuffer JavaDoc ret=new StringBuffer JavaDoc("Allowed maximum files per package:\n");
94         if (maxPackageSize!=null) {
95             ret.append("max-files: " + maxPackageSize + "\n");
96         }
97         
98         if (minPackageSize!=null) {
99             ret.append("min-files: " + minPackageSize + "\n");
100         }
101         return ret.toString();
102     }
103 }
104
Popular Tags