使用 lambda 和 JdbcTemplate 查询方法显示不明确的错误

sla*_*ron 4 java lambda spring-jdbc sonarqube

当我编译此代码时,它显示 [ERROR] 方法 query(String, Object[], ResultSetExtractor) 对于 JdbcTemplate 类型不明确

Collection<MyType> col = getJdbcTemplate().query(someQuery, new Object[]{param},
    rs -> {
        Map<Long, MyType> map = new HashMap();
        while (rs.next()) {
        // mapping logic
        }
        return map.values();
    });
Run Code Online (Sandbox Code Playgroud)

但是,如果我rs(ResultSetExtractor<Collection<MyType>>)某种方式对其进行转换,则会正确编译。

Collection<MyType> col = getJdbcTemplate().query(someQuery, new Object[]{param},
    (ResultSetExtractor<Collection<MyType>>)  rs -> {
        Map<Long, MyType> map = new HashMap();
        while (rs.next()) {
        // mapping logic
        }
        return map.values();
    });
Run Code Online (Sandbox Code Playgroud)

但是我的 IDE(带有声纳)将其报告为多余的演员,将 lambda 体内的所有内容报告为未使用。我正在使用 jdk 1.8.0_121

有人可以对此有所了解吗,谢谢

And*_*eas 5

JdbcTemplate有 3 个名为 的方法query,其中第一个参数是 a String,第二个参数是 an Object[]

前两个参数的第三个参数的功能接口采用一个类型为 的参数ResultSet

这就是为什么编译器需要一点帮助才能确定您的意思。

那个 IDE/Sonar 有缺陷,看不到强制转换是必要的,只是一个错误。