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.LanguageElement;
30: import com.pavelvlasov.jsel.TypeDefinition;
31:
32: /**
33: * ER-094
34: * Avoid more than two levels of nested inner classes
35: * @author Pavel Vlasov
36: * @version $Revision: 1.3 $
37: */
38: public class InnerClassNestingRule extends InspectorBase implements
39: Parameterizable {
40: /**
41: * Stores the setting form the configuration for the allowed
42: * maximal nesting of the inner classes.
43: */
44: private Integer maxNesting;
45:
46: /**
47: * Reviews the type definition if it is an inner class which nesting
48: * is deaper than the configured value.
49: *
50: * @param element the type definition to be reviewed.
51: */
52: public void visit(TypeDefinition element) {
53: if (maxNesting != null) {
54: int i = 0;
55: for (LanguageElement parent = element.getParent(); parent != null
56: && parent instanceof TypeDefinition; parent = parent
57: .getParent()) {
58: i++;
59: }
60: if (i > maxNesting.intValue()) {
61: context.reportViolation(element);
62: }
63: }
64: }
65:
66: /**
67: * Configures rule. Reads in the values of the parameter max-nesting.
68: *
69: * @param name the name of the parameter being loaded from Hammurapi configuration
70: * @param value the value of the parameter being loaded from Hammurapi configuration
71: * @exception ConfigurationException in case of a not supported parameter name, or value.
72: */
73: public boolean setParameter(String name, Object parameter)
74: throws ConfigurationException {
75: if ("max-nesting".equals(name)) {
76: maxNesting = (Integer) parameter;
77: return true;
78: } else {
79: throw new ConfigurationException("Parameter '" + name
80: + "' is not supported by " + getClass().getName());
81: }
82: }
83:
84: /**
85: * Gives back the preconfigured values.
86: */
87: public String getConfigInfo() {
88: StringBuffer ret = new StringBuffer(
89: "Allowed maximum nesting for inner classes:\n");
90: ret.append("max-nesting: " + maxNesting + "\n");
91: return ret.toString();
92: }
93: }
|