/** * Invoke the given callback on all fields in the target class, going up the * class hierarchy to get all declared fields. * * @param targetClass the target class to analyze * @param fc the callback to invoke for each field * @param ff the filter that determines the fields to apply the callback to */ public static void doWithFields(Class<?> targetClass, final FieldCallback fc, final FieldFilter ff) throws IllegalArgumentException { // Keep backing up the inheritance hierarchy. do { // Copy each field declared on this class unless it's static or // file. final Field[] fields = targetClass.getDeclaredFields(); for (final Field field : fields) { // Skip static and final fields. if (ff != null && !ff.matches(field)) { continue; } try { fc.doWith(field); } catch (final IllegalAccessException ex) { throw new IllegalStateException("Shouldn't be illegal to access field '" + field.getName() + "': " + ex); } } targetClass = targetClass.getSuperclass(); } while (targetClass != null && targetClass != Object.class); }