Is there any way to get an intelligible toString
for a lambda generated with a method handle? In pre-Java 8, I have been using the Function
interface from Guava, and I created a script that generated functions for many of my data classes. I also included a toString
that clearly identified the class and method that it was for, e.g., `MyItem::getPrice'.
However, with Java 8, although declaring functions have become much easier, I now have no way of identifying what the function is. The most important reason for nice toString
would be for debugging purposes, but I am sure that there are others as well. It would be nice if this information would be available with reflection, but it is apparently not.
This code snippet
Function<String,Integer> func = "ABS"::indexOf;
System.out.println(func);
System.out.println(func.getClass());
System.out.println(func.getClass().getSuperclass());
produces
com.github.jkstrauss.kavve.test.common.TestLambda$$Lambda$1/918221580@5ca881b5
class com.github.jkstrauss.kavve.test.common.TestLambda$$Lambda$1/918221580
class java.lang.Object
As you can see there is not even some lambda superclass to work with.
Clarification
I am not asking how I can define my own toString
, but how I can determine what class/method this lambda is calling. Defining my own toString
would defeat the whole purpose of using Java 8 lambdas since I would be required to do a lot more typo-prone work just to get a decent toString
. I might as well just use my automated script without lambdas.