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

com.zys.mybatis.utils.LambdaUtils Maven / Gradle / Ivy

There is a newer version: 0.2.7
Show newest version
package com.zys.mybatis.utils;

import com.zys.mybatis.crud.SFunction;
import com.zys.mybatis.exception.MybatisZysException;

import java.lang.invoke.SerializedLambda;
import java.lang.ref.WeakReference;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author zys
 * @version 1.0
 * @date 2022/1/10 11:35
 */
public class LambdaUtils {

    private static final String METHOD_NAME = "writeReplace";

    /**
     * 缓存字段
     */
    private static final Map>> CACHE_COLUMN = new ConcurrentHashMap<>();

    public static List resolve(SFunction... lambda) {
        for (SFunction tsFunction : lambda) {
            String canonicalName = tsFunction.getClass().getCanonicalName();
            WeakReference> listWeakReference = CACHE_COLUMN.get(canonicalName);
            if (listWeakReference != null) {
                List serializedLambdas = listWeakReference.get();
                if (serializedLambdas != null) {
                    return serializedLambdas;
                }
                CACHE_COLUMN.remove(canonicalName);
                break;
            }
        }
        List slList = new ArrayList<>(lambda.length);
        String canonicalName = null;
        for (SFunction tsFunction : lambda) {
            Class clazz = tsFunction.getClass();
            canonicalName = clazz.getCanonicalName();
            try {
                Method writeReplace = clazz.getDeclaredMethod(METHOD_NAME);
                writeReplace.setAccessible(true);
                SerializedLambda sl = (SerializedLambda) writeReplace.invoke(tsFunction);
                slList.add(sl);
            } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
                throw new MybatisZysException("lambda字段解析失败", e);
            }
        }
        CACHE_COLUMN.put(canonicalName, new WeakReference<>(slList));
        return slList;
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy