关于Java中@SuppressWarnings的正确使用方法
作者:Archie_java 时间:2023-07-22 17:17:58
@SuppressWarnings比较常见,理解和使用起来都很简单。
通过源码可以看出,支持在类、属性、方法、参数、构造方法、本地变量上使用。
@SuppressWarnings注解的使用有三种:
@SuppressWarnings(“unchecked”) [抑制单类型的警告]
@SuppressWarnings(“unchecked”,“rawtypes”) [抑制多类型的警告]
@SuppressWarnings(“all”) [ 抑制所有类型的警告]
下面一个简单的例子:
public class Machine {
private List versions;
public void addVersion(String version) {
versions.add(version);
}
}
编译器会发出使用原始数据类型的警告
如果不想修复这个问题,可以通过@SuppressWarnings注解来抑制编译器的警告。
该注解允许指定忽略该方法的哪些特定警告。 不同的编译器或IDE的警告类型可能会不同,最常见的两种类型是deprecation和unchecked。
deprecation 告诉编译器忽略过时的方法或类型检查。
unchecked 告诉编译器忽略原始类型检查
public class Machine {
private List versions;
@SuppressWarnings("unchecked")
// or
@SuppressWarnings({"unchecked"})
public void addVersion(String version) {
versions.add(version);
}
}
如果想抑制多种类型的警告,可以采用字符串数组方式
@SuppressWarnings({<!--{C}%3C!%2D%2D%20%2D%2D%3E-->"unchecked", "deprecated"})
Eclipse Galileo版本支持的抑制警告的名称:
关键字 | 用途 |
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 (抑制确在switch中缺失breaks的警告) |
finally | to suppress warnings relative to finally block that don’t return (抑制finally模块没有返回的警告) |
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)(忽略没有完整的switch语句) |
nls | to suppress warnings relative to non-nls string literals( 忽略非nls格式的字符) |
null | to suppress warnings relative to null analysis( 忽略对null的操作) |
rawtypes | to suppress warnings relative to un-specific types when using generics on class params( 使用generics时忽略没有指定相应的类型) |
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( 忽略在serializable类中没有声明serialVersionUID变量) |
static-access | to 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( 抑制没被使用过的代码的警告) |
来源:https://lebron.blog.csdn.net/article/details/122386115
标签:Java,@SuppressWarnings,使用
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Java实现批量下载选中文件功能
2021-09-09 17:57:45
新手小心:c语言中强符号与弱符号的使用
2023-06-24 15:16:23
C#多线程学习之(四)使用线程池进行多线程的自动管理
2021-07-17 10:04:43
Mybatis中resultMap的Colum和property属性详解
2023-09-16 11:17:33
![](https://img.aspxhome.com/file/2023/8/58538_0s.png)
java JSONArray 遍历方式(2种)
2021-09-07 19:52:48
IntelliJ IDEA 中使用jRebel进行 Java 热部署教程图解
2023-04-20 19:23:42
![](https://img.aspxhome.com/file/2023/8/131768_0s.jpg)
Redis6搭建集群并在SpringBoot中使用RedisTemplate的实现
2023-10-31 14:48:05
![](https://img.aspxhome.com/file/2023/2/58622_0s.png)
Android View 事件防抖的两种方案
2022-02-04 15:46:52
编写简易Android天气应用的代码示例
2023-10-16 18:30:36
![](https://img.aspxhome.com/file/2023/7/139287_0s.jpg)
java项目构建Gradle的使用教程
2023-06-07 19:17:41
![](https://img.aspxhome.com/file/2023/9/84649_0s.png)
C#中 Json 序列化去掉null值的方法
2022-02-02 12:13:52
Java实现文件批量重命名具体实例
2023-04-07 15:41:20
![](https://img.aspxhome.com/file/2023/2/125652_0s.png)
Java 基础语法
2021-09-21 19:26:32
docker 的java编译环境构建详细介绍
2023-02-10 04:08:30
Java内存溢出实现原因及解决方案
2022-10-19 09:07:42
![](https://img.aspxhome.com/file/2023/6/129176_0s.png)
hibernate 中 fetch=FetchType.LAZY 懒加载失败处理方法
2023-11-08 09:40:31
冒泡排序算法原理及JAVA实现代码
2022-08-13 10:30:40
Android Studio多工程引用同一个library项目配置的解决方法
2022-04-07 16:39:40
![](https://img.aspxhome.com/file/2023/2/138972_0s.png)
Android编程实现播放视频的方法示例
2023-04-20 06:34:17
![](https://img.aspxhome.com/file/2023/0/106050_0s.jpg)
flyway实现java 自动升级SQL脚本的问题及解决方法
2021-10-14 00:23:19
![](https://img.aspxhome.com/file/2023/9/60399_0s.jpg)