Java Reflection Field Value Set setFieldValue(Object target, String field, Object value)

Here you can find the source of setFieldValue(Object target, String field, Object value)

Description

set Field Value

License

Apache License

Declaration

public final static void setFieldValue(Object target, String field, Object value) 

Method Source Code

//package com.java2s;
/*/*from w w w. java 2  s.c o  m*/
 * BJAF - Beetle J2EE Application Framework
 * ???J2EE???????????
 * ??????2003-2015 ??? (www.beetlesoft.net)
 * 
 * ??????????????????
 *<http://www.apache.org/licenses/LICENSE-2.0>
 *????????????????????????
 *
 * ???????????????????????????????
 * ??? <yuhaodong@gmail.com/>.
 */

import java.lang.reflect.Field;

public class Main {

    public final static void setFieldValue(Object target, String field, Object value) {
        try {
            Class<?> obj = target.getClass();
            Field[] fields = obj.getDeclaredFields();
            for (int i = 0; i < fields.length; i++) {
                fields[i].setAccessible(true);
                if (field.equals(fields[i].getName())) {
                    fields[i].set(target, value);
                    break;
                }
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}

Related

  1. setFieldValue(Object receiver, String fieldName, Object value)
  2. setFieldValue(Object target, Class targetClass, String fieldName, Object value)
  3. setFieldValue(Object target, Field field, Object newValue)
  4. setFieldValue(Object target, Field field, Object value)
  5. setFieldValue(Object target, Object mock, final Field field)
  6. setFieldValue(Object target, String fieldName, Object fieldValue)
  7. setFieldValue(Object target, String fieldName, Object value)
  8. setFieldValue(Object target, String fieldName, Object value)
  9. setFieldValue(Object target, String fieldName, String fieldValue)