好吧,我们有FunctionalInterface
:
public interface Consumer<T> {
void accept(T t);
}
我可以像这样使用它:
.handle(Integer p -> System.out.println(p * 2));
我们如何generic type
在代码中解析该lambda参数的实际值?
当我们将其用作内联实现时,Integer
从该类的方法中提取并没有那么困难。
我想念什么吗?或者只是Java不支持lambda类?
为了更清洁:
该lambda被MethodInvoker
(在中提到handle
)包裹,后者在execute(Message<?> message)
提取实际参数以进行进一步的反射方法调用。在此之前,它使用Spring的将提供的参数转换为目标参数ConversionService
。
handle
在这种情况下,方法是在实际应用程序工作之前进行一些配置。
不同的问题,但期望有相同问题的解决方案:Java:使用lambda参数获取通用方法的实际类型
我最近在TypeTools中添加了对解析lambda类型参数的支持。例如:
MapFunction<String, Integer> fn = str -> Integer.valueOf(str);
Class<?>[] typeArgs = TypeResolver.resolveRawArguments(MapFunction.class, fn.getClass());
解析的类型args符合预期:
assert typeArgs[0] == String.class;
assert typeArgs[1] == Integer.class;
注意:基础实现使用@danielbodart概述的ConstantPool方法,该方法已知可在Oracle JDK和OpenJDK上使用。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句