Skip to content

chore(samples): Refactor gapic samples to not have try with resources#12777

Draft
lqiu96 wants to merge 10 commits intomainfrom
refactor/gapic-samples-no-try-with-resources
Draft

chore(samples): Refactor gapic samples to not have try with resources#12777
lqiu96 wants to merge 10 commits intomainfrom
refactor/gapic-samples-no-try-with-resources

Conversation

@lqiu96
Copy link
Copy Markdown
Member

@lqiu96 lqiu96 commented Apr 13, 2026

Fixes #12376

The samples simply show how to create a client. The comments have a link to the public cloud docs to reference the client lifecycle guide.

Copy link
Copy Markdown
Contributor

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

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 refactors the GAPIC generator to promote service client reuse in generated Java samples. It removes the try-with-resources pattern from sample generation and adds Javadoc comments regarding client lifecycle management. The SampleCodeWriter and SampleComposer classes were updated to support these changes, and numerous golden files were updated to reflect the new output format. Feedback suggests extracting duplicated logic for sample body construction into a shared utility method to improve maintainability.

Comment on lines +405 to +409
List<Statement> body = new ArrayList<>();
body.add(
ExprStatement.withExpr(
SampleComposerUtil.assignClientVariableWithCreateMethodExpr(clientVarExpr)));
body.addAll(bodyStatements);
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

This block of code for creating the sample body is duplicated in several places across ServiceClientCallableMethodSampleComposer, ServiceClientHeaderSampleComposer, and ServiceClientMethodSampleComposer.

To improve maintainability and reduce code duplication, consider extracting this logic into a helper method in a utility class. This aligns with the practice of moving shared code to separate helper/utility classes to avoid duplication and maintain clean layering.

References
  1. If code is duplicated and needs to be shared, move it to a separate helper/utility class to improve maintainability and avoid layering issues.

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.

[sdk-platform-java] Update the generated samples to reflect client lifecycle best practices

1 participant