Java Reflection Field Value Set setFieldValue(Field field, Object instance, T value)

Here you can find the source of setFieldValue(Field field, Object instance, T value)

Description

Sets the value for the field of the instance

License

Apache License

Declaration

public static <T> T setFieldValue(Field field, Object instance, T value) throws IllegalAccessException 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.lang.reflect.Field;

public class Main {
    /**/*from w  w w .j av a 2 s  .c o  m*/
     * Sets the value for the field of the instance
     */
    public static <T> T setFieldValue(Field field, Object instance, T value) throws IllegalAccessException {
        if (instance != null) {
            boolean oldAccessible = field.isAccessible();
            field.setAccessible(true);
            field.set(instance, value);
            field.setAccessible(oldAccessible);
            return value;
        }
        return null;
    }
}

Related

  1. setFieldValue(Class clazz, Object entity, String fieldName, V value)
  2. setFieldValue(Class clazz, String fieldName, Object value)
  3. setFieldValue(Class clazz, String fieldName, Object value)
  4. setFieldValue(Class expectedType, Object target, String fieldName, T value)
  5. setFieldValue(Field f, Object target, Object value)
  6. setFieldValue(Field field, Object obj, Object value)
  7. setFieldValue(Field field, Object object, Object value)
  8. setFieldValue(Field field, Object object, Object value)
  9. setFieldValue(Field field, Object target, Object value)