KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.util.HashSet JavaDoc;
26 import java.util.Iterator JavaDoc;
27 import java.util.Set JavaDoc;
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 JavaDoc clazz) {
46         try {
47             TypeIdentifier superclass = clazz.getSuperclass();
48             if (superclass!=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 JavaDoc parentClass=(Class JavaDoc) superclass.find();
53                 if (parentClass!=null) {
54                     Set JavaDoc parentFields=new HashSet JavaDoc();
55                     Iterator JavaDoc it=parentClass.getFields().iterator();
56                     while (it.hasNext()) {
57                         Field field = (Field) it.next();
58                         String JavaDoc cPkg = clazz.getCompilationUnit().getPackage().getName();
59                         String JavaDoc sPkg = superclass.getCompilationUnit().getPackage().getName();
60                         boolean isVisible=cPkg.equals(sPkg) ? !field.getModifiers().contains("private") : (field.getModifiers().contains("public") || field.getModifiers().contains("protected"));
61                         if (isVisible && field instanceof VariableDefinition) {
62                             parentFields.add(((VariableDefinition) field).getName());
63                         }
64                     }
65                     
66                     it=clazz.getFields().iterator();
67                     while (it.hasNext()) {
68                         Field field = (Field) it.next();
69                         if (field instanceof VariableDefinition && parentFields.contains(((VariableDefinition) field).getName())) {
70                             VariableDefinition vd=(VariableDefinition) field;
71                             // TODO make names configurable
72
if (!("serialVersionUID".equals(vd.getName()) && vd.getTypeSpecification().isKindOf("long"))) {
73                                 context.reportViolation(field);
74                             }
75                         }
76                     }
77                 }
78             }
79         } catch (JselException e) {
80             context.warn(clazz, "Could not resolve parent class: "+e);
81         }
82     }
83 }
84
Popular Tags