Java Reflection Field Value Set setFieldValue(Map map, Class cls)

Here you can find the source of setFieldValue(Map map, Class cls)

Description

set Field Value

License

Open Source License

Declaration

public static Object setFieldValue(Map<String, Object> map, Class<?> cls) throws Exception 

Method Source Code


//package com.java2s;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

import java.util.Map;

public class Main {
    public static Object setFieldValue(Map<String, Object> map, Class<?> cls) throws Exception {
        Field[] fields = cls.getDeclaredFields();
        Object obj = cls.newInstance();
        for (Field field : fields) {
            Class<?> clsType = field.getType();
            String name = field.getName();
            String strSet = "set" + name.substring(0, 1).toUpperCase() + name.substring(1, name.length());
            Method methodSet = cls.getDeclaredMethod(strSet, clsType);
            if (map.containsKey(name)) {
                Object objValue = map.get(name);
                methodSet.invoke(obj, objValue);
            }/*from   www . j a  va  2s.  c om*/
        }
        return obj;
    }
}

Related

  1. setFieldValue(final Object obj, final String fieldName, final Object value)
  2. setFieldValue(final Object object, final String field, final Object value)
  3. setFieldValue(final Object object, final String fieldName, final Object value)
  4. setFieldValue(final Object object, final String fieldName, final Object value)
  5. setFieldValue(final Object object, final String fieldName, final Object value)
  6. setFieldValue(Object bean, Field field, Object value)
  7. setFieldValue(Object bean, Field field, Object value)
  8. setFieldValue(Object bean, String field, Object value)
  9. setFieldValue(Object host, Field f, Object value)