本文主要是介绍@SuppressWarnings 注解详解,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
在编码时会发现eclipse总会提示一些变量未被使用的提示信息:
上述代码编译通过且可以运行,但每行前面的“感叹号”对有代码强迫症的“猿”来说就心情不畅了。这时我们可以在方法前添加@SuppressWarnings 注解去除这些warning。
作用:用于抑制编译器产生的警告信息。注解目标为类、字段、方法、方法入参、构造函数和函数的局部变量。建议注解应声明在最接近警告发生的位置。
示例一:抑制单类型警告
@SuppressWarnings("unused")
private static PdfPCell cellValue(String value){Font font = new Font(getBaseFontChinese(), 14, Font.NORMAL);PdfPCell cell = new PdfPCell(new Paragraph(value, font));//cell.setHorizontalAlignment(Element.ALIGN_CENTER);cell.setVerticalAlignment(Element.ALIGN_MIDDLE);cell.setFixedHeight(40F);return cell;
}
示例二:抑制多类型警告
@SuppressWarnings(value= {"unused", "unchecked"})
public class DeviceDeployPdf extends BasePdfTemplate{Class ....
}
示例三:抑制所有类型警告
@SuppressWarnings("all")
public class DeviceDeployPdf extends BasePdfTemplate{Class....
}
抑制警告的关键字:
关键字 | 用途 |
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语句中丢失的中断的相关的警告 |
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 抑制与non-nls字符串相关的警告 |
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 抑制与可序列化类缺少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://www.cnblogs.com/fsjohnhuang/p/4040785.html
这篇关于@SuppressWarnings 注解详解的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!