-
Notifications
You must be signed in to change notification settings - Fork 280
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
codegen-java: Support generating @Nullable annotations #811
Comments
I agree with the overall approach. With JSpecify gaining traction, it's a reasonable choice (and any somewhat popular annotation framework is better than bespoke annotations, because of the "unless explicitly configured").
It seems cleaner to me to end up with a single CLI parameter and property, that picks the flavor, rather than a parameter and property per flavor. I like I agree with your cost balancing assessment between (1) and (2), and I do like clean and simple. However, I think it's more important that our generated code is correct by default and (2) does leave awkward ways to hold it wrong. I prefer (1). Let's have @stackoverflow & @bioball chime in also. |
Can you clarify what you’re proposing here? |
I think maybe a better solution is to let users configure what library they want their nullness annotations to come from (this is maybe what @holzensp is talking about too). Something like: But, another issue here is that JSR305 doesn't actually have a
This doesn't seem like a great solution; it feels like we'd be generating incomplete code. Also, FWIW: I'm not sure how big of a problem this is, and at the same time, adds a lot of complexity to our code generator. It might be good to hold off for some more +1's from the community. |
Not supporting Pkl’s |
I don't think this needs to be deprecated; tools like IntelliJ let you configure nullability annotations. The intention here is that, by default, Pkl's generated code does not require any dependencies.
Yeah, fair. I think we can have two flags; the nullability library to use, and whether to generate nullable-by-default annotations. And we can throw if the nullability mode is non-null-by-default if targeting JSR305; e.g. throw here:
|
It doesn’t need to be, but users who care about nullness annotations should be strongly encouraged to use one of the widely supported libraries instead of configuring Pkl’s proprietary
Given that we already have |
I'm okay with switching the default to be JSpecify, but I'd prefer to keep around the current annotations as an alternative. We have users that are sensitive to new dependencies, so it would be good to provide options to those that don't want to incur a new dependency here. But, yeah, in principle, I agree that people that care about nullable annotations would want to use a known library.
Your suggestion seems a little quirky to me; it allows for someone to choose I feel that deprecating |
Motivation:
codegen-java already supports generating
@NonNull
annotations.However, many projects prefer
@Nullable
over@NonNull
annotations.JSpecify also recommends
@Nullable
.Changes:
--nullable-annotation
CLI parameter that defaults tonone
.nullableAnnotation
Gradle/CliJavaCodeGenerator/JavaCodeGenerator property that defaults tonull
.As part of this feature, I propose to also make the following changes:
-non-null-annotation
CLI parameter andnonNullAnnotation
properties accordingly.@NonNull
annotations by default.org.pkl.java.config.mapper.NonNull
and encourage users to switch to JSpecify.Note that tools won't recognize
org.pkl.java.config.mapper.NonNull
unless explicitly configured.Open question:
@Nullable
must be accompanied by@NullMarked
(JSpecify) or@NonnullByDefault
(JSR 305).I can think of two ways to go about this:
--null-marked-annotation
and annotate config classes accordingly.This is the most consistent solution, but it requires yet another CLI parameter and property.
It can be made more convenient by inferring the correct annotation based on
--nullable-annotation
and--non-null-annotation
for popular libraries such as JSpecify and JSR 305.package-info.java
with a@NullMarked
annotation.This is a clean and simple solution. However, it is less convenient than (1).
It could, in theory, cause problems if users compile generated and handwritten code separately and don't have a place where to put
package-info.java
.Final thought: The Java ecosystem is converging on JSpecify. For many users, the best option would be
--generate-jspecify-annotations
.The text was updated successfully, but these errors were encountered: