如果我们有代码:
@Contract("null, _, _ -> fail")
static void ifNull(Object object, ErrorType errorType, Object... args) throws ServiceException {
if (object == null) {
ExceptionFactory.throwServiceException(errorType, args);
}
}
Run Code Online (Sandbox Code Playgroud)
和
private boolean checkSeqValue(Sequence sequence, @Nonnull Number value) {
...
}
Run Code Online (Sandbox Code Playgroud)
和某处:
ifNull(value, ErrorType.NOT_FOUND, "Value for a sequence", "name = " + seqName);
if (checkSeqValue(sequence, value)) {
result = value;
}
Run Code Online (Sandbox Code Playgroud)
然后 SonarLint 插件显示了一个严重错误:
鱿鱼:S2637“@NonNull”值不应设置为空
对于此调用的 checkSeqValue 参数 2 已标记javax.annotation.Nonnull
但传递了 null
直接检查:
if (value == null) {
throw new ServiceException();
}
if …
Run Code Online (Sandbox Code Playgroud)