jaxb2-basics: Provide JSR-305 support

I’d like to analyze my source code with the Checker Framework, in particular using its Nullness checker. Some of my code interfaces with JAXB and JAXB-basics generated code. Now, I can add a package-level annotation for the generated code, specifying that all parameters, return types and fields are @Nullable (or @Nonnull), but in fact there are both @Nullable and @Nonnull parameters and return types, so that doesn’t actually work.

Hence my question: would you be open to adding the JSR-305 @Nullable and @Nonnull annotations to the interfaces and classes of the jaxb2-basics-runtme library, as well as the code generated by the jaxb2-basics plugin?

The change would introduce a provided-scope dependency on com.google.code.findbugs:jsr305:3.0.0 and thus be backwards-compatible. (Because Maven won’t add it to the classpath of downstream dependencies and Java will simply ignore annotations it can’t find on the classpath.)

I’d be willing to do the actual work and open a PR. Let me know if you have questions or concerns.

About this issue

  • Original URL
  • State: open
  • Created 9 years ago
  • Reactions: 2
  • Comments: 16 (7 by maintainers)

Most upvoted comments

I’d love to be able to use generated classes in a null-safe way from Kotlin. For interop with Java classes, Kotlin looks at both nullable and non-null annotations to distinguish between nullable, non-nullable and unknown nullability (neither annotation present).

I was thinking about a more general solution for the problem of nullable annotations on arrays and their elements: get the user to supply the desired annotations on the classpath, and also specify the desired null and non-null annotations. Then we apply the null/non-null annotation only if it is legal according to the @Target rules. In other words, if using ye olde JSR-305 annotations, arrays won’t be marked nullable, but users who need better null analysis can choose Checker Framework or JetBrains annotations.

So to write out JSR-305 annotations:

-Xannotation-null=javax.annotation.Nullable
-Xannotation-nonnull=javax.annotation.Nonnull

Or for Checker Framework annotations:

-Xannotation-null=org.checkerframework.checker.nullness.qual.Nullable
-Xannotation-nonnull=org.checkerframework.checker.nullness.qual.NonNull

For JetBrains annotations:

-Xannotation-null=org.jetbrains.annotations.Nullable
-Xannotation-nonnull=org.jetbrains.annotations.NotNull

And maybe even Lombok’s NonNull annotation which can insert runtime not-null assertions (if compiling with Lombok):

-Xannotation-nonnull=lombok.NonNull

I had to look them up, so I’ll just put the Maven coordinates here (in Gradle-Kotlin form)

    implementation("com.google.code.findbugs:jsr305:3.0.2")
    implementation("org.checkerframework:checker-qual:3.7.1")
    implementation("org.jetbrains:annotations:20.1.0")
    implementation("org.projectlombok:lombok:1.18.16")

@Stephan202

I know it has been five years, but did you save any code from your prototyping work?

@seanf I know you question has been a while ago, but throwing it down here for anyone stumbling upon this via Google (or via other means).

I prototype a simple XJC plugin (not using this library) that annotates all getters with @NotNull or @Nullable respectively. You can find it here: https://gist.github.com/lion7/b1065b52fdf781f9f82d024c619d480c Note that it is currently only compatible with the latest Jakarta JAXB version and will not work with older versions.