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

net.linksfield.cube.partnersdk.utils.ReflectUtils Maven / Gradle / Ivy

package net.linksfield.cube.partnersdk.utils;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @ClassName ReflectUtils
 * @Description TODO
 * @Author James.hu
 * @Date 2023/4/7
 **/
public class ReflectUtils {
    public static  T newInstance(Class clazz) {
        try {
            return clazz.getConstructor().newInstance();
        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        } catch (NoSuchMethodException e) {
            throw new RuntimeException(e);
        }
    }

    public static List> objectToMap(List objectList) {
        return objectList.stream().map(ReflectUtils::objectToMap).collect(Collectors.toList());
    }

    public static Map objectToMap(Object object){
        Map dataMap = new HashMap<>();
        Class clazz = object.getClass();
        for (Field field : clazz.getDeclaredFields()) {
            try {
                field.setAccessible(true);
                dataMap.put(field.getName(),field.get(object));
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
        return dataMap;
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy