public static List<Field> getDeclaredFieldsIncludingInherited(Class<?> clazz) {
    List<Field> fields = new ArrayList<Field>();
    while (clazz != null) {
      Field[] sortedFields = clazz.getDeclaredFields();
      Arrays.sort(sortedFields, new Comparator<Field>() {
        public int compare(Field a, Field b) {
          return a.getName().compareTo(b.getName());
        }
      });
      for (Field field : sortedFields) {
        fields.add(field);
      }
      clazz = clazz.getSuperclass();
    }
    
    return fields;
  }
@Test
  public void testGetDeclaredFieldsIncludingInherited() {
    Parent child = new Parent() {
      private int childField;
      @SuppressWarnings("unused")
      public int getChildField() { return childField; }
    };
    
    List<Field> fields = ReflectionUtils.getDeclaredFieldsIncludingInherited(
        child.getClass());
    boolean containsParentField = false;
    boolean containsChildField = false;
    for (Field field : fields) {
      if (field.getName().equals("parentField")) {
        containsParentField = true;
      } else if (field.getName().equals("childField")) {
        containsChildField = true;
      }
    }
    
    List<Method> methods = ReflectionUtils.getDeclaredMethodsIncludingInherited(
        child.getClass());
    boolean containsParentMethod = false;
    boolean containsChildMethod = false;
    for (Method method : methods) {
      if (method.getName().equals("getParentField")) {
        containsParentMethod = true;
      } else if (method.getName().equals("getChildField")) {
        containsChildMethod = true;
      }
    }
    
    assertTrue("Missing parent field", containsParentField);
    assertTrue("Missing child field", containsChildField);
    assertTrue("Missing parent method", containsParentMethod);
    assertTrue("Missing child method", containsChildMethod);
  }