GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — [ggj][codegen] feat: add header comments to ServiceClient (#307) · googleapis/sdk-platform-java@b4ca070

This repository was archived by the owner on Oct 29, 2026. It is now read-only.

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • src
    • main/java/com/google/api/generator/gapic
      • composer
        • ServiceClientClassComposer.java
        • ServiceClientCommentComposer.java
      • model
        • Service.java
    • test/java/com/google/api/generator/gapic/composer
      • ServiceClientClassComposerTest.java
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎src/main/java/com/google/api/generator/gapic/composer/ServiceClientClassComposer.java‎

Copy file name to clipboardExpand all lines: src/main/java/com/google/api/generator/gapic/composer/ServiceClientClassComposer.java
+2Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,8 @@ public GapicClass generate(Service service, Map<String, Message> messageTypes) {
101101

102102
ClassDefinition classDef =
103103
ClassDefinition.builder()
104+
.setHeaderCommentStatements(
105+
ServiceClientCommentComposer.createClassHeaderComments(service))
104106
.setPackageString(pakkage)
105107
.setAnnotations(createClassAnnotations(types))
106108
.setImplementsTypes(createClassImplements(types))
Collapse file

‎src/main/java/com/google/api/generator/gapic/composer/ServiceClientCommentComposer.java‎

Copy file name to clipboardExpand all lines: src/main/java/com/google/api/generator/gapic/composer/ServiceClientCommentComposer.java
+83Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,62 @@
1717
import com.google.api.generator.engine.ast.CommentStatement;
1818
import com.google.api.generator.engine.ast.JavaDocComment;
1919
import com.google.api.generator.engine.ast.TypeNode;
20+
import com.google.api.generator.gapic.model.Service;
21+
import com.google.api.generator.gapic.utils.JavaStyle;
22+
import java.util.Arrays;
23+
import java.util.List;
2024

2125
class ServiceClientCommentComposer {
26+
// Tokens.
2227
private static final String COLON = ":";
2328

29+
// Constants.
30+
private static final String SERVICE_DESCRIPTION_INTRO_STRING =
31+
"This class provides the ability to make remote calls to the backing service through method "
32+
+ "calls that map to API methods. Sample code to get started:";
33+
private static final String SERVICE_DESCRIPTION_CLOSE_STRING =
34+
"Note: close() needs to be called on the echoClient object to clean up resources such as "
35+
+ "threads. In the example above, try-with-resources is used, which automatically calls "
36+
+ "close().";
37+
private static final String SERVICE_DESCRIPTION_SURFACE_SUMMARY_STRING =
38+
"The surface of this class includes several types of Java methods for each of the API's "
39+
+ "methods:";
40+
private static final String SERVICE_DESCRIPTION_SURFACE_CODA_STRING =
41+
"See the individual methods for example code.";
42+
private static final String SERVICE_DESCRIPTION_RESOURCE_NAMES_FORMATTING_STRING =
43+
"Many parameters require resource names to be formatted in a particular way. To assist with"
44+
+ " these names, this class includes a format method for each type of name, and"
45+
+ " additionally a parse method to extract the individual identifiers contained within"
46+
+ " names that are returned.";
47+
private static final String SERVICE_DESCRIPTION_CREDENTIALS_SUMMARY_STRING =
48+
"To customize credentials:";
49+
private static final String SERVICE_DESCRIPTION_ENDPOINT_SUMMARY_STRING =
50+
"To customize the endpoint:";
51+
52+
private static final List<String> SERVICE_DESCRIPTION_SURFACE_DESCRIPTION =
53+
Arrays.asList(
54+
"A \"flattened\" method. With this type of method, the fields of the request type have"
55+
+ " been converted into function parameters. It may be the case that not all fields"
56+
+ " are available as parameters, and not every API method will have a flattened"
57+
+ " method entry point.",
58+
"A \"request object\" method. This type of method only takes one parameter, a request"
59+
+ " object, which must be constructed before the call. Not every API method will"
60+
+ " have a request object method.",
61+
"A \"callable\" method. This type of method takes no parameters and returns an immutable "
62+
+ "API callable object, which can be used to initiate calls to the service.");
63+
64+
// Patterns.
2465
private static final String CREATE_METHOD_STUB_ARG_PATTERN =
2566
"Constructs an instance of EchoClient, using the given stub for making calls. This is for"
2667
+ " advanced usage - prefer using create(%s).";
2768

69+
private static final String SERVICE_DESCRIPTION_CUSTOMIZE_SUMMARY_PATTERN =
70+
"This class can be customized by passing in a custom instance of %s to create(). For"
71+
+ " example:";
72+
73+
private static final String SERVICE_DESCRIPTION_SUMMARY_PATTERN = "Service Description: %s";
74+
75+
// Comments.
2876
static final CommentStatement CREATE_METHOD_NO_ARG_COMMENT =
2977
toSimpleComment("Constructs an instance of EchoClient with default settings.");
3078

@@ -45,6 +93,41 @@ class ServiceClientCommentComposer {
4593
"Returns the OperationsClient that can be used to query the status of a long-running"
4694
+ " operation returned by another API method call.");
4795

96+
static List<CommentStatement> createClassHeaderComments(Service service) {
97+
JavaDocComment.Builder classHeaderJavadocBuilder = JavaDocComment.builder();
98+
if (service.hasDescription()) {
99+
classHeaderJavadocBuilder.addComment(
100+
String.format(SERVICE_DESCRIPTION_SUMMARY_PATTERN, service.description()));
101+
}
102+
103+
// Service introduction.
104+
classHeaderJavadocBuilder.addParagraph(SERVICE_DESCRIPTION_INTRO_STRING);
105+
// TODO(summerji): Add sample code here.
106+
107+
// API surface description.
108+
classHeaderJavadocBuilder.addParagraph(SERVICE_DESCRIPTION_CLOSE_STRING);
109+
classHeaderJavadocBuilder.addParagraph(SERVICE_DESCRIPTION_SURFACE_SUMMARY_STRING);
110+
classHeaderJavadocBuilder.addOrderedList(SERVICE_DESCRIPTION_SURFACE_DESCRIPTION);
111+
classHeaderJavadocBuilder.addParagraph(SERVICE_DESCRIPTION_SURFACE_CODA_STRING);
112+
113+
// Formatting resource names.
114+
classHeaderJavadocBuilder.addParagraph(SERVICE_DESCRIPTION_RESOURCE_NAMES_FORMATTING_STRING);
115+
116+
// Customization examples.
117+
classHeaderJavadocBuilder.addParagraph(
118+
String.format(
119+
SERVICE_DESCRIPTION_CUSTOMIZE_SUMMARY_PATTERN,
120+
String.format("%sSettings", JavaStyle.toUpperCamelCase(service.name()))));
121+
classHeaderJavadocBuilder.addParagraph(SERVICE_DESCRIPTION_CREDENTIALS_SUMMARY_STRING);
122+
// TODO(summerji): Add credentials' customization sample code here.
123+
classHeaderJavadocBuilder.addParagraph(SERVICE_DESCRIPTION_ENDPOINT_SUMMARY_STRING);
124+
// TODO(summerji): Add endpoint customization sample code here.
125+
126+
return Arrays.asList(
127+
CommentComposer.AUTO_GENERATED_CLASS_COMMENT,
128+
CommentStatement.withComment(classHeaderJavadocBuilder.build()));
129+
}
130+
48131
static CommentStatement createCreateMethodStubArgComment(TypeNode settingsType) {
49132
return toSimpleComment(
50133
String.format(CREATE_METHOD_STUB_ARG_PATTERN, settingsType.reference().name()));
Collapse file

‎src/main/java/com/google/api/generator/gapic/model/Service.java‎

Copy file name to clipboardExpand all lines: src/main/java/com/google/api/generator/gapic/model/Service.java
+10-1Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,10 @@
1515
package com.google.api.generator.gapic.model;
1616

1717
import com.google.auto.value.AutoValue;
18+
import com.google.common.base.Strings;
1819
import com.google.common.collect.ImmutableList;
1920
import java.util.List;
21+
import javax.annotation.Nullable;
2022

2123
@AutoValue
2224
public abstract class Service {
@@ -32,7 +34,12 @@ public abstract class Service {
3234

3335
public abstract ImmutableList<Method> methods();
3436

35-
// TODO(miraleung): Get comments.
37+
@Nullable
38+
public abstract String description();
39+
40+
public boolean hasDescription() {
41+
return !Strings.isNullOrEmpty(description());
42+
}
3643

3744
public static Builder builder() {
3845
return new AutoValue_Service.Builder().setMethods(ImmutableList.of());
@@ -52,6 +59,8 @@ public abstract static class Builder {
5259

5360
public abstract Builder setMethods(List<Method> methods);
5461

62+
public abstract Builder setDescription(String description);
63+
5564
public abstract Service build();
5665
}
5766
}
Collapse file

‎src/test/java/com/google/api/generator/gapic/composer/ServiceClientClassComposerTest.java‎

Copy file name to clipboardExpand all lines: src/test/java/com/google/api/generator/gapic/composer/ServiceClientClassComposerTest.java
+49Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,55 @@ public void generateServiceClasses() {
8383
+ "import java.util.concurrent.TimeUnit;\n"
8484
+ "import javax.annotation.Generated;\n"
8585
+ "\n"
86+
+ "// AUTO-GENERATED DOCUMENTATION AND CLASS.\n"
87+
+ "/**\n"
88+
+ " * This class provides the ability to make remote calls to the backing service"
89+
+ " through method calls\n"
90+
+ " * that map to API methods. Sample code to get started:\n"
91+
+ " *\n"
92+
+ " * <p>Note: close() needs to be called on the echoClient object to clean up resources"
93+
+ " such as\n"
94+
+ " * threads. In the example above, try-with-resources is used, which automatically"
95+
+ " calls close().\n"
96+
+ " *\n"
97+
+ " * <p>The surface of this class includes several types of Java methods for each of"
98+
+ " the API's\n"
99+
+ " * methods:\n"
100+
+ " *\n"
101+
+ " * <ol>\n"
102+
+ " * <li>A \"flattened\" method. With this type of method, the fields of the request"
103+
+ " type have been\n"
104+
+ " * converted into function parameters. It may be the case that not all fields"
105+
+ " are available as\n"
106+
+ " * parameters, and not every API method will have a flattened method entry"
107+
+ " point.\n"
108+
+ " * <li>A \"request object\" method. This type of method only takes one parameter, a"
109+
+ " request object,\n"
110+
+ " * which must be constructed before the call. Not every API method will have a"
111+
+ " request object\n"
112+
+ " * method.\n"
113+
+ " * <li>A \"callable\" method. This type of method takes no parameters and returns"
114+
+ " an immutable API\n"
115+
+ " * callable object, which can be used to initiate calls to the service.\n"
116+
+ " * </ol>\n"
117+
+ " *\n"
118+
+ " * <p>See the individual methods for example code.\n"
119+
+ " *\n"
120+
+ " * <p>Many parameters require resource names to be formatted in a particular way. To"
121+
+ " assist with\n"
122+
+ " * these names, this class includes a format method for each type of name, and"
123+
+ " additionally a parse\n"
124+
+ " * method to extract the individual identifiers contained within names that are"
125+
+ " returned.\n"
126+
+ " *\n"
127+
+ " * <p>This class can be customized by passing in a custom instance of EchoSettings to"
128+
+ " create(). For\n"
129+
+ " * example:\n"
130+
+ " *\n"
131+
+ " * <p>To customize credentials:\n"
132+
+ " *\n"
133+
+ " * <p>To customize the endpoint:\n"
134+
+ " */\n"
86135
+ "@BetaApi\n"
87136
+ "@Generated(\"by gapic-generator\")\n"
88137
+ "public class EchoClient implements BackgroundResource {\n"

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.