public static <T, R> String getFieldName(SerializationFunction<T, R> function) {
Method method = null;
try {
method = function.getClass().getDeclaredMethod("writeReplace");
method.setAccessible(true);
SerializedLambda serializedLambda = (SerializedLambda) method.invoke(function);
String methodName = serializedLambda.getImplMethodName();
return methodName.substring(3);
} catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException e) {
throw new RuntimeException(e);
}
}
@FunctionalInterface
public interface SerializationFunction<T, R> extends Function<T, R>, Serializable {
}
例如:String propertyName = xxx.getFieldName(类名::getXXX);