@Test public void testFindField() throws Exception { assertThat(findField(TestClass.class, "testClassField")).isEqualTo(TestClass.class.getDeclaredField("testClassField")); assertThat(findField(TestClass.class, "superClassField")).isEqualTo(TestSuperClass.class.getDeclaredField("superClassField")); }
public static Field findField(Class<?> type, String name) { if (type == null) { throw new IllegalArgumentException("Method argument type must not be null"); } if (name == null) { throw new IllegalArgumentException("Method argument name must not be null"); } Class<?> c = type; do { for (Field f : c.getDeclaredFields()) { if (name.equals(f.getName())) { return f; } } c = c.getSuperclass(); } while (c != null); return null; }
ReflectionUtil { public static Field findField(Class<?> type, String name) { if (type == null) { throw new IllegalArgumentException("Method argument type must not be null"); } if (name == null) { throw new IllegalArgumentException("Method argument name must not be null"); } Class<?> c = type; do { for (Field f : c.getDeclaredFields()) { if (name.equals(f.getName())) { return f; } } c = c.getSuperclass(); } while (c != null); return null; } }
ReflectionUtil { public static Field findField(Class<?> type, String name) { if (type == null) { throw new IllegalArgumentException("Method argument type must not be null"); } if (name == null) { throw new IllegalArgumentException("Method argument name must not be null"); } Class<?> c = type; do { for (Field f : c.getDeclaredFields()) { if (name.equals(f.getName())) { return f; } } c = c.getSuperclass(); } while (c != null); return null; } private  ReflectionUtil(); }
ReflectionUtil { public static Field findField(Class<?> type, String name) { if (type == null) { throw new IllegalArgumentException("Method argument type must not be null"); } if (name == null) { throw new IllegalArgumentException("Method argument name must not be null"); } Class<?> c = type; do { for (Field f : c.getDeclaredFields()) { if (name.equals(f.getName())) { return f; } } c = c.getSuperclass(); } while (c != null); return null; } private  ReflectionUtil(); static Type getBoundaryOfParametrizedType(final Type type, final Type assigningType); @SuppressWarnings("unchecked") static Collection<K> instantiateCollectionType(Class<T> collectionType, int length); static boolean isInstantiableCollectionType(Class<?> type); static Class<?>[] getInstantiableCollectionTypes(); static Collection<K> instantiateCollectionType(Class<T> collectionType); static List<Method> methodsOf(Class<?> type); static Field findField(Class<?> type, String name); static Field makeAccessible(Field field); static Method makeAccessible(Method method); }
ReflectionUtil { public static Field findField(Class<?> type, String name) { if (type == null) { throw new IllegalArgumentException("Method argument type must not be null"); } if (name == null) { throw new IllegalArgumentException("Method argument name must not be null"); } Class<?> c = type; do { for (Field f : c.getDeclaredFields()) { if (name.equals(f.getName())) { return f; } } c = c.getSuperclass(); } while (c != null); return null; } private  ReflectionUtil(); static Type getBoundaryOfParametrizedType(final Type type, final Type assigningType); @SuppressWarnings("unchecked") static Collection<K> instantiateCollectionType(Class<T> collectionType, int length); static boolean isInstantiableCollectionType(Class<?> type); static Class<?>[] getInstantiableCollectionTypes(); static Collection<K> instantiateCollectionType(Class<T> collectionType); static List<Method> methodsOf(Class<?> type); static Field findField(Class<?> type, String name); static Field makeAccessible(Field field); static Method makeAccessible(Method method);  }