chore(samples): Refactor gapic samples to not have try with resources#12777
chore(samples): Refactor gapic samples to not have try with resources#12777
Conversation
…ient lifecycle link
…d add client lifecycle link
…on and split lines
…eComment property in Sample model
…operty for custom comments
There was a problem hiding this comment.
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.
| List<Statement> body = new ArrayList<>(); | ||
| body.add( | ||
| ExprStatement.withExpr( | ||
| SampleComposerUtil.assignClientVariableWithCreateMethodExpr(clientVarExpr))); | ||
| body.addAll(bodyStatements); |
There was a problem hiding this comment.
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
- If code is duplicated and needs to be shared, move it to a separate helper/utility class to improve maintainability and avoid layering issues.
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.