r/java • u/benevanstech • Aug 12 '24
JSpecify 1.0.0 and Nullability in Java
https://www.infoq.com/news/2024/08/jspecify-java-nullability/5
u/uniVocity Aug 12 '24
“The primary use cases are expected to be variables, parameters and return values, but they can be used anywhere that tracking nullness makes conceptual sense.”
I’d really love to be able to annotate a class as @NotNull, or implement some NotNull interface that won’t allow my code to compile if any instance of this class or subclasses could be passed around as null.
This would reduce the need to litter method parameters with @NotNull, or to add the suggested ? or unless necessary (when classes aren’t declared as notNull)
5
u/kevinb9n Aug 12 '24
It's actually very rare to need to write `@NonNull`. We almost didn't even include it in the jar! Just put `@NullMarked` on your class/package/module instead.
The feature you seem to be asking for was discussed a bit in https://github.com/jspecify/jspecify/issues/7 but overall it just doesn't have that many advantages.
2
Aug 13 '24
Are there any examples available that show how to specify
@NullMarkedat the package or module level? I've combed through the JSpecify docs, but haven't found any.3
u/kevinb9n Aug 14 '24
You put it before `package` in `package-info.java`, or before `module` in `module-info.java`.
1
u/uniVocity Aug 13 '24 edited Aug 13 '24
Thanks! That was a great discussion and I'm inclined to say you guys made the right call as handling that would add complexity.
Anyway, I still think it could be a decent feature to have later on. Basically, my suggestion boils down to making the compiler treat selected classes as it treats primitive types. That would not burden developers and users of my code to worry about adding annotations at all.
I'm highlighting the main argument made in that discussion you linked:
I don't think there is any such thing as a class that should viewed as "never let this be nullable". As soon as you try to get that thing out of a Map, there you are.
An analogous situation is when you try to get stuff out of a map. You'll get a NullPointerException at runtime, por example, assuming
mapis aHashMap<String, Integer>:int i = map.get("nothing"); //NullPointer exceptionNot perfect but not that unexpected.
With primitives you can't do this either:
int i = null;as the compiler yells with "java: incompatible types: <nulltype> cannot be converted to int"So declaring
MyNotNullType t = null;should cause a similar error, probably "java: incompatible types: <nulltype> cannot be converted to MyNotNullType"Analogously, we can't use methods like this:
public void execute(){ //compilation error - java: incompatible types: //<nulltype> cannot be converted to int doSomething(null); } public void doSomething(int v){ //do stuff }A "@NotNull" class could make the compiler enforce no null arguments are passed into methods, without littering the code with multiple @NotNull and @Nullable annotations. Personally, I'd rather avoid having to add these annotations everywhere - I feel we are simply replacing null checks with null annotations, and making the issue of dealing with nulls even more apparent.
1
u/GalacticBear91 Aug 12 '24
Good idea, you should suggest it on the project forums itself (the Google group seemed pretty active when I checked)
2
u/lurker_in_spirit Aug 12 '24
InfoQ: What's next for JSpecify to tackle?
Hoeller: From the Spring side, we need official meta-annotation support so that we can declare our own nullability annotations with JSpecify meta-annotations instead of JSR 305 meta-annotations.
This is interesting... is it on the table for a JSpecify 1.1.0 (or similar) release in the near future?
4
u/kevinb9n Aug 13 '24
Yes, what we'd like is for Spring to be able to add this to their own Nullable annotation interface:
`@Implies(org.jspecify.annotations.Nullable.class)`
And for any JSpecify-conformant tool to thereby automatically treat theirs as implicitly meaning everything that ours does.
This would be a path toward eventually these tools not having to have big hardcoded lists of annotation types anymore.
Specifying `@Implies` just got shockingly complicated so we punted it for a while.
2
u/pragmasoft Aug 12 '24
This has IMO the same problem as JPMS modules - adoption. In order to be useful, you need to have 100% of your dependencies including transitive ones annotated, or being able somehow to write some type definitions for 3rd party libraries, like Typescript does. Kotlin solves this differently, so without a language support JSpecify or any other nullability annotations simply add more harm than use.
5
u/kevinb9n Aug 12 '24
It's true that the more your dependencies are annotated, the more useful it becomes. But there is no "cliff" like you suggest. Nullness is tri-state: nullable, non-null, and "unspecified nullness" which is handled leniently. This makes it a lot easier to adopt than the original checker framework two-state, soundness-based approach. Any library that isn't providing the information just isn't going to be as well-validated, that's all.
And you can write type definitions for third-party libraries. Checker framework has a tool for reannotating class files, and I assume Google will opensource theirs when the demand is there. They use it to munge nullness annotations into JDK class files and it works!
I hope this changes your impression somewhat, but if not, sorry!
3
u/ventuspilot Aug 14 '24
But there is no "cliff" like you suggest.
Can confirm: in a toy project of mine I annotated a few methods to return @NotNull and IntelliJ Idea's inspections showed me a few unneeded null checks. Very little effort for immediate profit.
1
u/pragmasoft Aug 13 '24
I may reconsider if there's an easy way to at least download annotated most recent jdk libraries and include them into my maven build as an extra dependency. That's only the first step actually, but even it has not been made so far as I understand.
If I need to do this myself - no deal, too bad dx.
Unspecified nullness doesn't help either - that's effectively the same as nullable.
3
u/kevinb9n Aug 13 '24
Unspecified nullness doesn't help either - that's effectively the same as nullable.
That's... really quite untrue, but you can let me know if you're interested in that discussion or not!
2
u/pragmasoft Aug 13 '24
Sure I am, maybe I didn't understand the concept properly. I definitely played with the JSpecify earlier but had to get rid of it due to the reasons I described above.
2
u/pragmasoft Aug 13 '24
Ok, I seems realized the source of my confusion, just reread https://github.com/jspecify/jspecify/wiki/nullness-unspecified
From what I understood, unspecified is like any type in TS, can be cast without an error to both null and not-null, and vice versa, they both can be cast to unspecified.
The problem is though that my IDE support of nullable types (VSCode) does not support this, so the correct behavior depends on the tooling mostly.
Just curious, if it's possible to implement null checker as an annotation processor, or more precisely as a javac compiler plugin, similar to google error-prone?
This would then have automatic IDE support for errors I assume..
4
u/kevinb9n Aug 13 '24
Yep, NullAway actually builds on Error Prone to do this in javac.
We do need tools to converge around supporting JSpecify semantics; it's kind of the whole point, but it's in their hands now. Quite a few have been involved at some level, but yeah In particular we haven't talked to anyone from VSCode yet, and we really need to.
26
u/syneil86 Aug 12 '24
There is currently a draft JEP trying to address this in the language itself, but I'm very glad that the community is trying to address the nullability issue themselves in the meanwhile
https://openjdk.org/jeps/8316779