ReflectUtil { public static List<Field> getAllFieldsForClass(Class<?> cmdClass, Class<?> baseClass) { List<Field> fields = new ArrayList<Field>(); Collections.addAll(fields, cmdClass.getDeclaredFields()); Class<?> superClass = cmdClass.getSuperclass(); while (baseClass.isAssignableFrom(superClass) && baseClass != superClass) { Field[] superClassFields = superClass.getDeclaredFields(); if (superClassFields != null) Collections.addAll(fields, superClassFields); superClass = superClass.getSuperclass(); } return fields; }  static Pair<Class<?>, Field> getAnyField(Class<?> clazz, String fieldName); static Set<Class<?>> getClassesWithAnnotation(Class<? extends Annotation> annotation, String[] packageNames); static boolean isCmdClassAsync(Class<?> cmdClass, Class<?>[] searchClasses); static List<Field> getAllFieldsForClass(Class<?> cmdClass, Class<?> baseClass); static Set<Field> getAllFieldsForClass(Class<?> cmdClass, 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(Class clz);  }
@Test public void getAllFieldsForClassWithFoo() throws NoSuchFieldException, SecurityException { 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"))); }
@Test public void getAllFieldsForClassWithBar() throws NoSuchFieldException, SecurityException { Set<Field> barFields = ReflectUtil.getAllFieldsForClass(Bar.class, new Class<?>[] {}); Assert.assertNotNull(barFields); Assert.assertTrue(barFields.contains(Foo.class.getDeclaredField("fooField"))); Assert.assertTrue(barFields.contains(Foo.class.getDeclaredField("fooIntField"))); Assert.assertTrue(barFields.contains(Bar.class.getDeclaredField("barField"))); Assert.assertTrue(barFields.contains(Bar.class.getDeclaredField("barIntField"))); }
@Test public void getAllFieldsForClassWithBarWithoutFoo() throws NoSuchFieldException, SecurityException { Set<Field> barFields = ReflectUtil.getAllFieldsForClass(Bar.class, new Class<?>[] {Foo.class}); Assert.assertNotNull(barFields); Assert.assertTrue(barFields.contains(Bar.class.getDeclaredField("barField"))); Assert.assertTrue(barFields.contains(Bar.class.getDeclaredField("barIntField"))); }
@Test public void getAllFieldsForClassWithBazWithoutBar() throws NoSuchFieldException, SecurityException { Set<Field> bazFields = ReflectUtil.getAllFieldsForClass(Baz.class, new Class<?>[] {Bar.class}); Assert.assertNotNull(bazFields); Assert.assertTrue(bazFields.contains(Foo.class.getDeclaredField("fooField"))); Assert.assertTrue(bazFields.contains(Foo.class.getDeclaredField("fooIntField"))); Assert.assertTrue(bazFields.contains(Baz.class.getDeclaredField("bazField"))); Assert.assertTrue(bazFields.contains(Baz.class.getDeclaredField("bazIntField"))); }