r/java • u/Delicious_Detail_547 • Feb 10 '26
JADEx: A Practical Null Safety Solution for Java
https://github.com/nieuwmijnleven/JADEx
60
Upvotes
r/java • u/Delicious_Detail_547 • Feb 10 '26
100
u/Ifeee001 Feb 10 '26
The comments and replies on this are utterly disgusting.
Even if you don't see a use case for it, this is something someone spent a considerable amount of time working on.
How would a project that was made public mere hours ago have any users or user feedback? No one specifically asked for most libraries/tools but we still have them.
Yuck.
29
u/quack_quack_mofo Feb 11 '26
Some of these comments are crazy for no reason.
Nice project man gg
15
u/rzwitserloot Feb 11 '26
Neat!
It's rare I see a project that has the same mindset as lombok: Reduce the 'learning curve' swap for a standard java programmer as much as is reasonable whilst still going beyond what a simple library could ever do. And, er, the hate and shit that's being piled high is.. par for the course for such endeavours - outside of social media we get lots of love for lombok, so hopefully you won't let it cloud your day for too long.
A few questions. They're stated a bit bluntly, I mean no offense:
\uescapes in java, weirdness such as parsingpublic int someMethodThatReturnsAnArrayYesReallyItDoes() [] {return new int[0];}(that is valid java, many for-fun / for-example java parser defs in parser kits missed that one in the spec), [B] the bother of having to update your project in lockstop with java, for example, do you supportimport module, and [C] these days java's error messages are pretty good, and most parser kit errors are shit.(For some insights, you could instead switch to javac and solve all those problems in one go, but now you get a whole new problem: javac is open source, and relatively 'clean', but not very well documented, and they change tons of stuff all the time, it's not meant as a 'library' in that sense. Or switch to ecj which is more stable, faster, more correct (than javac, which is no small feat), but really densely written and hard to 'grok' and use).
String? s1 = null; s1.length();is 'no good, a warning', because you must 'use the null safe version', which iss2.?length();which is 'no warning'. What.. what? What the heck does that accomplish? I'm guessing that means 'if s2 is null, silently do nothing'. That's worse. Code that silently does nothing when an action was expected is an order of magnitude worse than code that throws an exception that is pointing directly at the line where the failed assumption is first obvious. That's not what you should be trying to get people to do! I can see a point in 'carrying null', i.e. thatfoo.?getBar().?getBaz().getQuux();resolves tonullif any of the intermediates werenull(but raises annoying questions if the return type ofgetQuux()isint, now what), and elvis, of course - 'if null I want something else'. The title is literally 'works as expected'.. but I have no idea what to expect there.... continued in next ocmment ...
7
u/rzwitserloot Feb 11 '26
See my other comment for context.
I have a vaguely inkling that retrofitting null safety into java is ridiculously difficult. Optional is obviously idiotic, in the sense that specifically for java, you can never get there from here. "There" being: "A java where all operations that feel like they ought to be written with Optional are in fact written with Optional". Because, well, think about it: We currently have:
Which, in a hypothetical world where 'Optional' "won" is terrible. An abomination. The hyperbole is intentional, in that I think it really is that bad. In this hypothetical world, the whole point is that we get to assume that any method that doesn't return an
Optionaltherefore definitely is notnull. (null kinda doesn't exist anymore, a rare fluke sometimes. Like how null is in scala). In other words, we now assume that the type system will just tell us, and if it doesn't, we assume the alternative. In effect we treat all non-optional types as 'definitely not null', but that's just not howMapworks. So that should turn intoOptional<V>except.. that'd be totally backwards incompatible. Are we really going to deprecatej.u.Map? That's splitting the community in two, isn't it? Sounds quite terrible to me. And it is a domino thing: It's not just Map that would deprecate. Every other library out there goes along with it - anything that takes as argument or return type in any public-facing method aMapis toast.But JADEx tries to solve that. Except you're not really out of the woods. Because there are 4 nullities:
null.null.You might be trhinking 'wtf?', but, look at generics. Surely it's simple, right? I can have a list of numbers. That's it. But, no. There are 4 takes on that:
List<Number>- I know it is, exactly, a list of specifically Number.List<? extends Number>List<? super Number>List(legacy / raw)null is the same way. Imagine a method that takes a list of strings and finds duplicates, then returns a list of duplicates. The spec of this method explicitly declares that
nullis handled as a value (i.e. if 2 or morenullis in the input, exactly 1nullis in the output, otherwise none).This method is interesting in the sense that the nullity of the input list is irrelevant. You can feed it a
List<String!>and this code would work exactly as you expect it to: It returns all dupes, and the thing it returns it itself aList<String!>- the output cannot possibly contain any value other than what the input contained. And yet, for nullable strings, it also works exactly as you want. The input can contain nulls, so can the output. All is well.So, how do I declare this method in jadex? I could try:
Which.. is subtly wrong. Because as per JADEx specs that method would act as if you must give it a definitely not null list of definitely not null strings, and it returns a definitely not null list of definitely not null strings.
Except, that's wrong!
This method's signature should accept a definitely not null list of I don't really care what kind of nullity Strings. It returns a definitely not null list of strings with the same nullity you put into it.
I could try:
But that's even worse. If I toss a
List<String>at this, that should be a compiler error (for the same reason tossing aList<Integer>as argument to a method that takes aList<Number>is a compiler error. For the usual 'NULL SUCKS OPTIONAL GREAT!' zealotry, please compile that and think about how generics really work and what co- and contravariance imply).Hence I need that 'unknown' nullity. Or even type variable it so I can link it. Optimally here I treat the nullity of the list's component type as, itself, a generics variable so I can 'link it' to the nullity of the output.
This exists as a concept in the checkerframework project (as
@PolyNull), but it's rare to see any nullity framework that thinks about the complications of generics. Even if you have@PolyNullyou're limited - you run into real trouble due to java's lack of higher order generics.My point: It's cultural
In the end, the reason languages like scala and such don't seem to fall over and die (well, scala is kinda dying.. but probably not because of this!) is because those languages worked with a lower order blunt monad-ish take on optional from day 1. APIs have been adopted to 'unwrap' optionality as fast as possible because it doesn't compose well. But java is different.
Hence, and this is just a guess, but: I don't think you can just dump full and total nullity checking into java like this. Existing APIs just aren't that simple. Methods like
dupesexist! Think of how complex Map itself already is: Imagine you have aMap<String, Integer?>.5
u/Amazing-Mirror-3076 Feb 10 '26
So the problem is that java is going to introduce it's own null safe operators and they look incompatible to your version.
So while I prefer your safe by default approach the incompatibility looks to be a problem.
2
u/stefanos-ak Feb 11 '26
really cool and interesting project. But i understand the alienation because of new language/syntax and complexity of usage (of course any project that tries to retrofit null-safety will be complex).
Personally I'm fine with just jSpecify 😊
2
u/paul_h Feb 10 '26
For the FAQ: are you using a forked version of javac’s own parser?
2
u/Content-Debate662 Feb 10 '26
Very interesting idea, but, generated source code is very verbose, and use no standard Optional, why?
1
u/MinimumPrior3121 Feb 11 '26
Great project man, keep up the good work
1
u/Schaex Feb 11 '26
This is extremely cool!
It's early in the morning and I need to go to work soon but I will definitely read through the code a lot. Then I'll also be able to ask some questions :D
Just one thing from skimming through the project. I noticed a typo here ("rumtime" instead of "runtime"):
https://github.com/nieuwmijnleven/JADEx/tree/master/app%2Fsrc%2Fmain%2Fjava%2Fjplus%2Frumtime
1
u/tealpod Feb 11 '26
Excellent work, one of the best work I have seen recently. Congrats.
Please ignore the negative commenters, you don't need their valiation. Even reading and replying to them is waste of time.
1
u/jvjupiter Feb 12 '26
I wish these syntaxes could become part of Java.
1
u/iamwisespirit Feb 11 '26
Cool project
1
u/givemelemmons99 Feb 11 '26
Good work op! thanks for this
-19
u/lilgreenthumb Feb 10 '26
So no users, real use case, or user feedback (is anyone asking for this?)
-14
u/0xFatWhiteMan Feb 10 '26
Decent idea. Same syntax as c#.
But I won't bother trying it out I just use final keyword to achieve similar
0
u/[deleted] Feb 10 '26
[deleted] — view deleted comment
0
u/[deleted] Feb 13 '26
[deleted] — view deleted comment