Java中bean转map

private static Map, List> CLASS_FIELD_CACHE = new HashMap, List>();

public static Map transBean2Map(Object object) {
    if (object == null) {
        return Collections.emptyMap();
    }
    Map map = new HashMap();
    try {
        List fields = CLASS_FIELD_CACHE.get(object.getClass());
        if (fields == null) {
            fields = new ArrayList();
            Field[] fieldArray = object.getClass().getDeclaredFields();
            for (Field field : fieldArray) {
                String name = field.getName();
                if ("serialVersionUID".equals(name)) {
                    continue;
                }
                field.setAccessible(true);
                fields.add(field);
            }
            CLASS_FIELD_CACHE.put(object.getClass(), fields);
        }
        for (Field field : fields) {
            String name = field.getName().toUpperCase();
            map.put(name, field.get(object));
        }
    }
    catch (Exception e) {
        logger.error("HashMap error: {}", e.getMessage(), e);
    }
    return map;
}

你可能感兴趣的:(java,webview,linq)