I'm a bit new with Kotlin and Java and have following question: how to check if reflected property type is ArrayList of specific type, for example is ArrayList<Pair<String, ByteArray>>?
Code behind:
class DataUploadFormData {
var files: ArrayList<Pair<String, ByteArray>> = arrayListOf()
var collection: String? = null
}
// DataUploadFormData type is passed here as T
suspend inline fun <reified T : Any> mapFormData(data: MultiPartData): T? {
val formData: FormData = getFormData(data)
val model: T? = getValue()
var fields = T::class.memberProperties
fields.forEach { field ->
if (field is KMutableProperty<*>) {
setFieldValue(field, formData, model)
}
}
return model
}
@OptIn(ExperimentalStdlibApi::class)
fun <T> setFieldValue(field: KMutableProperty<*>, formData: FormData, model: T?) {
var value: Any? = null
if (field.returnType.javaType == String::class.createType().javaType ||
field.returnType.javaType == Int::class.createType().javaType ||
field.returnType.javaType == Double::class.createType().javaType
) {
value = formData.fields.firstOrNull(predicate = { it.first == field.name })?.second
}
else if (
// check if field.returnType is of ArrayList<Pair<String, ByteArray>> type?
) {
value = formData.files
}
field.setter.call(model, value)
}
Checking simple data types works well but have no idea how to check if reflected property is of complex types. I tried to instantiate property and use is
or instanceof
but I couldn't instantiate property with code like that field.returnType.javaType::class.createInstance()
. Check field.returnType.classifier is ArrayList<*>
returns false.
Aucun commentaire:
Enregistrer un commentaire