public static Field getDeclaredField ( Class clazz , String fieldName ) throws NoSuchFieldException { Field f = getField ( clazz , fieldName ) ; if ( f == null ) { // Try the parent class Class parent = clazz . getSuperclass ( ) ; if ( parent != null ) { return getDeclaredField ( parent , fieldName ) ; } else { throw new NoSuchFieldException ( "The field '" + fieldName + "' could not be " + "found in the class of any of its parent " + "classes." ) ; } } else { return f ; } }
Recursively searches for the field specified by the fieldName in the class and all the super classes until it either finds it or runs out of parents .