javalombokfunctional-interface

Does Lombok @Nonnull also have an effect for e.g. suppliers?


For the following declaration

public static <E extends SomeInterface> E methodName(
    @NonNull Supplier<@NonNull E> supplier,
    Consumer<@NonNull E> consumer)

does Lombok automatically null-check the suppliers result ? And/or does it make it make sense for the consumer to ensure that the E passed in is null-checked (e.g. by generating a null wrapper)?


Solution

  • As per the docs, @NonNull is purely documentary, except for 2 cases:

    That is it. Lombok does not generate any checks if you stick it on a method / on a return type, for example.

    This is intentional, and any PRs to change this behaviour would be denied. (SOURCE: I'm one of the 2 people who makes the final call on such things).

    To explain specifically why lombok does not and in fact cannot do anything meaningful to @NonNull within <>:

    Imagine you have a method taking an Iterable<String>.

    What do you expect lombok to do? Call .iterator() on the parameter, loop through to the end (keep iterating until hasNext() returns false), and report if any .next() call returns null? This:

    At best lombok can add null checks when you invoke your lambdas inside that method. But this doesn't actually prove much. What if you hand this lambda off to another method? For 'normal' nullity annotations (directly on a parameter), the @NonNull guarantees it's right and this guarantee survives passing the variable around. This simply isn't possible with generics.