Reputation: 339
I would like to save some documents with the value containing a generic. And I constantly receive a StackOferflowError
.
Here is a fragment of my model class
data class MyDocument {
val errors: List<SomeError> = emptyList()
}
SomeError is in interface that should be implemented by different types of errors including ValidationError
interface SomeError
data class ValidationError<T: Any>(val objectType: KClass<T>, val objectId: String) : SomeError
I am trying to save my object with non-empty list of errors (using ReactiveMongoRespoitory):
myDocumentRepository.save(MyDocument(
errors = listOf(
ValidationError<MyDocument>(objectType = MyDocument::class, objectId = "somedoc")) //as SomeError did not help
))
Do you know how I can correct it?
This is a psrt of the Stack trace:
java.lang.StackOverflowError
at java.base/java.util.HashMap.putVal(HashMap.java:624)
at java.base/java.util.HashMap.putMapEntries(HashMap.java:510)
at java.base/java.util.HashMap.putAll(HashMap.java:780)
at org.springframework.data.util.TypeDiscoverer.resolveType(TypeDiscoverer.java:168)
at org.springframework.data.util.ParameterizedTypeInformation.calculateTypeVariables(ParameterizedTypeInformation.java:269)
Upvotes: 0
Views: 299
Reputation: 1971
You can do something like this:
import org.springframework.data.annotation.Transient
data class ValidationError private constructor(
private val type: String,
val objectId: String
) : SomeError {
constructor(type: KClass<*>, objectId: String) : this(type.qualifiedName!!, objectId)
@delegate:Transient
val objectType by lazy {
this.javaClass.classLoader.loadClass(type).kotlin
}
}
Store KClass internally as fully qualified name.
Upvotes: 1