All Downloads are FREE. Search and download functionalities are using the official Maven repository.

cn.zhxu.bp.utils.MapUtils Maven / Gradle / Ivy

The newest version!
package cn.zhxu.bp.utils;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;

public class MapUtils {

    public static Map newFrom(Object source) {
        if (source == null) {
            return null;
        }
        Map map = new HashMap<>();
        Class clazz = source.getClass();
        while (clazz != Object.class) {
            for (Field field : clazz.getDeclaredFields()) {
                field.setAccessible(true);
                try {
                    map.put(field.getName(), field.get(source));
                } catch (ReflectiveOperationException e) {
                    throw new RuntimeException(e);
                }
            }
            clazz = clazz.getSuperclass();
        }
        return map;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy