public static Field getFieldEx(Class<?> clazz, String fieldName) throws NoSuchFieldException
  {
    try {
      Field field = clazz.getDeclaredField(fieldName);
      field.setAccessible(true);
      return field;
    }
    catch(NoSuchFieldException e) {
      Class<?> superclass = clazz.getSuperclass();
      if(superclass != null && clazz.getPackage().equals(superclass.getPackage())) {
        return getFieldEx(superclass, fieldName);
      }
      throw e;
    }
    catch(SecurityException e) {
      throw new BugError(e);
    }
  }
public static Field getFieldEx ( Class < ? > clazz , String fieldName ) throws NoSuchFieldException { try { Field field = clazz . getDeclaredField ( fieldName ) ; field . setAccessible ( true ) ; return field ; } catch ( NoSuchFieldException e ) { Class < ? > superclass = clazz . getSuperclass ( ) ; if ( superclass != null && clazz . getPackage ( ) . equals ( superclass . getPackage ( ) ) ) { return getFieldEx ( superclass , fieldName ) ; } throw e ; } catch ( SecurityException e ) { throw new BugError ( e ) ; } }
Get named field of requested class class or its super-classes package hierarchy, with checked exception. Tries to
get requested field from given class; if not found try with super-classes hierarchy but limited to requested class
package. If field still not found throw {@link NoSuchFieldException}.
<p>
Implementation note: if field not found on requested class this method is executed recursively as long as
superclass is in the same package as requested base class. Is not possible to retrieve inherited fields if
superclass descendant is in different package.

@param clazz class to search for named field,
@param fieldName the name of field to retrieve.
@return requested field.
@throws NoSuchFieldException if class or super-class package hierarchy has no field with requested name.