mardi 16 février 2021

Dynamically accessing nested fields from a pojo by field name

I am building functionality that is intended to support any arbitrary POJOs. I don't know the specifics of the POJO, I am just given it as input. I have to access certain fields from it, provided to me in string format, for example fieldA.nestedFieldB.nestedFieldC and I need to extract the value from the POJO.

How can I do this in a generic way? Is reflection the way to go? I thought about loading the POJO into a map and then accessing it that way but I think the conversion would be expensive and hope there is a better way.

Also there is no guarantee about what type the fields are.





Aucun commentaire:

Enregistrer un commentaire