I am trying recursively get annotated fields with prefix for nested annotated fields. So for class SomeClassA
I want to get:
- field someString + prefix ""
- field stringB + prefix "someClassB."
data class SomeClassA(
private val someClassB : SomeClassB,
@SomeAnnotation
private val someString: String)
data class SomeClassB(
@SomeAnnotation
private val stringB: String,
@JsonIgnore
private val someClassA: SomeClassA)
This works for simple situations, but when classes have bidirectional properties, I get stackoverflow (because recursive call)
private fun <T : Annotation> getAnnotatedFields(fields: Array<Field>, clazz: Class<T>, prefix: String = ""): List<Pair<Field, String>> =
fields
.filter { BeanUtils.isSimpleProperty(it.type)
.and(Objects.nonNull(it.getAnnotation(clazz))) }
.map { it to prefix } +
fields
.filter { BeanUtils.isSimpleProperty(it.type).not()
.and(Objects.isNull(it.getAnnotation(JsonIgnore::class.java))) }
.map { getAnnotatedFields(it.type.declaredFields, clazz,"$prefix${it.name}.") }
.flatMap { it.toList() }
getAnnotatedFields(SomeClassA::class.java.declaredFields, SomeAnnotation::class.java)
So for now I annotate bidirectional field with different annotation (It is @JsonIgnore
, but not really important) and filter this fields .and(Objects.isNull(it.getAnnotation(JsonIgnore::class.java)))
Maybe someone can suggest how can I do it better?
Aucun commentaire:
Enregistrer un commentaire