package com.eitc.common.utils;

import com.eitc.common.utils.bean.BeanUtils;
import lombok.extern.log4j.Log4j2;

import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

//import static sun.invoke.util.Wrapper.isPrimitiveType;









@Log4j2
public class ObjectUtils {
    public static Object decryptObjectValues(Object obj) throws Exception{
        if (obj == null) {
            return null;
        }
        Class<?> objClass = obj.getClass();
        PropertyDescriptor[] propertyDescriptors = BeanUtils.getPropertyDescriptors(objClass);
        for (PropertyDescriptor propertyDescriptor : propertyDescriptors) {
            Method readMethod = propertyDescriptor.getReadMethod();
            Method writeMethod = propertyDescriptor.getWriteMethod();
            if (readMethod != null && writeMethod!=null) {
                try {
                    Object value = readMethod.invoke(obj);
                    if(value==null) {
                        continue;
                    }
//                    if(isPrimitiveType(value.getClass()) && value instanceof String) {
//                        String changeValue = CryptoUtil.decrypt((String)value);
//                        writeMethod.invoke(obj, changeValue);
//                    }
                } catch (IllegalAccessException | InvocationTargetException e) {
                    e.printStackTrace();
                    log.error("反射获取类【" + objClass.getName() + "】方法异常，", e);
                }
            }
        }
        return obj;

    }
}
