How to print a Function<A,B>?

Viewed 73

I would like to create a central logger class that logs messages like:

Couldn't find a record of type MyClass when searching for field MyField

Currently, I have this piece of code:

public static <T, A, B> String logNotFound(final Class<T> type, String field) {
    return String.format(DATA_NOT_FOUND, type.getSimpleName(), field);
}

And I call it like:

Optional<Person> person = findPersonByLastName("Smith");
if (person.isEmpty()) logNotFound(Person.class, "lastName");

However, I don't quite like passing a string to the field name. I would like to call the log method as

logNotFound(Person.class, Person::getLastName)

passing a Function<A,B> as parameter. I expect a message like

Couldn't find a record of type Person when searching for field > Person::getLastName

Is there a way to do this?

2 Answers
Related