It's so sad that Java 8 had the chance to really fix the null problem, but gave us only the half-assed `java.util.Optional`. Rather than implementing optional values at the language level, it's just another class tossed into the JRE.
This is perfectly legal code, where the optional wrapper itself is null:
Optional getMiddleName() {
return null;
}
Don't people use @NonNull everywhere now? It's been a few years since I've programmed in Java but even then I feel like that was common practice.
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)
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;