Skip to content

feat: add NullMarked annotation to classes#13517

Draft
nnicolee wants to merge 1 commit into
mainfrom
feat/jspecify-nullmarked
Draft

feat: add NullMarked annotation to classes#13517
nnicolee wants to merge 1 commit into
mainfrom
feat/jspecify-nullmarked

Conversation

@nnicolee

Copy link
Copy Markdown
Contributor

This PR updates the gapic-generator-java to add the JSpecify @NullMarked annotation to all generated class declarations. Adding @NullMarked at the class level defines the class scope as null-safe, establishing that all unannotated types in method signatures (parameters and return types) are nonnull able by default. This is the first 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
  • Settings Classes: AbstractServiceSettingsClassComposer and AbstractServiceStubSettingsClassComposer
  • Stub Classes: AbstractServiceStubClassComposer and AbstractTransportServiceStubClassComposer
  • Callable Factories: AbstractServiceCallableFactoryClassComposer
  • Resource Names: ResourceNameHelperClassComposer, CommonStrings

Implementation Changes:

  • Added JSpecify import statements to class-level generation templates
  • Registered NullMarked.class and Nullable.class in TypeStore across all class composers to ensure references compile and resolve properly

Verification/Testing:

Next Steps:

  • In the next PR, will add @nullable annotations

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

@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 integrates JSpecify nullness annotations (NullMarked and Nullable) across several GAPIC class composers to support better null-safety. The feedback highlights a few areas for improvement: an unused constant JSPECIFY_ANNOTATIONS_PACKAGE was introduced in CommonStrings.java, fully qualified class names were used in AbstractServiceStubSettingsClassComposer.java instead of imports (which is inconsistent with other files), and the import ordering in ResourceNameHelperClassComposer.java violates the Google Java Style Guide.

Comment on lines 24 to 25
public static final String JSPECIFY_ANNOTATIONS_PACKAGE = "org.jspecify.annotations";
}

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.

medium

The constant JSPECIFY_ANNOTATIONS_PACKAGE is defined but not used. If this is a public API constant that cannot be removed for backward compatibility, it should be marked as obsolete (e.g., with @ObsoleteApi) instead of being removed. Otherwise, if it is safe to remove, please delete it to avoid dead code.

References
  1. Public API constants that cannot be removed for backward compatibility should be marked as obsolete (e.g., with @ObsoleteApi) instead of being removed.

Comment on lines +2222 to +2223
org.jspecify.annotations.NullMarked.class,
org.jspecify.annotations.Nullable.class);

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.

medium

For consistency with the other composer classes in this pull request, consider importing org.jspecify.annotations.NullMarked and org.jspecify.annotations.Nullable at the top of the file and using their simple names here instead of fully qualified names.

Comment on lines 17 to 19
import org.jspecify.annotations.NullMarked;
import org.jspecify.annotations.Nullable;
import com.google.api.generator.engine.ast.AnnotationNode;

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.

medium

According to the Google Java Style Guide (Section 4.5.2), imports should be ordered alphabetically. The org.jspecify imports should be placed after the com.google imports.

References
  1. Google Java Style Guide Section 4.5.2: Ordering of imports. Imports are ordered alphabetically. (link)

@nnicolee nnicolee force-pushed the feat/jspecify-nullmarked branch from ef5eb49 to dae9b95 Compare June 18, 2026 19:51
@nnicolee nnicolee force-pushed the feat/jspecify-nullmarked branch from dae9b95 to b1aab81 Compare June 18, 2026 20:59
@sonarqubecloud

Copy link
Copy Markdown

Quality Gate Failed Quality Gate failed for 'gapic-generator-java-root'

Failed conditions
7.4% Duplication on New Code (required ≤ 3%)

See analysis details on SonarQube Cloud

@sonarqubecloud

Copy link
Copy Markdown

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.

1 participant