Java: how to resolve lambda parameters of wildcard types?
Then we have a functional interface:
public interface Consumer<T> { void accept(T t); }
I can use it:
.handle(Integer p -> System.out.println(p * 2));
How do we resolve the actual generic type of this lambda parameter in code?
When we use it as an inline implementation, it is not difficult to extract integers from the methods of this class
Do I miss anything? Or just Java doesn't support lambda classes?
To be cleaner:
The lambda is wrapped in methodinvoker (in the handle), and the actual parameters are extracted in its execution (message message) for further reflection method calls Previously, it used spring's conversionservice to convert the supplied parameters into target parameters
In this case, method handles are configurators before the actual application works
Different problems, but expectations for solutions to the same problems: Java: get actual type of generic method with lambda parameter
Solution
I recently added support for resolving lambda type parameters to typetools For example:
MapFunction<String,Integer> fn = str -> Integer.valueOf(str); Class<?>[] typeArgs = TypeResolver.resolveRawArguments(MapFunction.class,fn.getClass());
The resolved type parameters are as expected:
assert typeArgs[0] == String.class; assert typeArgs[1] == Integer.class;
Note: the underlying implementation uses the constantpool method outlined by @ danielbodart, which is known to work on Oracle JDK and openjdk