For debugging purposes I am trying to create string representations of lambda expressions (specifically of Predicate
s, though it would be interesting for other lambda expressions too) in Java 8. My idea would be something like this:
public class Whatever {
private static <T> String predicateToString(Predicate<T> predicate) {
String representation = ... // do magic
return representation;
}
public static void main(String[] args) {
System.out.println(Whatever.<Integer>predicateToString(i -> i % 2 == 0));
}
}
And the output would be i -> i % 2 == 0
(or something logically equivalent). The toString()
method seems to be of no help, the output is just something like com.something.Whatever$$Lambda$1/1919892312@5e91993f
(which I guess is to be expected as toString()
is not overridden).
I'm not sure whether something like this is even possible, e.g. with reflection, I certainly haven't been able to find anything on it so far. Any ideas?