Java Utililty Methods Reflection Field Set

List of utility methods to do Reflection Field Set

Description

The list of methods to do Reflection Field Set are organized into topic(s).

Method

voidsetField(Object target, String name, Object value)
Sets the new for field with given name.
Assert.isNotNull(target);
Assert.isNotNull(name);
Field field = getField(target, name);
if (field == null) {
    Class<?> targetClass = getTargetClass(target);
    throw new IllegalArgumentException(name + " in " + targetClass);
try {
...
voidsetField(Object targetObject, String fieldName, Object value, boolean failIfError)
Sets the fields value via reflection, this is useful in case there is no setter defined on the target object.
try {
    Field f = targetObject.getClass().getDeclaredField(fieldName);
    f.setAccessible(true);
    f.set(targetObject, value);
} catch (Exception e) {
    if (failIfError) {
        throw new RuntimeException("failed to set field", e);
    } else {
...
voidsetField(String field, Object object, Object value)
set Field
try {
    Field entryField = object.getClass().getDeclaredField(field);
    entryField.setAccessible(true);
    entryField.set(object, value);
} catch (Exception e) {
    e.printStackTrace();
voidsetField(String field, Object value)
set Field
Field f = packet.getClass().getDeclaredField(field);
f.setAccessible(true);
f.set(packet, value);
voidsetField(String fieldName, Object instance, Class instanceClass, Object value)
set Field
if (instanceClass == null) {
    throw new RuntimeException("NoSuchField: " + fieldName);
try {
    Field field = instanceClass.getDeclaredField(fieldName);
    field.setAccessible(true);
    field.set(instance, value);
} catch (NoSuchFieldException e) {
...
booleansetField(String fieldName, Object instance, Object value)
set Field
Field field = fieldFor(instance, fieldName);
return field != null && setField(field, instance, value);
voidsetField(String name, Object target, Object value)
set Field
try {
    Class<?> targetClass = target.getClass();
    Field field = targetClass.getDeclaredField(name);
    boolean unlocked = false;
    if (!field.isAccessible()) {
        field.setAccessible(true);
        unlocked = true;
    field.set(target, value);
    if (unlocked) {
        field.setAccessible(false);
} catch (Exception e) {
    throw new RuntimeException(e);
voidsetField(String name, Object target, Object value)
set Field
Field field = null;
Class<?> clazz = target.getClass();
do {
    try {
        field = clazz.getDeclaredField(name);
    } catch (Exception ex) {
    clazz = clazz.getSuperclass();
...
voidsetField(T object, Field field, ResourceBundle bundle)
set Field
try {
    String value = bundle.getString(field.getName());
    if (value != null) {
        field.setAccessible(true);
        field.set(object, value);
} catch (MissingResourceException mre) {
    field.setAccessible(true);
...
voidsetFieldAccessible(Class clazz, String... names)
set Field Accessible
try {
    Field field = getField(clazz, names);
    if (field != null)
        field.setAccessible(true);
} catch (Exception e) {