01: /*
02: * Hammurapi
03: * Automated Java code review system.
04: * Copyright (C) 2004 Hammurapi Group
05: *
06: * This program is free software; you can redistribute it and/or modify
07: * it under the terms of the GNU General Public License as published by
08: * the Free Software Foundation; either version 2 of the License, or
09: * (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: * ER-201
35: * Discourage usage of instance variables like a, j by enforcing minimal variable name length.
36: * @author Janos Czako
37: * @version $Revision: 1.4 $
38: */
39: public class MinimalInstanceVariableLengthRule extends InspectorBase
40: 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 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 name = vd.getName();
55: if (name.length() < minLength.intValue()) {
56: context.reportViolation(vd);
57: }
58: }
59: }
60: }
61:
62: /**
63: * Stores the setting form the configuration for the minimum allowed
64: * length of the name for class variables.
65: */
66: private Integer minLength;
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 name, Object value)
76: throws ConfigurationException {
77: if ("min-length".equals(name)) {
78: minLength = (Integer) value;
79: } else {
80: throw new ConfigurationException("Parameter '" + name
81: + "' is not supported");
82: }
83: return true;
84: }
85:
86: /**
87: * Gives back the preconfigured values.
88: */
89: public String getConfigInfo() {
90: StringBuffer ret = new StringBuffer(
91: "Allowed minimal length for instance variable names:\n");
92: ret.append("length: " + minLength + "\n");
93: return ret.toString();
94: }
95: }
|