@Test public void testSet_01() { Person p = new Person(); ReflectionUtil.set(p, "name", "John Smith"); assertEquals("01", "John Smith", p.getName()); }
public static void set(Object obj, String field, Object value) { Field f = getField(obj.getClass(), field); if (f == null) { throw new RuntimeException("No such field: " + field); } try { f.setAccessible(true); f.set(obj, value); } catch (IllegalArgumentException | IllegalAccessException e) { throw new RuntimeException(e); } }
ReflectionUtil { public static void set(Object obj, String field, Object value) { Field f = getField(obj.getClass(), field); if (f == null) { throw new RuntimeException("No such field: " + field); } try { f.setAccessible(true); f.set(obj, value); } catch (IllegalArgumentException | IllegalAccessException e) { throw new RuntimeException(e); } } }
ReflectionUtil { public static void set(Object obj, String field, Object value) { Field f = getField(obj.getClass(), field); if (f == null) { throw new RuntimeException("No such field: " + field); } try { f.setAccessible(true); f.set(obj, value); } catch (IllegalArgumentException | IllegalAccessException e) { throw new RuntimeException(e); } } private  ReflectionUtil(); }
ReflectionUtil { public static void set(Object obj, String field, Object value) { Field f = getField(obj.getClass(), field); if (f == null) { throw new RuntimeException("No such field: " + field); } try { f.setAccessible(true); f.set(obj, value); } catch (IllegalArgumentException | IllegalAccessException e) { throw new RuntimeException(e); } } private  ReflectionUtil(); static T newInstance(Class<T> cls); static T newInstance(Class<T> cls, Object... args); static T newInstance(Class<T> cls, Class<?>[] paramTypes, Object... args); static void set(Object obj, String field, Object value); static void setStatic(Class<?> cls, String field, Object value); @SuppressWarnings("unchecked") static T get(Object obj, String field); @SuppressWarnings("unchecked") static T getStatic(Class<?> cls, String field); @SuppressWarnings("unchecked") static T call(Object obj, String method); static T call(Object obj, String method, Object... args); @SuppressWarnings("unchecked") static T call(Object obj, String method, Class<?>[] paramTypes, Object... args); @SuppressWarnings("unchecked") static T callStatic(Class<?> cls, String method); static T callStatic(Class<?> cls, String method, Object... args); @SuppressWarnings("unchecked") static T callStatic(Class<?> cls, String method, Class<?>[] paramTypes,
      Object... args); static Field getField(Class<?> cls, String name); static Method getMethod(Class<?> cls, String name, Class<?>... parameterTypes); }
ReflectionUtil { public static void set(Object obj, String field, Object value) { Field f = getField(obj.getClass(), field); if (f == null) { throw new RuntimeException("No such field: " + field); } try { f.setAccessible(true); f.set(obj, value); } catch (IllegalArgumentException | IllegalAccessException e) { throw new RuntimeException(e); } } private  ReflectionUtil(); static T newInstance(Class<T> cls); static T newInstance(Class<T> cls, Object... args); static T newInstance(Class<T> cls, Class<?>[] paramTypes, Object... args); static void set(Object obj, String field, Object value); static void setStatic(Class<?> cls, String field, Object value); @SuppressWarnings("unchecked") static T get(Object obj, String field); @SuppressWarnings("unchecked") static T getStatic(Class<?> cls, String field); @SuppressWarnings("unchecked") static T call(Object obj, String method); static T call(Object obj, String method, Object... args); @SuppressWarnings("unchecked") static T call(Object obj, String method, Class<?>[] paramTypes, Object... args); @SuppressWarnings("unchecked") static T callStatic(Class<?> cls, String method); static T callStatic(Class<?> cls, String method, Object... args); @SuppressWarnings("unchecked") static T callStatic(Class<?> cls, String method, Class<?>[] paramTypes,
      Object... args); static Field getField(Class<?> cls, String name); static Method getMethod(Class<?> cls, String name, Class<?>... parameterTypes);  }