属性和对象报黄线怎么办
在开发中,声明变量或者是new对象什么的,如果没有调用会报黄线,在程序里是不影响运行的,但是在上线时有可能会被扫描出bug,所以我们要尽可能的进行调用,也可以采用第二种方法:加注解 @SuppressWarings
@SuppressWarings注解 作用:用于抑制编译器产生警告信息。
其注解目标为类、字段、函数、函数入参、构造函数和函数的局部变量。而家建议注解应声明在最接近警告发生的位置。
示例1——抑制单类型的警告:
@SuppressWarnings("unchecked")
public void addArgues(String argue){
@SuppressWarnings("rawtypes")
List items = new ArrayList();
items.add(argue);
}
示例2——抑制多类型的警告:
@SuppressWarnings(value={"unchecked", "rawtypes"})
public void addArgues(String argue){
List items = new ArrayList();
items.add(argue);
}
示例3——抑制所有类型的警告:
@SuppressWarnings("all")
public void addArgues(String item){
List items = new ArrayList();
items.add(argue);
}
抑制警告的关键字
关键字 用途
all to suppress all warnings
相对于null分析来抑制警告
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)
抑制警告相对缺少的条目在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
联合国警告相对抑制特定类型时使用泛型类参数
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 o suppress warnings relative to incorrect static access
o相对于不正确的静态访问抑制警告
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
禁止相对于未使用的代码发出警告