Skip to content

feat(gapic-generator): Add Nullable annotation to generated classes#13558

Merged
nnicolee merged 23 commits into
mainfrom
feat/jspecify-nullable
Jul 14, 2026
Merged

feat(gapic-generator): Add Nullable annotation to generated classes#13558
nnicolee merged 23 commits into
mainfrom
feat/jspecify-nullable

Conversation

@nnicolee

@nnicolee nnicolee commented Jun 25, 2026

Copy link
Copy Markdown
Contributor

This PR updates the gapic-generator-java to add the JSpecify @nullable annotation to all generated class declarations. This is the second phase in onboarding the generated client libraries to compile-time safety validation, see design doc for more details: go/sdk:java-jspecify-null-annotations-gapic

Classes Annotated:

  • Client Classes: AbstractServiceClientClassComposer
  • Stub Classes: AbstractServiceStubClassComposer and ServiceStubClassComposer
  • Resource Names: ResourceNameHelperClassComposer

Implementation Changes:

  • Built the parameter variable expression in equals(Object o) as a reference initially, and attached the @nullable annotation during its method argument declaration (with isDecl = true). This prevents IllegalStateException crashes in the generator's strict AST validation engine.
  • Leveraged a VaporReference type name-hacking trick to render generic type-use annotations (such as List<@nullable ResourceName>) without needing disruptive changes to the core AST reference rendering engine.
  • Modified two owlbot.py scripts to reflect the signature changes: ‎java-bigquerydatatransfer/owlbot.py & java-bigquerydatatransfer/owlbot.py

Verification/Testing:

@nnicolee
nnicolee requested a review from lqiu96 June 25, 2026 17:27
@nnicolee nnicolee added the do not merge Indicates a pull request not ready for merge, due to either quality or timing. label Jun 25, 2026

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces @Nullable annotations to various generated classes and generator code in the Java GAPIC generator. Specifically, it updates the composer classes to annotate settings fields, getter methods, non-required resource name helper arguments, parse methods, toStringList generic types, and equals method arguments, with corresponding updates to the golden files. A review comment highlights a fragile workaround in ResourceNameHelperClassComposer.java where the @Nullable annotation is hacked into the VaporReference name string, warning that this could lead to issues with type comparisons, fully qualified names, AST manipulation, and missing imports, and suggests extending the AST classes to properly support type-use annotations.

@nnicolee nnicolee added the kokoro:run Add this label to force Kokoro to re-run the tests. label Jun 25, 2026
@yoshi-kokoro yoshi-kokoro removed the kokoro:run Add this label to force Kokoro to re-run the tests. label Jun 25, 2026
@nnicolee nnicolee added the kokoro:force-run Add this label to force Kokoro to re-run the tests. label Jun 25, 2026
@yoshi-kokoro yoshi-kokoro removed the kokoro:force-run Add this label to force Kokoro to re-run the tests. label Jun 25, 2026
@nnicolee
nnicolee marked this pull request as ready for review June 26, 2026 23:07
@nnicolee
nnicolee requested review from a team as code owners June 26, 2026 23:07
public final Room getRoom(RoomName name) {
GetRoomRequest request =
GetRoomRequest.newBuilder().setName(name == null ? null : name.toString()).build();
GetRoomRequest request = GetRoomRequest.newBuilder().setName(name.toString()).build();

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

qq, I don't think we should change this. Perhaps better to mark the param as nullable?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1 the new logic may affect this, which is a good way to catch additional cases where @Nullable is needed.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Discussed with @nnicolee, looks like setName is a proto class, which we don't have control over, and is being marked by NullAway with:

[ERROR] /usr/local/google/home/nicolejlee/google-cloud-java/java-showcase/gapic-showcase/src/main/java/com/google/showcase/v1beta1/MessagingClient.java:[1822,64] error: [NullAway] passing @Nullable parameter 'parent == null ? null : parent.toString()' where @NonNull is required

If it was possible to configure ErrorProne to ignore proto classes in these cases that would be great. That could be a good first step.
On the other hand, looks like Proto classes actually can take nulls (since the code's been like this), which can be worked around by adding a warning suppression as Nicole suggested.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, currently we're ignoring this error

@nnicolee
nnicolee force-pushed the feat/jspecify-nullable branch from 96dffb8 to 30ce14a Compare June 30, 2026 19:03
@nnicolee
nnicolee requested review from a team as code owners June 30, 2026 19:03
@nnicolee
nnicolee force-pushed the feat/jspecify-nullable branch 2 times, most recently from 96dffb8 to 0a438e0 Compare July 6, 2026 16:10
@nnicolee
nnicolee changed the base branch from main to feat/jspecify-add-nullmarked July 6, 2026 16:17
@nnicolee
nnicolee changed the base branch from feat/jspecify-add-nullmarked to main July 8, 2026 05:05
@nnicolee
nnicolee force-pushed the feat/jspecify-nullable branch 2 times, most recently from c245fbc to 8084981 Compare July 8, 2026 22:10
@nnicolee nnicolee added the kokoro:force-run Add this label to force Kokoro to re-run the tests. label Jul 9, 2026
@yoshi-kokoro yoshi-kokoro removed the kokoro:force-run Add this label to force Kokoro to re-run the tests. label Jul 9, 2026
@sonarqubecloud

Copy link
Copy Markdown

@sonarqubecloud

Copy link
Copy Markdown

@nnicolee nnicolee closed this Jul 13, 2026
@nnicolee nnicolee reopened this Jul 13, 2026
@nnicolee nnicolee removed the do not merge Indicates a pull request not ready for merge, due to either quality or timing. label Jul 14, 2026
@nnicolee
nnicolee enabled auto-merge (squash) July 14, 2026 16:01
lqiu96 added a commit that referenced this pull request Jul 14, 2026
The `check-version` job in showcase is not running in PRs that do not
modify the specific paths. Update the job to use a filter job to skip
the job on non-relevant PRs.

Example: #13558
@nnicolee
nnicolee merged commit e3e9d0b into main Jul 14, 2026
228 of 233 checks passed
@nnicolee
nnicolee deleted the feat/jspecify-nullable branch July 14, 2026 19:29
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.

5 participants