我有一个方法签名,用于根据 Method 类型的给定 getter 方法使用反射来获取 setter 方法。 该方法的签名如下:
Method getSetter(final Method getterMethod, final Class classType)
现在,从类的方法中,我想要一个 getter 方法到关联的 setter 方法的映射。我的代码如下:
final Method[] methods = classType.getMethods();
Stream.of(methods)
.filter(ReflectionUtils::isGetter)
.collect(Collectors.toMap(Function.identity(), (method) -> getSetter(method, classType)));
我在 getSetter(method, classType) 处收到编译错误
它说发现错误的第一个类型参数:<lambda parameter>
,必填:java.lang.reflect.Method
我还尝试指定 lambda 参数的类型。见下文
Stream.of(methods)
.filter(ReflectionUtils::isGetter)
.collect(Collectors.toMap(Function.identity(), (Method method) -> getSetter(method, classType)));
现在它说无法推断功能接口(interface)类型。
最佳答案
您的代码可以在 javac 8u25、u40、u60、u71 以及 ecj 3.10.2 上正常运行。这里唯一的问题是原始的Class
发出警告的参数。应使用 <?>
对其进行参数化:
Method getSetter(final Method getterMethod, final Class<?> classType) { ... }
关于java - Collectors-map-java8 内 lambda 中的类类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36162798/