I'm trying different ways of getting rid of NPEs in my code. I use nullness analysis in Eclipse and I'm quite fond of @NonNullByDefault. But today I run into a problem with guava's Optional:
private static Optional<Foo> bar() {
Foo foobar = new Foo();
return Optional.of(foobar);
}
I get following comment on the return line:
Null type safety: The expression of type 'Optional<Foo>' needs unchecked conversion to conform to '@NonNull Optional<Foo>'
I tried using JSR305 annotations to no avail. Is there a way to make it work (use both Optional and @NonNull annotations)?
The full solution to this question requires null annotations in the Optional API. In particular of() should be declared to return @NonNull.
Until the library authors annotate their API, you will need external annotations, support for which is being developed for Eclipse Mars. See https://wiki.eclipse.org/JDT_Core/Null_Analysis/External_Annotations
OTOH, I'm curious, why indeed you want both concepts for NPE avoidance combined in one project.