Java Codegen: classes get keyToJson via inheritance rather than re-generating for each class (#18385)

This commit is contained in:
Raphael Speyer 2024-02-07 11:10:53 +11:00 committed by GitHub
parent 094c392ae7
commit 2ff3f7c378
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -132,26 +132,11 @@ private[inner] object ToJsonGenerator {
.methodBuilder("keyJsonEncoder")
.addModifiers(Modifier.PUBLIC)
.returns(classOf[JsonLfEncoder])
.addAnnotation(classOf[Override])
.addStatement("return this.key.map($L).orElse(null)", encoderOf(keyDamlType))
.build()
val toString = MethodSpec
.methodBuilder("keyToJson")
.addModifiers(Modifier.PUBLIC)
.addStatement("var enc = keyJsonEncoder()")
.addStatement("if (enc == null) return null")
.addStatement("var w = new $T()", classOf[StringWriter])
.beginControlFlow("try")
.addStatement("enc.encode(new $T(w))", classOf[JsonLfWriter])
.nextControlFlow("catch ($T e)", classOf[IOException])
.addComment("Not expected with StringWriter")
.addStatement("throw new $T(e)", classOf[UncheckedIOException])
.endControlFlow()
.addStatement("return w.toString()")
.returns(classOf[String])
.build()
Seq(encoder, toString)
Seq(encoder)
}
// When a type has type parameters (generic classes), we need to tell