@Test public void testGetFieldValue() throws NoSuchFieldException, IllegalAccessException { Assertions.assertEquals("d", ReflectionUtil.getFieldValue(new DurationUtil(), "DAY_UNIT")); Assertions.assertThrows(NoSuchFieldException.class, () -> ReflectionUtil.getFieldValue(new Object(), "A1B2C3")); }
public static Object getFieldValue(Object target, String fieldName) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException { Class<?> cl = target.getClass(); int i = 0; while ((i++) < MAX_NEST_DEPTH && cl != null) { try { Field field = cl.getDeclaredField(fieldName); field.setAccessible(true); return field.get(target); } catch (Exception e) { cl = cl.getSuperclass(); } } throw new NoSuchFieldException("class:" + target.getClass() + ", field:" + fieldName); }
ReflectionUtil { public static Object getFieldValue(Object target, String fieldName) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException { Class<?> cl = target.getClass(); int i = 0; while ((i++) < MAX_NEST_DEPTH && cl != null) { try { Field field = cl.getDeclaredField(fieldName); field.setAccessible(true); return field.get(target); } catch (Exception e) { cl = cl.getSuperclass(); } } throw new NoSuchFieldException("class:" + target.getClass() + ", field:" + fieldName); } }
ReflectionUtil { public static Object getFieldValue(Object target, String fieldName) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException { Class<?> cl = target.getClass(); int i = 0; while ((i++) < MAX_NEST_DEPTH && cl != null) { try { Field field = cl.getDeclaredField(fieldName); field.setAccessible(true); return field.get(target); } catch (Exception e) { cl = cl.getSuperclass(); } } throw new NoSuchFieldException("class:" + target.getClass() + ", field:" + fieldName); }  }
ReflectionUtil { public static Object getFieldValue(Object target, String fieldName) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException { Class<?> cl = target.getClass(); int i = 0; while ((i++) < MAX_NEST_DEPTH && cl != null) { try { Field field = cl.getDeclaredField(fieldName); field.setAccessible(true); return field.get(target); } catch (Exception e) { cl = cl.getSuperclass(); } } throw new NoSuchFieldException("class:" + target.getClass() + ", field:" + fieldName); }  static Class<?> getClassByName(String className); static Object getFieldValue(Object target, String fieldName); static Object invokeMethod(Object target, String methodName); static Object invokeMethod(Object target, String methodName, Class<?>[] parameterTypes, Object[] args); static Object invokeStaticMethod(Class<?> targetClass, String methodName, Class<?>[] parameterTypes,
                                            Object[] parameterValues); static Method getMethod(Class<?> classType, String methodName, Class<?>[] parameterTypes); static Set<Class<?>> getInterfaces(Class<?> clazz); }
ReflectionUtil { public static Object getFieldValue(Object target, String fieldName) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException { Class<?> cl = target.getClass(); int i = 0; while ((i++) < MAX_NEST_DEPTH && cl != null) { try { Field field = cl.getDeclaredField(fieldName); field.setAccessible(true); return field.get(target); } catch (Exception e) { cl = cl.getSuperclass(); } } throw new NoSuchFieldException("class:" + target.getClass() + ", field:" + fieldName); }  static Class<?> getClassByName(String className); static Object getFieldValue(Object target, String fieldName); static Object invokeMethod(Object target, String methodName); static Object invokeMethod(Object target, String methodName, Class<?>[] parameterTypes, Object[] args); static Object invokeStaticMethod(Class<?> targetClass, String methodName, Class<?>[] parameterTypes,
                                            Object[] parameterValues); static Method getMethod(Class<?> classType, String methodName, Class<?>[] parameterTypes); static Set<Class<?>> getInterfaces(Class<?> clazz); static final int MAX_NEST_DEPTH; }