Secondary constructors should be allowed to throw exceptions instead of calling this() constructor

I’m trying to use Kotlin with Apache CXF which in turn uses JAXB and requires XML DTOs to have default constructor.

I try to use Kotlin data classes as XML DTOs to marshall response What JAXB needs a public no-arg constructor for? currently I’m forced to provide each and every data class used as JAXB DTO either with default on every val or with an ugly additional constructor which calls the default constructor of data class specifying all fields and then throws IllegalStateException()

Is it a restriction of JVM or is there a workaround?

Having to delegate to another constructor is a requirement of Kotlin, not a JVM restriction. However, I believe that Kotlin already provides a good solution for your problem - namely, the no-arg compiler plugin.

1 Like

Thanks, I did not know about it. I’ve totally skipped compiler plugins section of documentation because I thought that it is something complicated and unnecessary. Keeping public empty constructors in java just for serialization is annoying.

Is it possible to use custom externalizable serializators and still keep fields val?