eclipse-jdt / eclipse.jdt.core

Eclipse Public License 2.0
157 stars 125 forks source link

[feat] Add support for polymorphic external null annotations #2800

Open sebthom opened 1 month ago

sebthom commented 1 month ago

This is a follow-up issue to https://bugs.eclipse.org/bugs/show_bug.cgi?id=575822

I am looking for a way to declare the nullability of a return value depending on nullability of one (or possibly more) input values. This is an example from commons-lang3 StringUtils:

public static String join(final boolean[] array, final char delimiter) {
    if (array == null) {
        return null;
    }
    return join(array, delimiter, 0, array.length);
}

As suggested in https://bugs.eclipse.org/bugs/show_bug.cgi?id=575822, it would be great to make it work by being able to specify something like:

join
 ([ZC)Ljava/lang/String;
 ([0ZC)L0java/lang/String;
 ([1ZC)L1java/lang/String;

This would also be great for the java.util.Optional class:

Optional<String> opt = ...
@Nullable String result1 = opt.orElse(null);
@NonNull String result2 = opt.orElse("else");

when one could specify these EEAs:

orElse
 (TT;)TT;
 (T0T;)T0T;
 (T1T;)T1T;
stephan-herrmann commented 1 month ago

In the light of #2783 also this is of interest: https://github.com/jspecify/jspecify/issues/79 IOW: if Jspecify would agree on @PolyNull then this would be a strong reason for ecj to follow suit.