Skip to content

feat: add optional and auto-detected @NullMarked annotation for generated classes - #4

Open
kistlers wants to merge 1 commit into
swesteme:mainfrom
kistlers:feature/add-nullmarked-support
Open

feat: add optional and auto-detected @NullMarked annotation for generated classes#4
kistlers wants to merge 1 commit into
swesteme:mainfrom
kistlers:feature/add-nullmarked-support

Conversation

@kistlers

Copy link
Copy Markdown

Closes #3

Description

This PR adds support for annotating generated Java classes with @NullMarked (org.jspecify.annotations.NullMarked).

Features

  1. Auto-Detection: Automatically checks if org.jspecify:jspecify is present in the project's dependencies or artifacts. If present, @NullMarked is automatically imported and added to generated classes. If not present, it is safely omitted (no extra dependencies required).
  2. Explicit Parameter: Provides an <addNullMarked> configuration parameter (true/false) to explicitly force or disable annotation generation.

Changes

  • Updated Mojo templates for generated classes and autoconfiguration.
  • Added isJSpecifyPresent() and isNullMarked() helper methods in GenerateServiceMojo.
  • Added unit test cases covering explicit parameter settings and dependency auto-detection in GenerateServiceMojoTest.
  • Updated README.md documentation.

@swesteme

Copy link
Copy Markdown
Owner

Hey @kistlers !

Thanks a lot for your efforts to improve the library. I am currently on vacation, so I won‘t be able to take a closer look until after my return.
I only have a few quick remarks you may want to consider:

  • There are quite a few null-safety annotations, e.g. Jakarta or Jetbrains. What about a more generic approach?
  • You may want to keep it simple? Detecting the availability of a library every time is probably fancy, but maybe not worth the hassle? When writing the pom.xml you aleeady know if a library or framework is included.

So if I had to implement this feature, I would simply add configuration options to add the nullability annotations of my choice.

Thanks again for your contribution!

@kistlers

Copy link
Copy Markdown
Author

The reason for jspecify is simple. It is the new standard for nullability, supported by everyone, in a consortium led by Google. See https://jspecify.dev/docs/whether/ and https://jspecify.dev/about/

In that case however, I am not adding NotNull/Nullable annotations, but rather @NullMarked, which is another annotation https://jspecify.dev/docs/user-guide/#nullmarked that tells a static analyser that this class or package treats types as defaulf NonNull (as opposed to the Java default).

As an example, Spring Boot 4 is fully NullMarked with jspecify (NonNull as the default) and kotlin 1.82 and newer understands jspecify annotations as-well :)

Due to how quickly this is "spreading" I would argue that the detection is preferable, so that everyone that upgrades to the new version will alrrady have the generated classes annotated, as adding NullMarked has no downsides for everyone, including those that do not enforce every package to be null-marked. But it is very helpful for everyone that enforces it :)

@swesteme

Copy link
Copy Markdown
Owner

I see. Excuse my ignorance, we are switching to Spring Boot 4 sometime in the next two or three months 😜

I will take a closer look soon, I promise!

@kistlers

kistlers commented Jul 23, 2026

Copy link
Copy Markdown
Author

All good :D
thanks!

Unrelated: I have opened another issue in the service accompanying this plugin, since I already got your attention :D
swesteme/artifact-version-service#2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Annotate generated classes with @NullMarked (JSpecify) when present

2 participants