MybatisPlus Lambda 属性获取原理

青苗 青苗 | 402 | 2023-04-15

MybatisPlus Lambda 属性获取原理抛砖引玉核心如下

import java.io.Serializable;
import java.lang.invoke.SerializedLambda;
import java.lang.reflect.Method;
import java.util.function.Function;

public class Test {

    public class Entity {
        private String userName;

        public String getUserName() {
            return this.userName;
        }
    }

    public interface FieldFunction<T, R> extends Function<T, R>, Serializable {

    }

    public static <T> String getFieldName(FieldFunction<T, ?> func) {
        try {
            Method method = func.getClass().getDeclaredMethod("writeReplace");
            method.setAccessible(true);
            SerializedLambda serializedLambda = (SerializedLambda) method.invoke(func);
            String getter = serializedLambda.getImplMethodName();
            return "获取到方法名称 = " + getter;
        } catch (ReflectiveOperationException e) {
            throw new RuntimeException(e);
        }
    }

    public static void main(String[] args) {
        System.out.println(getFieldName(Entity::getUserName));
    }
}

文章标签: Java
推荐指数:

真诚点赞 诚不我欺~

MybatisPlus Lambda 属性获取原理

点赞 收藏 评论

关于作者

青苗
青苗

青苗幼儿园园长

等级 LV5

粉丝 20

获赞 47

经验 1182