Annotation Type Nullable


  • @Retention(RUNTIME)
    @Target(PARAMETER)
    public @interface Nullable
    Annotation to mark a parameter as nullable: Ops with nullable parameters should be callable with or without their Nullable arguments

    This annotation should only be specified on one of the signatures of the method (only on the FunctionalInterface's method, or only on the Op implementation, etc) for purposes of simplicity and readability. Writing

     public interface BiFunctionWithNullable<I1, I2, I3, O> extends
            Functions.Arity3<>
     {
    
            public O apply(I1 in1, I2 in2, @Nullable I3 in3);
     }
     
    and then writing an implementation
     public class Impl implements BiFunctionWithNullable<Double, Double, Double, Double> {
            public Double apply(Double in1, @Nullable Double in2, Double in3) {
            ...
            }
     }
     
    is confusing and hard to read. Which parameters are nullable in this case? Is it obvious that in3 is nullable just by looking at Impl? For this reason, it should be enforced that the annotation is only on one of the method signatures.

    Note also that annotations are currently (as of Java 11) not supported on lambdas. Nullable parameters are supported on interfaces and class implementations (including anonymous classes) but the following will not recognize the nullability of in:

    
     @OpField(names = "nullableLambda")
     public final Function<Integer, Float> nullableLambda = (
            @Nullable Integer in) -> {
            Integer nonNullIn = in == null ? 0 : in;
            return nonNullIn + 0.5f;
     };
     
    See also these SO posts: one, two
    Author:
    Gabriel Selzer