Community
Participate
Working Groups
In the program below, the method requireNonNull is invoked with @Nullable Iterable<@NonNull String> as argument, and therefore the returned value is @NonNull Iterable<@NonNull String>. Assigning the returned value to the field iterable is therefore safe. With Annotation-based null analysis enabled and "Unchecked conversion from non-annotated type to @NonNull type" set to warning, the null analysis issues a bogus warning: "The expression of type '@NonNull Iterable<String>' needs unchecked conversion to conform to '@NonNull Iterable<@NonNull String>'" at the line marked with (*) package foo.bar; import org.eclipse.jdt.annotation.NonNull; import org.eclipse.jdt.annotation.NonNullByDefault; import org.eclipse.jdt.annotation.Nullable; @NonNullByDefault({}) public abstract class Foo { abstract <T> @NonNull T requireNonNull(@Nullable T obj); @NonNull Iterable<@NonNull String> iterable; Foo(@Nullable Iterable<@NonNull String> iterable) { this.iterable = requireNonNull(iterable); // (*) } }
Thanks for the report, I'll take a look.
It's what I thought it is: When creating "the @NonNull variant of T" using T=@Nullable Iterable<@NonNull String> we not only drop the outer @Nullable (OK) but also the inner @NonNull (NOK). I'm currently investigating whether all seven sites calling type.unannotated(true) actually require removal only of the toplevel annotation or whether we'll need another parameter in that method... Depending on the result the fix can be quite simple.
Released for 4.5 M2 via http://git.eclipse.org/c/jdt/eclipse.jdt.core.git/commit/?id=8913fe5c78a14de5e97c4d6b801f5ed8424bf8a8 The patch looks bigger than might be expected, but much of it is just due to the divorce of two diverging methods: - TypeBinding.unannotated() with this change basically back to the simple version before bug 438458. - TypeBinding.withoutToplevelNullAnnotations() new method for shallow manipulation of null type annotations See also bug 441797 for an issue discovered during work on this bug.
*** Bug 441338 has been marked as a duplicate of this bug. ***
Verified for Mars 4.5M2 using I20140915-2000 build.