KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > puppycrawl > tools > checkstyle > bcel > checks > HiddenInheritedFieldCheck


1 //Tested with BCEL-5.1
2
//http://jakarta.apache.org/builds/jakarta-bcel/release/v5.1/
3

4 package com.puppycrawl.tools.checkstyle.bcel.checks;
5
6 import org.apache.bcel.classfile.Field;
7 import org.apache.bcel.classfile.JavaClass;
8 import com.puppycrawl.tools.checkstyle.api.Scope;
9 import com.puppycrawl.tools.checkstyle.bcel.ReferenceVisitor;
10
11 /**
12  * Checks for fields that hide fields in superclasses
13  * @author Daniel Grenner
14  */

15 public class HiddenInheritedFieldCheck
16     extends AbstractReferenceCheck
17 {
18     /** @see AbstractReferenceCheck */
19     public void setScope(String JavaDoc aFrom)
20     {
21         super.setScope(aFrom);
22         ((ReferenceVisitor) getVisitor()).addFieldScope(
23             Scope.getInstance(aFrom));
24     }
25
26     /** @see com.puppycrawl.tools.checkstyle.bcel.IObjectSetVisitor */
27     public void visitObject(Object JavaDoc aJavaClass)
28     {
29         final JavaClass javaClass = (JavaClass) aJavaClass;
30         final String JavaDoc className = javaClass.getClassName();
31         final JavaClass[] superClasses = javaClass.getSuperClasses();
32         final Field[] fields = javaClass.getFields();
33         // Check all fields
34
for (int i = 0; i < fields.length; i++) {
35             final Field field = fields[i];
36             // Go through all superclasses
37
for (int j = 0; j < superClasses.length; j++) {
38                 final JavaClass superClass = superClasses[j];
39                 final String JavaDoc superClassName = superClass.getClassName();
40                 final Field[] superClassFields = superClass.getFields();
41                 // Go through the filds of the superclasses
42
for (int k = 0; k < superClassFields.length; k++) {
43                     final Field superClassField = superClassFields[k];
44                     if (!superClassField.isPrivate() &&
45                         superClassField.getName().equals(field.getName()) &&
46                         !ignore(className, field)) {
47                         log(
48                             javaClass,
49                             0,
50                             "hidden.inherited.field",
51                             new Object JavaDoc[] {fields[i], superClassName});
52                     }
53                 }
54             }
55         }
56     }
57
58     /** @see AbstractReferenceCheck */
59     public boolean ignore(String JavaDoc aClassName, Field aField) {
60         final String JavaDoc fieldName = aField.toString();
61         return (/*super.ignore(aClassName, aField)
62                 || */
fieldName.indexOf("[Synthetic]") > -1);
63     }
64 }
65
Popular Tags