Search in sources :

Example 6 with BeanMap

use of org.springframework.cglib.beans.BeanMap in project slate by shepherdviolet.

the class MapToBeanConverterImpl method convert0.

/**
 * Convert
 */
private void convert0(Map<String, Object> fromMap, Object toBean, ConversionPath conversionPath) {
    if (fromMap == null || fromMap.size() <= 0 || toBean == null) {
        return;
    }
    // Create BeanMap of toBean, and get propertyInfos of toBean
    BeanMap toBeanMap;
    Map<String, BeanInfoUtils.PropertyInfo> toBeanPropertyInfos;
    try {
        toBeanMap = BeanMap.create(toBean);
        toBeanPropertyInfos = BeanInfoUtils.getPropertyInfos(toBean.getClass());
    } catch (Throwable e) {
        throwConversionException("MapXBean: Error while mapping Map to " + toBean.getClass().getName() + ", map data:" + fromMap, e, conversionPath);
        return;
    }
    // Handle all properties in toBean
    for (Object keyObj : toBeanMap.keySet()) {
        // KV
        String key = String.valueOf(keyObj);
        Object value = fromMap.get(convertMapKey(key));
        if (propertyUpperCamelCase) {
            if (value == null) {
                // Get by lowerCase
                value = fromMap.get(key);
                if (value == null) {
                    continue;
                }
            }
        } else {
            if (value == null) {
                continue;
            }
        }
        // Property info
        BeanInfoUtils.PropertyInfo toBeanPropertyInfo = toBeanPropertyInfos.get(key);
        // Check write method
        if (inspectBeanStrictly) {
            if (toBeanPropertyInfo.getReadMethod() == null || toBeanPropertyInfo.getWriteMethod() == null) {
                continue;
            }
        } else {
            if (toBeanPropertyInfo.getWriteMethod() == null) {
                continue;
            }
        }
        // From this type
        Class<?> valueClass = value.getClass();
        // To this type
        Class<?> expectClass = toBeanMap.getPropertyType(key);
        Type expectType = toBeanPropertyInfo.getPropertyType();
        // Create sub ConversionPath for element
        ConversionPath subConversionPath = new ConversionPath(key, valueClass, expectClass, expectType, conversionPath);
        // Convert property
        Object convertedValue;
        try {
            convertedValue = propertyConverter.convert(value, valueClass, expectClass, expectType, subConversionPath);
        } catch (Throwable e) {
            throwConversionException("MapXBean: Error while mapping Map to " + toBean.getClass().getName() + ", property \"" + key + "\" mapping failed, map data:" + fromMap, e, subConversionPath);
            continue;
        }
        // Skip if null
        if (convertedValue == null) {
            continue;
        }
        // Put into bean
        try {
            toBeanMap.put(keyObj, convertedValue);
        } catch (Throwable e) {
            throwConversionException("MapXBean: Error while mapping Map to " + toBean.getClass().getName() + ", putting \"" + key + "\" failed, map data:" + fromMap, e, subConversionPath);
        }
    }
}
Also used : BeanInfoUtils(sviolet.thistle.util.reflect.BeanInfoUtils) BeanMap(org.springframework.cglib.beans.BeanMap) Type(java.lang.reflect.Type)

Example 7 with BeanMap

use of org.springframework.cglib.beans.BeanMap in project kykms by mahonelau.

the class RedisServiceImpl method getMapForReport.

/**
 * 查询redis信息for报表
 * @param type 1redis key数量 2 占用内存 3redis信息
 * @return
 * @throws RedisConnectException
 */
@Override
public Map<String, JSONArray> getMapForReport(String type) throws RedisConnectException {
    Map<String, JSONArray> mapJson = new HashMap<String, JSONArray>();
    JSONArray json = new JSONArray();
    if ("3".equals(type)) {
        List<RedisInfo> redisInfo = getRedisInfo();
        for (RedisInfo info : redisInfo) {
            Map<String, Object> map = Maps.newHashMap();
            BeanMap beanMap = BeanMap.create(info);
            for (Object key : beanMap.keySet()) {
                map.put(key + "", beanMap.get(key));
            }
            json.add(map);
        }
        mapJson.put("data", json);
        return mapJson;
    }
    for (int i = 0; i < 5; i++) {
        JSONObject jo = new JSONObject();
        Map<String, Object> map;
        if ("1".equals(type)) {
            map = getKeysSize();
            jo.put("value", map.get("dbSize"));
        } else {
            map = getMemoryInfo();
            Integer used_memory = Integer.valueOf(map.get("used_memory").toString());
            jo.put("value", used_memory / 1000);
        }
        String create_time = DateUtil.formatTime(DateUtil.date((Long) map.get("create_time") - (4 - i) * 1000));
        jo.put("name", create_time);
        json.add(jo);
    }
    mapJson.put("data", json);
    return mapJson;
}
Also used : BeanMap(org.springframework.cglib.beans.BeanMap) JSONObject(com.alibaba.fastjson.JSONObject) HashMap(java.util.HashMap) JSONArray(com.alibaba.fastjson.JSONArray) JSONObject(com.alibaba.fastjson.JSONObject) RedisInfo(org.jeecg.modules.monitor.domain.RedisInfo)

Example 8 with BeanMap

use of org.springframework.cglib.beans.BeanMap in project buessionframework by buession.

the class BeanUtils method toMap.

/**
 * 将 bean 对象转换成 {@link Map} 对象
 *
 * @param bean
 * 		Bean 对象
 *
 * @return Map
 */
@SuppressWarnings("rawtypes")
public static Map<String, Object> toMap(final Object bean) {
    Assert.isNull(bean, "No source bean specified.");
    if (bean instanceof Map) {
        final Map<?, ?> beanMap = (Map<?, ?>) bean;
        final Map<String, Object> result = new HashMap<>(beanMap.size());
        beanMap.forEach((key, value) -> {
            if (key != null) {
                result.put(key.toString(), value);
            }
        });
        return result;
    } else {
        BeanMap beanMap = BeanMap.create(bean);
        return new HashMap<>(beanMap);
    }
}
Also used : BeanMap(org.springframework.cglib.beans.BeanMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) BeanMap(org.springframework.cglib.beans.BeanMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) Map(java.util.Map)

Example 9 with BeanMap

use of org.springframework.cglib.beans.BeanMap in project spring-boot-starter-jdbc-helper by nyvi.

the class BeanMapUtils method beanToMap.

/**
 * bean转为Map
 * @param bean 实体bean
 * @param ignore 是否忽略值为空的字段
 * @param <T> 泛型
 * @return map对象
 */
public static final <T> Map<String, Object> beanToMap(T bean, boolean ignore) {
    if (Objects.isNull(bean)) {
        return new HashMap<>(0);
    }
    BeanMap beanMap = BeanMap.create(bean);
    Map<String, Object> map = Maps.newHashMap(beanMap.size());
    for (Object key : beanMap.keySet()) {
        Object value = beanMap.get(key);
        if (ignore && Objects.isNull(value)) {
            continue;
        }
        map.put(key.toString(), value);
    }
    return map;
}
Also used : BeanMap(org.springframework.cglib.beans.BeanMap) HashMap(java.util.HashMap)

Example 10 with BeanMap

use of org.springframework.cglib.beans.BeanMap in project spring-boot-starter-jdbc-helper by nyvi.

the class BeanMapUtils method mapToBean.

/**
 * Map转Bean
 * @param map map对象
 * @param bean 实体
 * @param <T> 泛型
 * @return bean对象
 */
public static <T> T mapToBean(Map<String, Object> map, T bean) {
    BeanMap beanMap = BeanMap.create(bean);
    beanMap.putAll(map);
    return bean;
}
Also used : BeanMap(org.springframework.cglib.beans.BeanMap)

Aggregations

BeanMap (org.springframework.cglib.beans.BeanMap)24 HashMap (java.util.HashMap)8 JSONObject (com.alibaba.fastjson.JSONObject)4 JSONArray (com.alibaba.fastjson.JSONArray)3 Map (java.util.Map)3 RedisInfo (org.jeecg.modules.monitor.domain.RedisInfo)3 Head (com.alibaba.excel.metadata.Head)2 Type (java.lang.reflect.Type)2 Test (org.junit.Test)2 BeanInfoUtils (sviolet.thistle.util.reflect.BeanInfoUtils)2 JSONArray (cn.hutool.json.JSONArray)1 JSONObject (cn.hutool.json.JSONObject)1 RedisInfo (com.albedo.java.modules.monitor.domain.RedisInfo)1 ExcelDataConvertException (com.alibaba.excel.exception.ExcelDataConvertException)1 ReadCellData (com.alibaba.excel.metadata.data.ReadCellData)1 ExcelContentProperty (com.alibaba.excel.metadata.property.ExcelContentProperty)1 ExcelReadHeadProperty (com.alibaba.excel.read.metadata.property.ExcelReadHeadProperty)1 CellWriteHandlerContext (com.alibaba.excel.write.handler.context.CellWriteHandlerContext)1 WriteHolder (com.alibaba.excel.write.metadata.holder.WriteHolder)1 Field (java.lang.reflect.Field)1