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

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

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

标准用法

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

关键字用途
allto suppress all warnings
boxing to suppress warnings relative to boxing/unboxing operations
castto suppress warnings relative to cast operations
dep-annto suppress warnings relative to deprecated annotation
deprecationto 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
hidingto 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
nullto suppress warnings relative to null analysis
rawtypesto suppress warnings relative to un-specific types when using generics on class params
restrictionto suppress warnings relative to usage of discouraged or forbidden references
serialto suppress warnings relative to missing serialVersionUID field for a serializable class
static-accesso 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-accessto suppress warnings relative to field access unqualified
unusedto suppress warnings relative to unused code

idea

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

关键字用途
UnnecessaryLocalVariableLocal variable ‘userId’ is redundant
FieldCanBeLocalField can be converted to a local variable

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

关闭,但未完全关闭

Spring

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

关键字用途
SpringJavaInjectionPointsAutowiringInspectionCould not autowire. No beans of ‘XXX’ type found.
SpringJavaAutowiredFieldsWarningInspectionField injection is not recommended
ConfigurationPropertiesNot registered via @EnableConfigurationProperties or marked as Spring component
@SuppressWarnings(“ConstantConditions”)Condition ‘xxx != null’ is always ‘true’
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值