Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/mercurial/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 8 SonarLint警告,此代码将始终返回相同的值_Java 8_Optional_Sonarlint - Fatal编程技术网

Java 8 SonarLint警告,此代码将始终返回相同的值

Java 8 SonarLint警告,此代码将始终返回相同的值,java-8,optional,sonarlint,Java 8,Optional,Sonarlint,Sonarint告诉我: "Refactor this code to not always return the same value." 但我似乎不明白为什么 private List<InvoiceRejectionMessage> invoiceList = new ArrayList<>(); public String getMessage() { if(!invoiceList.isEmpty()) { return invoic

Sonarint告诉我:

"Refactor this code to not always return the same value."
但我似乎不明白为什么

private List<InvoiceRejectionMessage> invoiceList = new ArrayList<>();

public String getMessage() {
    if(!invoiceList.isEmpty()) {
        return invoiceList
                .stream()
                .max(comparing(InvoiceRejectionMessage::getCreatedAt))
                .get()
                .getMessage();
    }
    return null;
}
private List invoiceList=new ArrayList();
公共字符串getMessage(){
如果(!invoiceList.isEmpty()){
退货发票清单
.stream()
.max(比较(InvoiceRejectionMessage::getCreatedAt))
.get()
.getMessage();
}
返回null;
}
InvoiceList已定义并将始终初始化,因此它不能为null,只能为空。如果为空,则返回null。如果不是,我们确定其中有一个元素可以由.max()和.get()返回


仅仅因为Sonarint告诉我重构这个方法,我觉得不舒服,我更想知道为什么我会得到这个警告

与这个提示相关的规则是

squid:S3516-方法返回不应该是不变的

此规则的SonarQube实现可在上获得

如果不将代码视为一个整体,我无法100%确定为什么会触发此规则。然而,我怀疑声纳能够发现这一点

  • 发票列表
    无条件为空且为空;所以
  • if-then-else语句的
    if
    分支从未执行过;所以,
  • getMessage
    方法无条件返回
    null

  • 无论如何,没有必要将空列表视为特例;您可以通过以下方式简化代码,这可能会安抚声纳:

    private List<InvoiceRejectionMessage> invoiceList = new ArrayList<>();
    
    public String getMessage() {
        return invoiceList
                .stream()
                .max(comparing(InvoiceRejectionMessage::getCreatedAt))
                .map(InvoiceRejectionMessage::getMessage)
                .orElse(null);
    }
    

    invoiceList
    已初始化,但它是一个空数组。你永远不会
    。向它添加()。因此,
    .isEmpty()
    将返回
    true
    ,您的函数将返回
    null
    @RonNabuurs它是否检查代码库的其余部分是否有.add()?如果我在Intellij的测试项目中使用SonaLint复制粘贴代码段,即使我没有调用
    .add()
    ,我也不会得到错误。也许是某个地方的背景。恐怕我也不知道。风格问题:为什么不返回
    可选的
    而不是一个“null”
    字符串
    ?@Jubobs目前它唯一的用途是在一个我宁愿保持干净的DTO中;e、 setInvoiceMessage(invoice.getMessage)这确实使声纳平静下来,而且看起来更干净。不过我还是想知道为什么Sonar认为它“错了”,抱歉应该提到这个:squid:S3516与squid:S3655组合(它希望我在.get()之前调用isPresent,我理解它,但它没有用,因为当我们到达语句时它不会为null或空)。“可能是这两种情况的结合导致声纳认为情况比实际情况更糟吗?”劳伦斯我补充了我的答案。甚至Sonar也正确地指出,
    可选#get
    需要小心;事实上,这在这里是可以避免的。你没有具体解决我的问题,但你的答案是简洁的,并且很好地组合在一起,我将把这个标记为已解决,并将其归因于Sonarint的打嗝
    public Optional<String> getMessage() {
        return invoiceList
                .stream()
                .max(comparing(InvoiceRejectionMessage::getCreatedAt))
                .map(InvoiceRejectionMessage::getMessage)
    }