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...
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With