Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get return type of constructor lambda

I was wondering if it is possible to get the return type of a Supplier that was assigned to a constructor.

E.g.

Supplier<Foo> sFoo = Foo::new;

How do I get "Foo.class" from the supplier? I have been using typetools to solve this problem for other things.

This works, for example:

Supplier<Foo> sFoo = () -> new Foo();
Class<?> fooClasss = net.jodah.typetools.TypeResolver.resolveRawArguments(Supplier.class, sFoo.getClass())[0];
// fooClass == Foo.class

But if I assign Supplier like: Supplier<Foo> sFoo = Foo::new, the return type cannot be resolved...

Any thoughts? I don't have to use typetools btw...

like image 934
akagixxer Avatar asked Oct 31 '22 11:10

akagixxer


1 Answers

Seems that parsing method references is not supported currently by typeTools. There's an open issue with the similar problem.

In general such feature is quite fragile as runtime lambda representation is not specified and implementation dependent. So it may break one day. If you actually need a class I would suggest passing the actual Class<?> argument.

like image 128
Tagir Valeev Avatar answered Nov 09 '22 09:11

Tagir Valeev