Class 在 collectors-map-java8 中输入 lambda

Class type in lambda inside collectors-map-java8

我有一个方法签名,用于根据类型为方法的给定 getter 方法使用反射获取 setter 方法。 该方法的签名如下:

Method getSetter(final Method getterMethod, final Class classType)

现在从 class 的方法中,我想要一个 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)));

现在它说无法推断功能接口类型。

您的代码适用于 javac 8u25、u40、u60、u71 以及 ecj 3.10.2。这里唯一的问题是发出警告的原始 Class 参数。它应该用 <?>:

参数化
Method getSetter(final Method getterMethod, final Class<?> classType) { ... }