SPEL解析器

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class AnnotationResolver {
    private static final Logger LOGGER = Logger.getLogger(AnnotationResolver.class.getName());
    private static AnnotationResolver resolver;

    public static AnnotationResolver newInstance() {
        if (resolver == null) {
            return resolver = new AnnotationResolver();
        } else {
            return resolver;
        }
    }

    public String resolverSql(JoinPoint joinPoint, String sql){
        if(StringUtil.isEmpty(sql)){
            return StringUtil.EMPTY;
        }

        Pattern pattern = Pattern.compile("#\\{[^\\}]*\\}");
        Matcher matcher = pattern.matcher(sql);
        List<String> executionList = new ArrayList<>();
        while(matcher.find()){
            executionList.add(sql.substring(matcher.start(), matcher.end()));
        }
        String[] executions = executionList.toArray(new String[executionList.size()]);
        Object[] objects = resolver(joinPoint, executions);
        for(int index = 0; index < executions.length; index ++){
            sql = sql.replace(executions[index], "'" + objects[index] + "'");
        }
        return sql;
    }
    /**
     * 解析注解上的值
     *
     * @param joinPoint
     * @return
     */
    public Object[] resolver(JoinPoint joinPoint, String[] executions) {
        if (executions == null || executions.length == 0) {
            return null;
        }

        /*
         * 提取变量
         */
        Integer len = executions.length;
        Object[] values = new Object[len];
        for(int index=0; index < len; index ++){
            if (executions[index].matches("#\\{\\D*\\}")) {// 如果name匹配上了#{},则把内容当作变量
                String newStr = executions[index].replaceAll("#\\{", "").replaceAll("\\}", "");
                if (newStr.contains(".")) { // 复杂类型
                    try {
                        values[index] = complexResolver(joinPoint, newStr);
                    } catch (Exception e) {
                        LOGGER.severe("操作轨迹解析器:解析复杂spel异常" + e.getCause().getMessage());
                    }
                } else {
                    values[index] = simpleResolver(joinPoint, newStr);
                }
            } else { //非变量
                values[index] = executions[index];
            }
        }
        return values;
    }

    private Object complexResolver(JoinPoint joinPoint, String str) throws Exception {

        String[] names = joinPoint.getParameterNames();
        Object[] args = joinPoint.getArgs();
        String[] strs = str.split("\\.");

        for (int i = 0; i < names.length; i++) {
            if (strs[0].equals(names[i])) {
                Object obj = args[i];

                Method dmethod = obj.getClass().getMethod(getMethodName(strs[1]));
                Object value = dmethod.invoke(args[i]);
                return getValue(value, 1, strs);
            }
        }
        return null;
    }

    private Object getValue(Object obj, int index, String[] strs) {
        try {
            if (obj != null && index < strs.length - 1) {
                Method method = obj.getClass().getMethod(getMethodName(strs[index + 1]));
                obj = method.invoke(obj);
                getValue(obj, index + 1, strs);
            }
            return obj;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    private String getMethodName(String name) {
        return "get" + name.replaceFirst(name.substring(0, 1), name.substring(0, 1).toUpperCase());
    }

    private Object simpleResolver(JoinPoint joinPoint, String str) {
        String[] names = joinPoint.getParameterNames();
        Object[] args = joinPoint.getArgs();

        for (int i = 0; i < names.length; i++) {
            if (str.equals(names[i])) {
                return args[i];
            }
        }
        return null;
    }

}

猜你喜欢

转载自blog.csdn.net/sunboylife/article/details/131054888
今日推荐