This is not a false positive. Your code is as follow:
if (obj.getOptional().isPresent()) {
obj.getOptional().get() // issue
}
There is no guarantee that the first getOptional() method execution returns the same object as the second getOptional() method execution. You have to store the value locally:
Optional<String> value = obj.getOptional()
if (value.isPresent()) {
value.get() // no issue
}
So you are saying, what if “Optional Object” is mutated between the “isPresent” and “get” call, in case of multithreaded app?
Hence chained call is not honored same as - assign to variable and use the same !?
if (obj.getOptional().isPresent()) {
// execution at this commented line - in case of multithreaded app.
obj.getOptional().get() // issue
}
@Rohit_kalia Your code suggests that aliMnsId is already an Optional<ID>. Your call Optional.ofNullable(aliMnsId) creates an Optional<Optional<ID>>, which (by convention) should never be empty because an Optional should never be set to null (rule java:S2789).
Thus your calls to isPresent() and get() are on different objects.