Skip to content

Commit f4a76a0

Browse files
Fix record javadoc generation (#63)
1 parent f20e5ee commit f4a76a0

File tree

4 files changed

+64
-12
lines changed

4 files changed

+64
-12
lines changed

sourcegen-generator-java/src/main/java/io/micronaut/sourcegen/javapoet/MethodSpec.java

+4-1
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,10 @@ static CodeBlock makeJavadocWithParameters(CodeBlock javadoc,
148148
for (ParameterSpec parameterSpec : parameters) {
149149
if (!parameterSpec.javadoc.isEmpty()) {
150150
// Emit a new line before @param section only if the method javadoc is present.
151-
if (emitTagNewline && !javadoc.isEmpty()) builder.add("\n");
151+
if (!builder.isEmpty()) {
152+
if (emitTagNewline) builder.add("\n");
153+
builder.add("\n");
154+
}
152155
emitTagNewline = false;
153156
builder.add("@param $L $L", parameterSpec.name, parameterSpec.javadoc);
154157
}

sourcegen-generator-java/src/test/java/io/micronaut/sourcegen/javapoet/MethodSpecTest.java

+5-5
Original file line numberDiff line numberDiff line change
@@ -297,12 +297,12 @@ abstract static class AbstractClassWithPrivateAnnotation {
297297
@Test public void withParameterJavaDoc() {
298298
MethodSpec methodSpec = MethodSpec.methodBuilder("getTaco")
299299
.addParameter(ParameterSpec.builder(TypeName.DOUBLE, "money")
300-
.addJavadoc("the amount required to buy the taco.\n")
300+
.addJavadoc("the amount required to buy the taco.")
301301
.build())
302302
.addParameter(ParameterSpec.builder(TypeName.INT, "count")
303-
.addJavadoc("the number of Tacos to buy.\n")
303+
.addJavadoc("the number of Tacos to buy.")
304304
.build())
305-
.addJavadoc("Gets the best Taco money can buy.\n")
305+
.addJavadoc("Gets the best Taco money can buy.")
306306
.build();
307307
assertThat(methodSpec.toString()).isEqualTo("/**\n"
308308
+ " * Gets the best Taco money can buy.\n"
@@ -317,10 +317,10 @@ abstract static class AbstractClassWithPrivateAnnotation {
317317
@Test public void withParameterJavaDocAndWithoutMethodJavadoc() {
318318
MethodSpec methodSpec = MethodSpec.methodBuilder("getTaco")
319319
.addParameter(ParameterSpec.builder(TypeName.DOUBLE, "money")
320-
.addJavadoc("the amount required to buy the taco.\n")
320+
.addJavadoc("the amount required to buy the taco.")
321321
.build())
322322
.addParameter(ParameterSpec.builder(TypeName.INT, "count")
323-
.addJavadoc("the number of Tacos to buy.\n")
323+
.addJavadoc("the number of Tacos to buy.")
324324
.build())
325325
.build();
326326
assertThat(methodSpec.toString()).isEqualTo("/**\n"

sourcegen-generator-java/src/test/java/io/micronaut/sourcegen/javapoet/TypeSpecTest.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -1301,17 +1301,17 @@ public void interfacePrivateMethods() {
13011301
@Test
13021302
public void recordComponentJavadoc() throws Exception {
13031303
TypeSpec taco = TypeSpec.recordBuilder("Taco")
1304-
.addJavadoc("Wants a cat to be a tacocat.\n")
1304+
.addJavadoc("Wants a cat to be a tacocat.")
13051305
.addRecordComponent(ParameterSpec.builder(ClassName.get(String.class), "shell")
1306-
.addJavadoc("the outer level of tortilla of the taco\n")
1306+
.addJavadoc("the outer level of tortilla of the taco")
13071307
.build())
13081308
.addRecordComponent(ParameterSpec.builder(boolean.class, "soft")
13091309
.addJavadoc("true for a soft flour tortilla, or false for a crunchy corn tortilla\n")
13101310
.build())
13111311
.compactConstructor(MethodSpec.constructorBuilder()
1312-
.addJavadoc("Makes a taco without a cat :(.\n")
1312+
.addJavadoc("Makes a taco without a cat :(.")
13131313
.addParameter(ParameterSpec.builder(ClassName.get(String.class), "shell")
1314-
.addJavadoc("the outer level of tortilla of the taco\n")
1314+
.addJavadoc("the outer level of tortilla of the taco")
13151315
.build())
13161316
.build())
13171317
.build();

sourcegen-generator-java/src/test/java/io/micronaut/sourcegen/javapoet/write/RecordWriteTest.java

+51-2
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,55 @@ record TestRecord(
4646
assertEquals(expected.strip(), result.strip());
4747
}
4848

49+
@Test
50+
public void writeRecordWithJavadoc() throws IOException {
51+
RecordDef recordDef = RecordDef.builder("test.TestRecord")
52+
.addProperty(PropertyDef.builder("name").ofType(String.class)
53+
.addJavadoc("The person's name").build())
54+
.addProperty(PropertyDef.builder("age").ofType(Integer.class)
55+
.addJavadoc("The person's age")
56+
.build()
57+
)
58+
.addJavadoc("A record representing a person.")
59+
.build();
60+
var result = writeRecord(recordDef);
61+
62+
var expected = """
63+
/**
64+
* A record representing a person.
65+
*
66+
* @param name The person's name
67+
* @param age The person's age
68+
*/
69+
record TestRecord(
70+
String name,
71+
Integer age
72+
) {
73+
}
74+
""";
75+
assertEquals(expected.strip(), result.strip());
76+
}
77+
78+
@Test
79+
public void writeRecordWithOnlyParameterJavadoc() throws IOException {
80+
RecordDef recordDef = RecordDef.builder("test.TestRecord")
81+
.addProperty(PropertyDef.builder("name").ofType(String.class)
82+
.addJavadoc("The person's name").build())
83+
.build();
84+
var result = writeRecord(recordDef);
85+
86+
var expected = """
87+
/**
88+
* @param name The person's name
89+
*/
90+
record TestRecord(
91+
String name
92+
) {
93+
}
94+
""";
95+
assertEquals(expected.strip(), result.strip());
96+
}
97+
4998
private String writeRecord(RecordDef recordDef) throws IOException {
5099
JavaPoetSourceGenerator generator = new JavaPoetSourceGenerator();
51100
String result;
@@ -55,8 +104,8 @@ private String writeRecord(RecordDef recordDef) throws IOException {
55104
}
56105

57106
// The regex will skip the imports and make sure it is a record
58-
final Pattern RECORD_REGEX = Pattern.compile("package [^;]+;[\\s\\S]+" +
59-
"(record \\S+[\\s\\S]+})\\s*");
107+
final Pattern RECORD_REGEX = Pattern.compile("package [^;]+;[^/]+" +
108+
"((?:/\\*\\*[\\S\\s]+\\*/\\s+|)record \\S+[\\s\\S]+})\\s*");
60109
Matcher matcher = RECORD_REGEX.matcher(result);
61110
if (!matcher.matches()) {
62111
fail("Expected record to match regex: \n" + RECORD_REGEX + "\nbut is: \n" + result);

0 commit comments

Comments
 (0)