samedi 19 juin 2021

Parse Java reflection class

Let's say I have a method called Class<?> classInstance = createInstance("Page", map, factory) which creates an instance of "Page" with java reflection and does some stuff with it. For demonstration purposes I've called it "Page", but it could be any of my classes.

Now I want to add this object to a List<Page>. To call list.add(classInstance) and add it to the list I need to parse it to "Page". Is there a way to do it, given the only information I have is the string containing the class name? So instead of doing (Page) classInstance I need to do something like (Class.forName("Page")) classInstance.

I can not modify the List or the way it is added to the list.

Thank you.





Aucun commentaire:

Enregistrer un commentaire