【持续更新】Java 中使用 @SuppressWarnings 警告压制汇总

不管现实多么惨不忍睹,都要持之以恒地相信,这只是黎明前短暂的黑暗而已。不要惶恐眼前的难关迈不过去,不要担心此刻的付出没有回报,别再花时间等待天降好运。真诚做人,努力做事!你想要的,岁月都会给你。【持续更新】Java 中使用 @SuppressWarnings 警告压制汇总,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com,来源:原文

根据个人实际代码中遇到的警告问题进行汇总,持续更新。

通过 @SuppressWarnings(“关键字”) 进行抑制警告,一般我们对警告的处理是要么抑制要么修复。

标准用法

一般常用在普通 JavaSE 项目中。

关键字 用途
all to suppress all warnings
boxing  to suppress warnings relative to boxing/unboxing operations
cast to suppress warnings relative to cast operations
dep-ann to suppress warnings relative to deprecated annotation
deprecation to suppress warnings relative to deprecation
fallthrough  to suppress warnings relative to missing breaks in switch statements
finally  to suppress warnings relative to finally block that don’t return
hiding to suppress warnings relative to locals that hide variable
incomplete-switch  to suppress warnings relative to missing entries in a switch statement (enum case)
nls  to suppress warnings relative to non-nls string literals
null to suppress warnings relative to null analysis
rawtypes to suppress warnings relative to un-specific types when using generics on class params
restriction to suppress warnings relative to usage of discouraged or forbidden references
serial to suppress warnings relative to missing serialVersionUID field for a serializable class
static-access o suppress warnings relative to incorrect static access
synthetic-access   to suppress warnings relative to unoptimized access from inner classes
unchecked  to suppress warnings relative to unchecked operations
unqualified-field-access to suppress warnings relative to field access unqualified
unused to suppress warnings relative to unused code

idea

idea 给普通 JavaSE 提供的语法警告。

关键字 用途
UnnecessaryLocalVariable Local variable ‘userId’ is redundant
FieldCanBeLocal Field can be converted to a local variable

个人觉得 Local variable ‘xxx’ is redundant 这种提示你做 inline 处理的其实没必要进行 inline,就 debug (不用点进去再勾选方法结束,勾选易卡顿)和可读性而言我个人是不喜欢做 inline 处理的。由于还存在 javac 编译时优化,源码上多一个变量,编译后执行也不会有性能问题,因此可以关闭 idea 的警告。

关闭,但未完全关闭

Spring

idea 在 Spring 以及 SpringBoot 等 Spring 的衍生项目中提供的语法警告。

关键字 用途
SpringJavaInjectionPointsAutowiringInspection Could not autowire. No beans of ‘XXX’ type found.
SpringJavaAutowiredFieldsWarningInspection Field injection is not recommended
ConfigurationProperties Not registered via @EnableConfigurationProperties or marked as Spring component
@SuppressWarnings(“ConstantConditions”) Condition ‘xxx != null’ is always ‘true’

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/180249.html

(0)
飞熊的头像飞熊bm

相关推荐

发表回复

登录后才能评论
极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!