0

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.

Joseph K. Strauss
  • 4,683
  • 1
  • 23
  • 40
  • Excuse me, but I believe I am asking a different question. That question is asking how to use lambda to define a toString, and I am asking how I can get an automatic, useful description. – Joseph K. Strauss Jan 04 '15 at 15:03
  • 1
    You more or less can't. – Louis Wasserman Jan 04 '15 at 17:20
  • @LouisWasserman What does 'more or less' mean? – Joseph K. Strauss Jan 04 '15 at 18:17
  • 1
    It means I needed a few more letters for StackOverflow to let me write the comment. – Louis Wasserman Jan 04 '15 at 18:18
  • 2
    If you want to “determine what class/method this lambda is calling” your question is still a duplicate, in that case of [this question](http://stackoverflow.com/q/21860875/2711488). There is no easy solution and the one shown there has its price (don’t use such things for other than debugging). Consider also [this](http://stackoverflow.com/a/26656912/2711488) – Holger Jan 12 '15 at 19:56
  • @Holger Guilty as accused. Thank you for the answer anyway. Sometimes these questions are hard to find. I was specifically looking for a solution to use for debugging. – Joseph K. Strauss Jan 12 '15 at 20:04
  • 2
    Since lambda instances are always accessed via one or more `interface`s, you can always create a functional identical [proxy](http://docs.oracle.com/javase/8/docs/api/java/lang/reflect/Proxy.html#newProxyInstance-java.lang.ClassLoader-java.lang.Class:A-java.lang.reflect.InvocationHandler-) which wraps the lambda instance and delegates the `interface` method calls to the lambda instance but handles the `toString` method. During debugging you might accept the additional overhead… – Holger Jan 13 '15 at 09:00
  • I created a sample implementation of Holger suggestion at: http://stackoverflow.com/a/42876841/1325574 – Sebastian Mar 18 '17 at 16:36

0 Answers0