Open mattmook opened 5 years ago
👍
Note: we usually do data class Something(val data: List<@JvmSuppressWildcards OtherClass>)
as a workaround.
Meant to mention that as a workaround, but it means your class is then compiling as the invariant List<OtherClass>
which isn't ideal if you still care about Java compatibility. You want to use bounded wildcards to increase API flexibility as per Item 31 in Effective Java Third Edition.
When using jfixture on Kotlin it's common to define a class with a generic type as follows:
In Java this compiles down to the equivalent of
List<? extends OtherClass>
.It would be possible to at least perform some basic logic in this scenario of a
WildcardType
, by for example, detecting a single upper bounds and using that for the type information rather than throwing anUnsupportedOperationException
inSpecimenType.getFields()
.Something similar to the following for example:
Of course the downside is it will only create types of that type, rather than super types but often that is enough.