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));
}
}
推荐指数:
真诚点赞 诚不我欺~