1 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 15 public class HiddenInheritedFieldCheck 16 extends AbstractReferenceCheck 17 { 18 19 public void setScope(String aFrom) 20 { 21 super.setScope(aFrom); 22 ((ReferenceVisitor) getVisitor()).addFieldScope( 23 Scope.getInstance(aFrom)); 24 } 25 26 27 public void visitObject(Object aJavaClass) 28 { 29 final JavaClass javaClass = (JavaClass) aJavaClass; 30 final String className = javaClass.getClassName(); 31 final JavaClass[] superClasses = javaClass.getSuperClasses(); 32 final Field[] fields = javaClass.getFields(); 33 for (int i = 0; i < fields.length; i++) { 35 final Field field = fields[i]; 36 for (int j = 0; j < superClasses.length; j++) { 38 final JavaClass superClass = superClasses[j]; 39 final String superClassName = superClass.getClassName(); 40 final Field[] superClassFields = superClass.getFields(); 41 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 [] {fields[i], superClassName}); 52 } 53 } 54 } 55 } 56 } 57 58 59 public boolean ignore(String aClassName, Field aField) { 60 final String fieldName = aField.toString(); 61 return (fieldName.indexOf("[Synthetic]") > -1); 63 } 64 } 65 | Popular Tags |