@Test void testFindFieldsInClass() { Iterable<Field> actualFields = ReflectionUtils.fieldsOf(SomeSubType.class); int t = 0; for (Field actual : actualFields) { if (actual.isSynthetic()) { continue; } switch (t++) { case 0: assertEquals("field3", actual.getName()); break; case 1: case 2: assertTrue("field1".equals(actual.getName()) || "field2".equals(actual.getName()), "Expected either field1 or field2, but got " + actual.getName() + " declared in " + actual.getDeclaringClass().getName()); break; } } assertTrue(t >= 2); }
public static Iterable<Field> fieldsOf(Class<?> clazz) { List<Field> fields = new LinkedList<>(); Class<?> currentClazz = clazz; do { fields.addAll(Arrays.asList(currentClazz.getDeclaredFields())); currentClazz = currentClazz.getSuperclass(); } while (currentClazz != null); return Collections.unmodifiableList(fields); }
ReflectionUtils { public static Iterable<Field> fieldsOf(Class<?> clazz) { List<Field> fields = new LinkedList<>(); Class<?> currentClazz = clazz; do { fields.addAll(Arrays.asList(currentClazz.getDeclaredFields())); currentClazz = currentClazz.getSuperclass(); } while (currentClazz != null); return Collections.unmodifiableList(fields); } }
ReflectionUtils { public static Iterable<Field> fieldsOf(Class<?> clazz) { List<Field> fields = new LinkedList<>(); Class<?> currentClazz = clazz; do { fields.addAll(Arrays.asList(currentClazz.getDeclaredFields())); currentClazz = currentClazz.getSuperclass(); } while (currentClazz != null); return Collections.unmodifiableList(fields); } private  ReflectionUtils(); }
ReflectionUtils { public static Iterable<Field> fieldsOf(Class<?> clazz) { List<Field> fields = new LinkedList<>(); Class<?> currentClazz = clazz; do { fields.addAll(Arrays.asList(currentClazz.getDeclaredFields())); currentClazz = currentClazz.getSuperclass(); } while (currentClazz != null); return Collections.unmodifiableList(fields); } private  ReflectionUtils(); @SuppressWarnings("unchecked") static R getFieldValue(Field field, Object object); static void setFieldValue(Field field, Object object, T value); static Class<?> declaringClass(Class<?> instanceClass, String methodName, Class<?>... parameterTypes); static boolean hasEqualsMethod(Class<?> type); @SuppressWarnings("unchecked") static boolean explicitlyUnequal(Object value, Object otherValue); static T ensureAccessible(T member); static boolean isAccessible(AccessibleObject member); static boolean isNonFinalPublicMember(Member member); static Iterable<Field> fieldsOf(Class<?> clazz); static Method methodOf(Class<?> clazz, String methodName, Class<?>... parameterTypes); static Iterable<Method> methodsOf(Class<?> clazz); static Class<?> resolvePrimitiveWrapperType(Class<?> primitiveType); static Type resolvePrimitiveWrapperTypeIfPrimitive(Type type); static Type unwrapIfType(Type type, Class<?>... wrapperTypes); static boolean isTransient(Field field); static Optional<Class<?>> resolveGenericType(Field field, int genericTypeIndex); static Optional<Class<?>> resolveMemberGenericType(Member member, int genericTypeIndex); @SuppressWarnings("unchecked") static R invokeAndGetMethodValue(Method method, Object object); @SuppressWarnings("unchecked") static R getMemberValue(Member member, Object target); static Class<?> getMemberValueType(Member member); static Type getMemberGenericType(Member member); static String getMemberGenericString(Member member); }
ReflectionUtils { public static Iterable<Field> fieldsOf(Class<?> clazz) { List<Field> fields = new LinkedList<>(); Class<?> currentClazz = clazz; do { fields.addAll(Arrays.asList(currentClazz.getDeclaredFields())); currentClazz = currentClazz.getSuperclass(); } while (currentClazz != null); return Collections.unmodifiableList(fields); } private  ReflectionUtils(); @SuppressWarnings("unchecked") static R getFieldValue(Field field, Object object); static void setFieldValue(Field field, Object object, T value); static Class<?> declaringClass(Class<?> instanceClass, String methodName, Class<?>... parameterTypes); static boolean hasEqualsMethod(Class<?> type); @SuppressWarnings("unchecked") static boolean explicitlyUnequal(Object value, Object otherValue); static T ensureAccessible(T member); static boolean isAccessible(AccessibleObject member); static boolean isNonFinalPublicMember(Member member); static Iterable<Field> fieldsOf(Class<?> clazz); static Method methodOf(Class<?> clazz, String methodName, Class<?>... parameterTypes); static Iterable<Method> methodsOf(Class<?> clazz); static Class<?> resolvePrimitiveWrapperType(Class<?> primitiveType); static Type resolvePrimitiveWrapperTypeIfPrimitive(Type type); static Type unwrapIfType(Type type, Class<?>... wrapperTypes); static boolean isTransient(Field field); static Optional<Class<?>> resolveGenericType(Field field, int genericTypeIndex); static Optional<Class<?>> resolveMemberGenericType(Member member, int genericTypeIndex); @SuppressWarnings("unchecked") static R invokeAndGetMethodValue(Method method, Object object); @SuppressWarnings("unchecked") static R getMemberValue(Member member, Object target); static Class<?> getMemberValueType(Member member); static Type getMemberGenericType(Member member); static String getMemberGenericString(Member member);  }