-
Notifications
You must be signed in to change notification settings - Fork 55
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support all varieties of Null handling #111
Comments
I'm opening this new issue to deal with any Null issues in Record-Builder. I'd like to stipulate a few things to keep in mind for this discussion and any solutions:
Given this - what what needs to be done here. I disagree that the current implementation is wrong in any way. We can use the options to generate classes as needed. So, what should the generation be for the purposes of this issue? cc @agentgt |
See my comment here: #106 (comment) Pasted below here for others: EDIT the below is not for runtime support (e.g. @Randgalt I'm sorry I just don't have time anymore. One of the reasons I was "flooding the inbox" was that was my window to work on your project. That was why I was being urgent. As for production code... you have never seen or used?
Anyway it doesn't really matter because I think the best thing to do is something like what Immutables does and provide your own annotation say Then wherever you like to do configuration (perhaps package level annotations) users can specify what annotation they would like propagated. This is somewhat how jOOQ works. For example record MyRecord( @MyNullable /* the previous annotation will not be read by record builder */ @RecordBuilderNullable String field) {} Then in configuration you say RecordBuilderNullable = MyNullable.class. Then when you generate any method you just bring along The only issue at that point is pure Going back to the example if MyRecordBuilder with(java.lang. @MyNullable String field); instead of MyRecordBuilder with(@MyNullable java.lang.String field); I think but am not sure some generators get around this issue by importing instead of FQN but that will eventually fail. The above practice will avoid the nasty issue of pulling TYPE_USE annotations from fields which I admit is difficult given the bugs of both the JDK and JPoet. Anyway I don't have anymore time for now for this project but wish you best of luck. I'll try to answer anymore questions you have. |
Thanks for the description - I'll see what can be done |
I forgot to add that there are three "use cases" of null handling you should consider:
I know you talked about not really complying to a spec but about utility and the above are three use cases when dealing with null. As for:
When I said the library was "wrong" it was because you or someone who did the That is a very common use case is to have: @checkerframework.Nullable @javax.constraint.validation.NotNull String someField; (replace checkerframework with any static analysis annotation) However the generated code does a:
How can you ever validate an object if you cannot make it because NPE is thrown? That is The irony is if you do use static analysis (you need far less to almost zero defensive programming with static analysis) and java validation the only time a I explained this extensively over and over again in #107 . See this comment validation api: #107 (comment) So it's not "wrong" in terms of it doesn't crash. In fact it arguable other than not supporting TYPE_USE did indeed propagate the annotation. However it certainly doesn't do out of the box what most folks who know what the validation api is supposed to do. It certainly doesn't do what Spring expects in its So since you don't want to comply to spec I recommend the best solution is to do what Immutables does and make your own annotations for the various null handling. And while Immutables isn't your goal in my opinion it currently does a better job than this library because the exact scenario I had with validation/static analysis contradiction didn't happen on my first stab at the library (it also has semver... not that I love semver it does give me some idea of the backward compatibility). It is not about being "wrong" or "right". I should not have said that. That wasn't my point. It was about making people use the other libraries you plan on making "specialized" classes for correctly as those do have specs and designs. It's about being aware of a specific portion of annotations that has been around since Java 8. Static analysis loves code generation (since reflection cannot be verified) and annotation processing. Its about knowing validation needs nulls. It's also about planning things correctly from the beginning so you don't break peoples code later on every upgrade or make a trillion little switches of configuration. You advertise the library frequently on reddit so imagine I won't be the only once with some opinion that what the libraries is not exactly the "right" way. So it is not "wrong" but can we agree it needs to be improved? |
As I said, I've never encountered a TYPE_USE annotation and thus don't know what to do. I'm happy to it make it work properly but I need to understand what is wrong and what can be improved. I hope we can just focus on the problem and eliminate any superfluous comments. For example, in #106 you say:
@RecordBuilder
public record MyRecord(@Nullable String name, String required) {}
I searched around and the docs for TYPE_USE (even that JDK bug report) are opaque. Wouldn't it also be correct to just copy the TYPE_USE
My read of the spec says that the It's a 2 line code change to get the Record Builder processor to recognize the TYPE_USE annotations. Let me know if this is correct for Given:
|
What happens when you have name conflicts? A dumb example is No it will not work for all cases. You also need to worry static inner classes, arrays and generics. For example you can have a record like: record MyRecord(
Map<String, @Nullable String> model,
@NonNull String @Nullable [] ) {
} You basically need to print the type exactly as it is and that is what the JDK bug is about. You should be able to just call In fact you probably do not even need to try to interpret the meaning of the TYPE_USE annotations but just propagate them. That is all you have to do. However your solution might just be good enough (e.g. 80-20) till the JDK makes it easier. Apologies on the superfluous comments but it is very complicated subject and is extremely frustrating when someone is dismissive with "I am not wrong" or the implementation "is not wrong". That makes me feel like I have to go out of my way to show its problems. |
OK - that example helps - there's definitely more work to do. I'll also add some conflicting names to would surface FQPNs. Currently, I have:
I'll make something that has a duplicate name for it. So, that's the |
FYI - I did a lot of playing around/investigating. In order to get a complete solution to the various possible positions of
Update: maybe not - there has been some recent activity ¯\(ツ)/¯ |
Java's DAG for annotations processors doesn't contain `TYPE_USE` annotations on the Element for some reason. However, they are on the type. So, use the type instead. Note due to limitations of JavaPoet this doesn't fix `TYPE_USE` annotations on parameterized types or array components. If we want to address those we will need changes in JavaPoet which has been dormant for a very long time. Fixes #113 Relates to #111
Java's DAG for annotations processors doesn't contain `TYPE_USE` annotations on the Element for some reason. However, they are on the type. So, use the type instead. Note due to limitations of JavaPoet this doesn't fix `TYPE_USE` annotations on parameterized types or array components. If we want to address those we will need changes in JavaPoet which has been dormant for a very long time. Fixes #113 Relates to #111
Java's DAG for annotations processors doesn't contain `TYPE_USE` annotations on the Element for some reason. However, they are on the type. So, use the type instead. Note due to limitations of JavaPoet this doesn't fix `TYPE_USE` annotations on parameterized types or array components. If we want to address those we will need changes in JavaPoet which has been dormant for a very long time. Fixes #113 Relates to #111
Hi, |
Are there any annotation similar to lomboks |
I played around with some ideas but they all required bytecode modifications and I'd like to stay away from that. See here: #120 |
Yes that will probably be good for all. However I can't keep working on #106 or the other nullable annotation bugs till some decisions are made. Which is why I have been so back and forth on this as there are some serious previous design decisions that are impacting me.
So I'm fine with holding off indefinitely till some decisions are made (like lets copy what immutables does).
And I'm sorry for coming off rough and flooding the inbox but I have already sunken some time on this (a lot more than just changing some call from
of
toofNullable
) as I sort of felt obligated given previous discussions on reddit and whatnot.I'll stop work and commenting till the dust clears.
Originally posted by @agentgt in #107 (comment)
The text was updated successfully, but these errors were encountered: