Skip to content

Commit d13d3c3

Browse files
alicejliddixit14
andauthored
feat: update JavaDoc to use @return (#1233)
* feat: update JavaDoc to use @return * add another test * add unit test for emptyComments * remove helper comment Co-authored-by: Deepankar Dixit <[email protected]>
1 parent 7e6e750 commit d13d3c3

File tree

2 files changed

+62
-8
lines changed

2 files changed

+62
-8
lines changed

gapic-generator-java/src/main/java/com/google/api/generator/engine/ast/JavaDocComment.java

+11-1
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ public abstract static class Builder {
5151
String throwsType = null;
5252
String throwsDescription = null;
5353
String deprecated = null;
54+
String returnDescription = null;
5455
List<String> paramsList = new ArrayList<>();
5556
List<String> componentsList = new ArrayList<>();
5657
// Private accessor, set complete and consolidated comment.
@@ -69,6 +70,11 @@ public Builder setDeprecated(String deprecatedText) {
6970
return this;
7071
}
7172

73+
public Builder setReturn(String returnText) {
74+
returnDescription = returnText;
75+
return this;
76+
}
77+
7278
public Builder addParam(String name, String description) {
7379
paramsList.add(String.format("@param %s %s", name, processParamComment(description)));
7480
return this;
@@ -129,12 +135,13 @@ public boolean emptyComments() {
129135
return Strings.isNullOrEmpty(throwsType)
130136
&& Strings.isNullOrEmpty(throwsDescription)
131137
&& Strings.isNullOrEmpty(deprecated)
138+
&& Strings.isNullOrEmpty(returnDescription)
132139
&& paramsList.isEmpty()
133140
&& componentsList.isEmpty();
134141
}
135142

136143
public JavaDocComment build() {
137-
// @param, @throws and @deprecated should always get printed at the end.
144+
// @param, @throws, @return, and @deprecated should always get printed at the end.
138145
componentsList.addAll(paramsList);
139146
if (!Strings.isNullOrEmpty(throwsType)) {
140147
componentsList.add(
@@ -143,6 +150,9 @@ public JavaDocComment build() {
143150
if (!Strings.isNullOrEmpty(deprecated)) {
144151
componentsList.add(String.format("@deprecated %s", deprecated));
145152
}
153+
if (!Strings.isNullOrEmpty(returnDescription)) {
154+
componentsList.add(String.format("@return %s", returnDescription));
155+
}
146156
// Escape component in list one by one, because we will join the components by `\n`
147157
// `\n` will be taken as escape character by the comment escaper.
148158
componentsList =

gapic-generator-java/src/test/java/com/google/api/generator/engine/ast/JavaDocCommentTest.java

+51-7
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,16 @@
2222
import org.junit.Test;
2323

2424
public class JavaDocCommentTest {
25+
@Test
26+
public void emptyJavaDocComment() {
27+
JavaDocComment.Builder javaDocCommentBuilder = JavaDocComment.builder();
28+
assertEquals(true, javaDocCommentBuilder.emptyComments());
29+
30+
String content = "testing return";
31+
javaDocCommentBuilder.setReturn(content);
32+
assertEquals(false, javaDocCommentBuilder.emptyComments());
33+
}
34+
2535
@Test
2636
public void createJavaDocComment_basic() {
2737
String content = "this is a test comment";
@@ -153,9 +163,34 @@ public void createJavaDocComment_multipleParams() {
153163
}
154164

155165
@Test
156-
public void createJavaDocComment_throwsAndDeprecated() {
157-
// No matter how many times or order `setThrows` and `setDeprecated` are called,
158-
// only one @throws and @deprecated will be printed.
166+
public void createJavaDocComment_multipleParamsAndReturn() {
167+
// Parameters should be grouped together and get printed after block comments.
168+
// Return text should get printed at the very end.
169+
String comment = "This is a block comment.";
170+
String paramName1 = "shelfName";
171+
String paramDescription1 = "The name of the shelf where books are published to.";
172+
String paramName2 = "shelfId";
173+
String paramDescription2 = "The shelfId of the shelf where books are published to.";
174+
String returnText = "This is the method return text.";
175+
JavaDocComment javaDocComment =
176+
JavaDocComment.builder()
177+
.addParam(paramName1, paramDescription1)
178+
.addParam(paramName2, paramDescription2)
179+
.addComment(comment)
180+
.setReturn(returnText)
181+
.build();
182+
String expected =
183+
"This is a block comment.\n"
184+
+ "@param shelfName The name of the shelf where books are published to.\n"
185+
+ "@param shelfId The shelfId of the shelf where books are published to.\n"
186+
+ "@return This is the method return text.";
187+
assertEquals(expected, javaDocComment.comment());
188+
}
189+
190+
@Test
191+
public void createJavaDocComment_throwsAndDeprecatedAndReturn() {
192+
// No matter how many times or order `setThrows`, `setDeprecated`, `setReturn` are called,
193+
// only one @throws, @deprecated, and @return will be printed.
159194
String throwsType = "com.google.api.gax.rpc.ApiException";
160195
String throwsDescription = "if the remote call fails.";
161196
String throwsType_print = "java.lang.RuntimeException";
@@ -164,28 +199,35 @@ public void createJavaDocComment_throwsAndDeprecated() {
164199
String deprecatedText = "Use the {@link ArchivedBookName} class instead.";
165200
String deprecatedText_print = "Use the {@link ShelfBookName} class instead.";
166201

202+
String returnText = "This is the incorrect method return text.";
203+
String returnText_print = "This is the correct method return text.";
204+
167205
JavaDocComment javaDocComment =
168206
JavaDocComment.builder()
169207
.setThrows(throwsType, throwsDescription)
170208
.setDeprecated(deprecatedText)
209+
.setReturn(returnText)
171210
.setThrows(throwsType_print, throwsDescription_print)
172211
.setDeprecated(deprecatedText_print)
212+
.setReturn(returnText_print)
173213
.build();
174214
String expected =
175215
LineFormatter.lines(
176216
"@throws java.lang.RuntimeException if the remote call fails.\n",
177-
"@deprecated Use the {@link ShelfBookName} class instead.");
217+
"@deprecated Use the {@link ShelfBookName} class instead.\n",
218+
"@return This is the correct method return text.");
178219
assertEquals(expected, javaDocComment.comment());
179220
}
180221

181222
@Test
182223
public void createJavaDocComment_allComponents() {
183-
// No matter what order `setThrows`, `setDeprecated` are called,
224+
// No matter what order `setThrows`, `setDeprecated`, and `setReturn` are called,
184225
// They will be printed at the end. And `@param` should be grouped,
185-
// they should always be printed right before `@throws` and `@deprecated`.
226+
// they should always be printed right before `@throws`, `@deprecated`, and `@return`.
186227
// All other add methods should keep the order of how they are added.
187228
String content = "this is a test comment";
188229
String deprecatedText = "Use the {@link ArchivedBookName} class instead.";
230+
String returnText = "This is the method return text.";
189231
String paramName1 = "shelfName";
190232
String paramDescription1 = "The name of the shelf where books are published to.";
191233
String paramName2 = "shelf";
@@ -204,6 +246,7 @@ public void createJavaDocComment_allComponents() {
204246
JavaDocComment.builder()
205247
.setDeprecated(deprecatedText)
206248
.setThrows(throwsType, throwsDescription)
249+
.setReturn(returnText)
207250
.addParam(paramName1, paramDescription1)
208251
.addComment(content)
209252
.addParagraph(paragraph1)
@@ -226,7 +269,8 @@ public void createJavaDocComment_allComponents() {
226269
"@param shelfName The name of the shelf where books are published to.\n",
227270
"@param shelf The shelf to create.\n",
228271
"@throws com.google.api.gax.rpc.ApiException if the remote call fails.\n",
229-
"@deprecated Use the {@link ArchivedBookName} class instead.");
272+
"@deprecated Use the {@link ArchivedBookName} class instead.\n",
273+
"@return This is the method return text.");
230274
assertEquals(expected, javaDocComment.comment());
231275
}
232276
}

0 commit comments

Comments
 (0)