我有以下Java代码:
String func() {
Object data = otherFunc();
return data.toString();
}
otherFunc()是@Nullable函数,它可以返回null。因此,IDE强调toString()方法是危险的,因为它可能导致NullPointerException。
但就我而言,我对otherFunc()的非空结果有100%的信心。因此,我在此处添加了一个断言条件:
String func() {
Object data = otherFunc();
MyAssert.notNull(data, some_debug_info);
return data.toString();
}
MyAssert.notNull()调用中有一个空检查验证,如果为空,则会引发异常(带有一些其他调试信息)。Android Studio仍然突出显示toString()...
问题:
在调用MyAssert.notNull()之后,如何指示“数据”对象不能为null?也许有一些@annotations?
代码中有很多类似的地方,因此该解决方案仅应修改assert函数!
森林海
相关分类