"Optional value should only be accessed after calling isPresent()" 尽管已为多个值签入

"Optional value should only be accessed after calling isPresent()" although checked in if for multiple values

我收到一个包含姓氏和名字的客户对象。在转换中,我检查两个值是否不为空,然后将它们传递给 DTO:

if (customer.getFirstName().isPresent() && customer.getLastName().isPresent()) {
      final String firstName = customer.getFirstName().get();
      final String lastName = customer.getLastName().get();
      // do assignment
}

但我仍然收到 Sonar 消息 只能在调用 isPresent() 后访问可选值

我是不是遗漏了什么或者这是误报?

如何使用 ifPresent :

customer.getFirstName().ifPresent(name1->
  customer.getLastName().ifPresent(name2->
      final String firstName = name1;
      final String lastName = name2;
  );
);