package com.gic.haoban.manage.web.utils;

import lombok.extern.slf4j.Slf4j;

import java.lang.reflect.Method;

@Slf4j
public class ReflectUtils {

    public static String getDateFieldValue(Object obj, String fieldName) {
        try {
            Method method = obj.getClass().getMethod("get" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1));
            return (String) method.invoke(obj);
        } catch (Exception e) {
            log.warn("反射get值错误:{}", e.getMessage(), e);
            return null;
        }
    }

    public static void setDateFieldValue(Object obj, String fieldName, String dataValue) {
        try {
            Method method = obj.getClass().getMethod("set" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1), String.class);
            method.invoke(obj, dataValue);
        } catch (Exception e) {
            log.warn("反射set值错误:{}", e.getMessage(), e);
        }
    }

    public static void setDateFieldValue(Object obj, String fieldName, Integer dataValue) {
        try {
            Method method = obj.getClass().getMethod("set" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1), Integer.class);
            method.invoke(obj, dataValue);
        } catch (Exception e) {
            log.warn("反射set值错误:{}", e.getMessage(), e);
        }
    }

    public static  <T> T createInstance(Class<T> clazz) {
        try {
            return clazz.newInstance();
        } catch (InstantiationException e) {
            return null;
        } catch (IllegalAccessException e) {
            return null;
        }
    }
}
