@Test public void getAllFieldsForClassWithFoo() throws NoSuchFieldException, SecurityException { final Set<Field> fooFields = ReflectUtil.getAllFieldsForClass(Foo.class, new Class<?>[]{}); Assert.assertNotNull(fooFields); Assert.assertTrue(fooFields.contains(Foo.class.getDeclaredField("fooField"))); Assert.assertTrue(fooFields.contains(Foo.class.getDeclaredField("fooIntField"))); }
public static List<Field> getAllFieldsForClass(final Class<?> cmdClass, final Class<?> baseClass) { final List<Field> fields = new ArrayList<>(); Collections.addAll(fields, cmdClass.getDeclaredFields()); Class<?> superClass = cmdClass.getSuperclass(); while (baseClass.isAssignableFrom(superClass) && baseClass != superClass) { final Field[] superClassFields = superClass.getDeclaredFields(); if (superClassFields != null) { Collections.addAll(fields, superClassFields); } superClass = superClass.getSuperclass(); } return fields; }
ReflectUtil { public static List<Field> getAllFieldsForClass(final Class<?> cmdClass, final Class<?> baseClass) { final List<Field> fields = new ArrayList<>(); Collections.addAll(fields, cmdClass.getDeclaredFields()); Class<?> superClass = cmdClass.getSuperclass(); while (baseClass.isAssignableFrom(superClass) && baseClass != superClass) { final Field[] superClassFields = superClass.getDeclaredFields(); if (superClassFields != null) { Collections.addAll(fields, superClassFields); } superClass = superClass.getSuperclass(); } return fields; } }
ReflectUtil { public static List<Field> getAllFieldsForClass(final Class<?> cmdClass, final Class<?> baseClass) { final List<Field> fields = new ArrayList<>(); Collections.addAll(fields, cmdClass.getDeclaredFields()); Class<?> superClass = cmdClass.getSuperclass(); while (baseClass.isAssignableFrom(superClass) && baseClass != superClass) { final Field[] superClassFields = superClass.getDeclaredFields(); if (superClassFields != null) { Collections.addAll(fields, superClassFields); } superClass = superClass.getSuperclass(); } return fields; }  }
ReflectUtil { public static List<Field> getAllFieldsForClass(final Class<?> cmdClass, final Class<?> baseClass) { final List<Field> fields = new ArrayList<>(); Collections.addAll(fields, cmdClass.getDeclaredFields()); Class<?> superClass = cmdClass.getSuperclass(); while (baseClass.isAssignableFrom(superClass) && baseClass != superClass) { final Field[] superClassFields = superClass.getDeclaredFields(); if (superClassFields != null) { Collections.addAll(fields, superClassFields); } superClass = superClass.getSuperclass(); } return fields; }  static Pair<Class<?>, Field> getAnyField(final Class<?> clazz, final String fieldName); static Set<Class<?>> getClassesWithAnnotation(final Class<? extends Annotation> annotation, final String[] packageNames); static boolean isCmdClassAsync(final Class<?> cmdClass, final Class<?>[] searchClasses); static List<Field> getAllFieldsForClass(final Class<?> cmdClass, final Class<?> baseClass); static Set<Field> getAllFieldsForClass(final Class<?> cmdClass, final Class<?>[] excludeClasses); static List<String> flattenProperties(final Object target, final Class<?> clazz); static List<String> flattenPropeties(final Object target, final Class<?> clazz, final String... excludedProperties); static String getEntityName(final Class clz); }
ReflectUtil { public static List<Field> getAllFieldsForClass(final Class<?> cmdClass, final Class<?> baseClass) { final List<Field> fields = new ArrayList<>(); Collections.addAll(fields, cmdClass.getDeclaredFields()); Class<?> superClass = cmdClass.getSuperclass(); while (baseClass.isAssignableFrom(superClass) && baseClass != superClass) { final Field[] superClassFields = superClass.getDeclaredFields(); if (superClassFields != null) { Collections.addAll(fields, superClassFields); } superClass = superClass.getSuperclass(); } return fields; }  static Pair<Class<?>, Field> getAnyField(final Class<?> clazz, final String fieldName); static Set<Class<?>> getClassesWithAnnotation(final Class<? extends Annotation> annotation, final String[] packageNames); static boolean isCmdClassAsync(final Class<?> cmdClass, final Class<?>[] searchClasses); static List<Field> getAllFieldsForClass(final Class<?> cmdClass, final Class<?> baseClass); static Set<Field> getAllFieldsForClass(final Class<?> cmdClass, final Class<?>[] excludeClasses); static List<String> flattenProperties(final Object target, final Class<?> clazz); static List<String> flattenPropeties(final Object target, final Class<?> clazz, final String... excludedProperties); static String getEntityName(final Class clz);  }