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 java.util.HashSet;
26: import java.util.Iterator;
27: import java.util.Set;
28:
29: import org.hammurapi.InspectorBase;
30:
31: import com.pavelvlasov.jsel.Class;
32: import com.pavelvlasov.jsel.Field;
33: import com.pavelvlasov.jsel.JselException;
34: import com.pavelvlasov.jsel.TypeIdentifier;
35: import com.pavelvlasov.jsel.VariableDefinition;
36:
37: /**
38: * ER-092
39: * Avoid hiding inherited instance fields
40: * @author Pavel Vlasov
41: * @version $Revision: 1.4 $
42: */
43: public class HidingInheritedFieldsRule extends InspectorBase {
44:
45: public void visit(Class clazz) {
46: try {
47: TypeIdentifier super class = clazz.getSuperclass();
48: if (super class != null) {
49: // TODO Need to check superclass and implemented interfaces and their
50: // superclasses. Use scopes for that. Create a list of superscopes and
51: // for each field in the class check if it is found in one of superscopes
52: Class parentClass = (Class) super class.find();
53: if (parentClass != null) {
54: Set parentFields = new HashSet();
55: Iterator it = parentClass.getFields().iterator();
56: while (it.hasNext()) {
57: Field field = (Field) it.next();
58: String cPkg = clazz.getCompilationUnit()
59: .getPackage().getName();
60: String sPkg = super class.getCompilationUnit()
61: .getPackage().getName();
62: boolean isVisible = cPkg.equals(sPkg) ? !field
63: .getModifiers().contains("private")
64: : (field.getModifiers().contains(
65: "public") || field
66: .getModifiers().contains(
67: "protected"));
68: if (isVisible
69: && field instanceof VariableDefinition) {
70: parentFields
71: .add(((VariableDefinition) field)
72: .getName());
73: }
74: }
75:
76: it = clazz.getFields().iterator();
77: while (it.hasNext()) {
78: Field field = (Field) it.next();
79: if (field instanceof VariableDefinition
80: && parentFields
81: .contains(((VariableDefinition) field)
82: .getName())) {
83: VariableDefinition vd = (VariableDefinition) field;
84: // TODO make names configurable
85: if (!("serialVersionUID".equals(vd
86: .getName()) && vd
87: .getTypeSpecification().isKindOf(
88: "long"))) {
89: context.reportViolation(field);
90: }
91: }
92: }
93: }
94: }
95: } catch (JselException e) {
96: context.warn(clazz, "Could not resolve parent class: " + e);
97: }
98: }
99: }
|