Let's say I have some API that returns a list of objects of lots of different types(100+). All of these types may or may not implement certain methods that I want to call, such as getSum or getName or getId.
How can I call these methods when I only know the types at runtime? How would you design this in an elegant way?
I would like to call these methods and return some correct value if it's actually implemented and otherwise return null, maybe even an Optional.
I know I can use reflection and I looked into something like Dynamic Proxy Classes but this only works if the objects actually implements some common interface, which is not the case.
Aucun commentaire:
Enregistrer un commentaire