使用lambda表达式获取方法名称
Mybatis-plus lambda 中的效果
例子:
public class LambdaUtils {
@SneakyThrows
public static <T> String getFieldName(FieldFunction<T, ?> func) {
Method method = func.getClass().getDeclaredMethod("writeReplace");
ReflectionUtils.makeAccessible(method);
SerializedLambda serializedLambda = (SerializedLambda) method.invoke(func);
return serializedLambda.getImplMethodName();
}
interface FieldFunction<T, R> extends Function<T, R>, Serializable {
}
@Data
static class User {
private String name;
private Integer age;
}
public static void main(String[] args) {
System.out.println(getFieldName(User::getName));
}
}
版权声明:
本站所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自
Blog!
喜欢就支持一下吧