TypeFunction.java 函数式接口
@FunctionalInterface
public interface TypeFunction extends Serializable, Function {
static String getLambdaColumnName(Serializable lambda) {
try {
Method method = lambda.getClass().getDeclaredMethod("writeReplace");
method.setAccessible(Boolean.TRUE);
SerializedLambda serializedLambda = (SerializedLambda) method.invoke(lambda);
String getter = serializedLambda.getImplMethodName();
String fieldName = Introspector.decapitalize(getter.replace("get", ""));
return fieldName;
} catch (ReflectiveOperationException e) {
throw new RuntimeException(e);
}
}
}
LambdaUtils.java 获取字段名称工具类
public class LambdaUtils {
public static String getfieldName(TypeFunction typeFunction) {
return TypeFunction.getLambdaColumnName(typeFunction);
}
}
使用方式
public static void main(String[] args) {
// es 对应的实体类
ElasticOrder pddElasticOrder = new ElasticOrder();
String fieldName = LambdaUtils.getfieldName(ElasticOrder ::getOrderSn);
System.out.println("getfieldName = " + getfieldName);
}
打印结果:orderSn