package com.crunii.micro.core.aspect;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.web.multipart.MultipartFile;

public class AopUtils {
    
    /**
     * 获取参数名和值
     * @param joinPoint
     * @return
     */
    public static Map<String,Object> getFieldsName(JoinPoint joinPoint) throws ClassNotFoundException, NoSuchMethodException {
        // 参数值
        Object[] args = joinPoint.getArgs();

        Signature signature = joinPoint.getSignature();
        MethodSignature methodSignature = (MethodSignature) signature;
        String[] parameterNames = methodSignature.getParameterNames();

        // 通过map封装参数和参数值
        Map<String, Object> paramMap = new HashMap<>();
        Object param = null;
        for (int i = 0; i < parameterNames.length; i++) {
            param = args[i];
            if(param instanceof HttpServletRequest || param instanceof HttpServletResponse || param instanceof MultipartFile){
                continue;
            }
            paramMap.put(parameterNames[i], param);
        }
        return paramMap;
    }
}
