Hacker News new | ask | show | jobs
by mrkeen 1312 days ago
Yes, it's always been possible to check for nulls at runtime.

Personally I use notNull(..) over @NonNull since it actually fires when you expect it to (as opposed to whether your framework dispatcher interceptor trigger decided to invoke it)

3 comments

I think he means using @NonNull for compile time checking, not instrumenting it for runtime checking (though that doesn’t hurt either).
isn't @NonNull just a syntactic sugar for adding checkNonNull() call as first statement to the function declaration in compile time, or am I mistaken? Just like lombok, it is supposed to generate code that checks null arguments, from what I know.
I tried:

    @Test
    public void foo() {
        bar(null);
    }

    void bar(@NonNull String param) {
        System.out.println(param);
    }
All versions of that annotation let the null right through. I tried with:

    import lombok.NonNull;
    import javax.validation.constraints.NotNull;
    import io.micronaut.core.annotation.NonNull;
    import org.springframework.lang.NonNull;
    import reactor.util.annotation.NonNull;
@Nullable/@NotNull is great when the IDE shows the warnings, basically dev time checking. There are also tools to integrate it into your builds for compile time checking.
I get a little green tick in the top right of my IDE window with the following:

    @Test
    public void foo() {
        final Map<String, Integer> map = new HashMap<>();
        map.put("present", 1);
        bar(map.get("missing"));
    }

    void bar(@NotNull Integer param) {
        System.out.println(param);
    }
"No problems found"
Right, that's because the system libraries don't have the annotations. That's the biggest issue with it. But it still helps a lot if you're religious about it in your own code.
Do the heuristics used for Kotlin-interop work with notNull(...)?