r/learnjava • u/josephjnk • 9h ago
How do you check null safety when publishing a Java library?
I’m gearing up to publish my first Java library in the next few weeks. I’m using jspecify for null checking, with `NullMarked` annotations on my packages. Using my library requires implementing a subclass of an abstract class and overriding a method whose return value is marked as `NonNull`.
When I implement a subclass from my test suite, if I don’t have NullMarked enabled or if I don’t attach a `NonNull` annotation to the overridden method then I get a warning: `Not annotated method overrides method annotated with NullMarked`. So far so good; this seems like the tool doing its job by catching a potential source of errors.
Here’s my problem though: what if a consumer of my library doesn’t want to use jspecify? Am I dooming them to eternal build warnings? Is there a better way for me to have null checking in my library’s internal implementation and to publish it with useful `NonNull` annotations without locking consumers into a particular null checking dependency?