Skip to content

Commit

Permalink
add classtypedef to annotation value (#183)
Browse files Browse the repository at this point in the history
  • Loading branch information
elifKurtay authored Nov 11, 2024
1 parent b499bdf commit 2a85128
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,8 @@ private void addAnnotationValue(AnnotationSpec.Builder builder, String memberNam
builder.addMember(memberName, "$Lf", value);
} else if (value instanceof Character) {
builder.addMember(memberName, "'$L'", Util.characterLiteralWithoutSingleQuotes((char) value));
} else if (value instanceof ClassTypeDef typeDef) {
builder.addMember(memberName, "$L.class", typeDef.getSimpleName());
} else {
builder.addMember(memberName, "$L", value);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,4 +142,35 @@ private String writeRecord(RecordDef recordDef) throws IOException {
return matcher.group(1);
}


@Test
public void annotationClassValue() throws IOException {
RecordDef recordDef = RecordDef.builder("test.$TestRecord")
.addAnnotation(AnnotationDef.builder(ClassTypeDef.of("jackson.annotation.JsonSubTypes.Type"))
.addMember("value", ClassTypeDef.STRING)
.addMember("name", "string")
.build())
.build();
JavaPoetSourceGenerator generator = new JavaPoetSourceGenerator();
String result;
try (StringWriter writer = new StringWriter()) {
generator.write(recordDef, writer);
result = writer.toString();
}

var expected = """
package test;
import jackson.annotation.JsonSubTypes;
@JsonSubTypes.Type(
value = String.class,
name = "string"
)
record $TestRecord() {
}
""";
assertEquals(expected.strip(), result.strip());
}

}

0 comments on commit 2a85128

Please sign in to comment.