I have these classes with the method:
public abstract class Bar<T extends Bar> {
Foo myFoo;
public Optional<Foo> findFoo() { return Optional.ofNullable(this.myFoo); }
}
public class Baz extends Bar<Baz> {
}
, where Foo is a concrete final class and o is an object that extends Bar.
It ok to get the value in my unit test this way:
Bar bar = new Baz();
Optional<Foo> optinalFoo = bar.findFoo();
Foo foo = optionalFoo.get();
However, when called directly the type information is lost:
Foo foo = bar.findFoo().get();
In the second example the compiler thinks get() returns an Object instead of a Foo. Why? Is there a way to provide the type information to the compiler in some other way?
(I know that you should avoid calling get() directly but since this is in a unit test it's ok if it throws on unexpected results.)