KickJava   Java API By Example, From Geeks To Geeks.

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


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.Field;
30 import com.pavelvlasov.jsel.TypeDefinition;
31 import com.pavelvlasov.jsel.VariableDefinition;
32
33
34 /**
35  * ER-201
36  * Discourage usage of instance variables like a, j by enforcing minimal variable name length.
37  * @author Janos Czako
38  * @version $Revision: 1.4 $
39  */

40 public class MinimalInstanceVariableLengthRule extends InspectorBase implements Parameterizable {
41     
42     /**
43      * Reviews the variable definitions, if they have clashing names
44      * with the class name.
45      *
46      * @param element the method definition to be reviewed.
47      */

48     public void visit(TypeDefinition element) {
49         java.util.Iterator JavaDoc iter = element.getFields().iterator();
50         while (iter.hasNext()) {
51             Field field = (Field) iter.next();
52             if (field instanceof VariableDefinition) {
53                 VariableDefinition vd = (VariableDefinition) field;
54                 String JavaDoc name = vd.getName();
55                 if (name.length()<minLength.intValue()) {
56                     context.reportViolation(vd);
57                 }
58             }
59         }
60     }
61     /**
62      * Stores the setting form the configuration for the minimum allowed
63      * length of the name for class variables.
64      */

65     private Integer JavaDoc minLength;
66     
67     
68     /**
69      * Configures the rule. Reads in the values of the parameters min-length.
70      *
71      * @param name the name of the parameter being loaded from Hammurapi configuration
72      * @param value the value of the parameter being loaded from Hammurapi configuration
73      * @exception ConfigurationException in case of a not supported parameter
74      */

75     public boolean setParameter(String JavaDoc name, Object JavaDoc value) throws ConfigurationException {
76         if ("min-length".equals(name)) {
77             minLength= (Integer JavaDoc) value;
78         } else {
79             throw new ConfigurationException("Parameter '"+name+"' is not supported");
80         }
81         return true;
82     }
83
84     /**
85      * Gives back the preconfigured values.
86      */

87     public String JavaDoc getConfigInfo() {
88         StringBuffer JavaDoc ret=new StringBuffer JavaDoc("Allowed minimal length for instance variable names:\n");
89         ret.append("length: " + minLength + "\n");
90         return ret.toString();
91     }
92 }
93
Popular Tags