Java Reflection Field Set setField(Class cls, Object obj, String fieldName, Object value)

Here you can find the source of setField(Class cls, Object obj, String fieldName, Object value)

Description

set Field

License

MIT License

Declaration

public static void setField(Class cls, Object obj, String fieldName, Object value) throws Exception 

Method Source Code

//package com.java2s;
/*******************************************************************************
 *  Copyright ? 2012-2015 eBay Software Foundation
 *  This program is dual licensed under the MIT and Apache 2.0 licenses.
 *  Please see LICENSE for more information.
 *******************************************************************************/

import java.lang.reflect.Field;

public class Main {
    public static void setField(Object obj, String fieldName, Object value) throws Exception {
        Class cls = obj.getClass();
        Field field = cls.getDeclaredField(fieldName);
        field.setAccessible(true);/*from   w w w  .  j  a  v a 2 s .c o m*/
        field.set(obj, value);
    }

    public static void setField(Class cls, Object obj, String fieldName, Object value) throws Exception {
        Field field = cls.getDeclaredField(fieldName);
        field.setAccessible(true);
        field.set(obj, value);
    }
}

Related

  1. setField(Class clazz, Object instance, String fieldName, Object value)
  2. setField(Class clazz, Object src, String fieldName, Object value)
  3. setField(Class clazz, String name, Object value)
  4. setField(Class type, String name, Object instance, Object value)
  5. setField(Class klazz, Object recipient, String fieldName, Object newValue)
  6. setField(Class clazz, String fieldName, T instance, Object value)
  7. setField(Class c, Object inst, String name, Object value)