简体   繁体   English

通过构建器样式链式函数调用进行的未经检查/未经确认的Findbugs强制转换

[英]Findbugs Unchecked/unconfirmed cast from builder style chained function calls

FindBugs is reporting an Unchecked/Unconfirmed cast issue with the and() line in the following builder pattern code for configuring Spring security. FindBugs报告了以下用于构建Spring安全性的构建器模式代码中的and()行,存在未检查/未确认的强制转换问题。

public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
    auth
        .inMemoryAuthentication()
            .withUser("admin").password("secret").roles("ADMIN")
                .and()
            .withUser("user").password("secret").roles("USER");
}

The code works fine, how do I appease FindBugs? 该代码工作正常,如何安抚FindBugs?

Edit: 编辑:

As suggested by @h3xStream (in the comments below), if you run into a false positive with any code analysis tool, the best solution is to configure the tool to ignore the false positive and to take action to correct the code analysis tool. 正如@ h3xStream所建议的那样(在下面的评论中),如果您在使用任何代码分析工具时遇到误报,则最佳解决方案是将工具配置为忽略误报并采取措施纠正代码分析工具。 This of course assumes that it is indeed a false positive and that your code, in its current form, is correct and better left unaltered. 当然,这假定它确实是一个误报,并且您的当前形式的代码是正确的,最好不要更改。

In a pinch, you may be able to rewrite your code to keep the false positive from being triggered. 在紧要关头,您可能可以重写代码,以防止误报被触发。 That's what I ended up doing in this particular case, though it is really just a work around: 这就是我在此特定情况下要做的,尽管实际上只是一种解决方法:


I was able to stop the false positive from being triggered by updating the code to the following: 通过将代码更新为以下内容,我能够阻止误报触发:

public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
    InMemoryUserDetailsManagerConfigurer<AuthenticationManagerBuilder> conf 
            = auth.inMemoryAuthentication();
    conf.withUser("admin").password("secret").roles("ADMIN");
    conf.withUser("user").password("secret").roles("USER");
}

As I was no longer chaining the functions together, the return values became irrelevant and the false positive was no longer triggered. 由于我不再将函数链接在一起,因此返回值变得无关紧要,并且不再触发误报。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM