Skip to content

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

Draft
nnicolee wants to merge 4 commits into
mainfrom
feat/jspecify-nullable
Draft

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

Conversation

@nnicolee

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.

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.

Comment on lines +982 to +987
.setGenerics(
Arrays.asList(
VaporReference.builder()
.setName("@Nullable " + thisClassType.reference().name())
.setPakkage(thisClassType.reference().pakkage())
.build()))

This comment was marked as resolved.

@sonarqubecloud

Copy link
Copy Markdown

@sonarqubecloud

Copy link
Copy Markdown

@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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

do not merge Indicates a pull request not ready for merge, due to either quality or timing.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants