Java Reflection Field Value Set setFieldValue(Class c, Object instance, String name, Object value)

Here you can find the source of setFieldValue(Class c, Object instance, String name, Object value)

Description

set Field Value

License

Apache License

Declaration

public static void setFieldValue(Class c, Object instance, String name, Object value) 

Method Source Code


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

import java.lang.reflect.Field;

public class Main {
    public static void setFieldValue(Class c, Object instance, String name, Object value) {
        try {/*from ww  w.j  a v a  2s .c  o  m*/
            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);
        }
    }
}

Related

  1. setFieldValue(Class clazz, Object instance, String fieldName, T value)
  2. setFieldValue(Class clazz, Object target, String fieldName, Object value)
  3. setFieldValue(Class clazz, E instance, Object value, String... names)
  4. setFieldValue(Class clazz, Object entity, String fieldName, V value)