List of utility methods to do Reflection Field Value Set
void | setFieldValue(Class c, Object instance, String name, Object value) set Field Value try { Field declaredField = c.getDeclaredField(name); declaredField.setAccessible(true); declaredField.set(instance, value); } catch (IllegalAccessException e) { throw new RuntimeException(e); } catch (NoSuchFieldException e) { throw new RuntimeException(e); ... |
void | setFieldValue(Class clazz, Object instance, String fieldName, T value) Sets the value of a field using Reflection API. try { getField(clazz, fieldName).set(instance, value); } catch (IllegalAccessException e) { throw new RuntimeException(e); |
void | setFieldValue(Class clazz, Object target, String fieldName, Object value) Set class field value, returns success status HashMap<String, Object> result = new HashMap<>(); Field field = clazz.getDeclaredField(fieldName); field.setAccessible(true); Field modifiersField = Field.class.getDeclaredField("modifiers"); modifiersField.setAccessible(true); modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL); field.set(target, value); |
void | setFieldValue(Class extends E> clazz, E instance, Object value, String... names) set Field Value try { Field field = getField(clazz, names); if (field != null) field.set(instance, value); } catch (Exception e) { |
boolean | setFieldValue(Class> clazz, Object entity, String fieldName, V value) set Field Value try { Field f = clazz.getDeclaredField(fieldName); f.setAccessible(true); f.set(entity, value); return true; } catch (Exception ex) { throw new RuntimeException("Cannot set field " + fieldName + " value to " + value, ex); |
void | setFieldValue(Class> clazz, String fieldName, Object value) set Field Value setFieldValue(clazz, null, fieldName, value); |
void | setFieldValue(Class> clazz, String fieldName, Object value) Set static field value. final Field field = findField(clazz, fieldName); try { field.set(null, value); } catch (Exception e) { throw new RuntimeException(e); |
T | setFieldValue(Class set Field Value try { Class<? extends Object> targetClass = target.getClass(); while (targetClass != null) { Field field = locateField(expectedType, targetClass, fieldName); if (field != null) { T oldValue = (T) field.get(target); field.set(target, value); return oldValue; ... |
void | setFieldValue(Field f, Object target, Object value) Convenience method; Wraps Field#set(Object,Object) to propagate RuntimeException s. try { f.set(target, value); } catch (IllegalArgumentException | IllegalAccessException e) { throw Throwables.propagate(e); |
T | setFieldValue(Field field, Object instance, T value) Sets the value for the field of the instance if (instance != null) { boolean oldAccessible = field.isAccessible(); field.setAccessible(true); field.set(instance, value); field.setAccessible(oldAccessible); return value; return null; ... |