简体   繁体   中英

Eclipse External Null Annotation for AbstractStreamEx.nonNull()

Consider the following example code. This code uses Eclipse's @NonNull and @Nullable annotations to check for null s. Unfortunately, Eclipse flags an error on the line map(toNonNull) .

import java.util.function.Function;
import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.jdt.annotation.Nullable;
import one.util.streamex.StreamEx;

Function<@NonNull Integer, @Nullable Integer> toNull;
Function<@NonNull Integer, @NonNull Integer> toNonNull;

toNull    = value -> value;
toNonNull = value -> value;

StreamEx.
   of(1, 2, 3, 4, 5, 6).
   map(toNull).
   nonNull().
   map(toNonNull).   // Error is here
   count();

Here's the error message:

Null type mismatch (type annotations): required 'Function' but this expression has type 'Function<@NonNull Integer, @NonNull Integer>'

I would like to create an Eclipse External Annotation on StreamEx 's nonNull() (or more precisely AbstractStreamEx 's nonNull() ) so that Eclipse will know that the values in the stream can not be null.

Here's the start of the Eclipse External Annotation.

class one/util/streamex/AbstractStreamEx<TS>

nonNull
 ()TS;
 ()???;

What do I put for ??? ?

For StreamEx 0.6.4 or newer, StreamEx overrides nonNull() so that one can use the following External Annotation:

nonNull
 ()Lone/util/streamex/StreamEx<TT;>;
 ()L1one/util/streamex/StreamEx<T1T;>;

For StreamEx 0.6.3 or older, one ugly solution is to change the Java code. This works around the compiler error.

StreamEx.
   of(1, 2, 3, 4, 5, 6).
   map(toNull).
   nonNull().
   map(item -> item != null ? toNonNull.apply(item) : null).
   count();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM