List of utility methods to do Reflection Field Set
boolean | setField(Object object, String fieldName, Object newValue, boolean isFindDeclaredField, boolean isUpwardFind) set Field boolean result = false; Field field = getField(object.getClass(), fieldName, isFindDeclaredField, isUpwardFind); if (field != null) { try { field.setAccessible(true); field.set(object, newValue); result = true; } catch (IllegalAccessException e) { ... |
void | setField(Object object, String fieldName, Object value) set Field try { Field field = getField(object, fieldName); field.set(object, value); } catch (IllegalAccessException e) { throw new IllegalArgumentException(e); |
void | setField(Object object, String fieldName, Object value) set Field try { getField(object.getClass(), fieldName).set(object, value); } catch (Exception unexpectedReflectionException) { throw new RuntimeException("unexpected exception ", unexpectedReflectionException); |
void | setField(Object object, String fieldName, Object value) Set the field value of the field with the given name of the specified object. try { Field field; if (object instanceof Class) { field = findField((Class) object, fieldName, null); object = null; } else { field = findField(object.getClass(), fieldName, null); field.setAccessible(true); field.set(object, value); } catch (IllegalAccessException | IllegalArgumentException | SecurityException e) { throw new Error(e); |
void | setField(Object object, String fieldName, Object value) set Field try { Field field = object.getClass().getField(fieldName); field.set(object, value); } catch (NoSuchFieldException | IllegalAccessException e) { e.printStackTrace(); |
void | setField(Object object, String name, boolean value) Set the field, even if it's private, to a boolean value. Field field = findDeclaredField(object.getClass(), name); field.setAccessible(true); field.setBoolean(object, value); |
void | setField(Object object, String name, Object value) set Field Class<?> clazz = object.getClass(); Field field = clazz.getDeclaredField(name); boolean accessable = field.isAccessible(); if (!accessable) field.setAccessible(true); field.set(object, value); if (!accessable) field.setAccessible(false); ... |
void | setField(Object object, String name, Object value) set Field try { Field field = object.getClass().getDeclaredField(name); boolean isPrivate = !field.isAccessible(); if (isPrivate) { field.setAccessible(true); field.set(object, value); if (isPrivate) { ... |
void | setField(Object owner, String name, Object newValue, Class> definedIn) set Field Field field = getFieldDescriptor(owner, name, definedIn); field.set(owner, newValue); |
void | setField(Object owner, String targetClass, String fieldName, Object value) Set object's field with custom value even it's private. Class<?> ownerclass = getTargetclass(owner, targetClass);
Field field = ownerclass.getDeclaredField(fieldName);
if (!field.isAccessible()) {
field.setAccessible(true);
field.set(owner, value);
|