mardi 15 novembre 2016

Creating primitive types/objects using Java Reflection

I am trying to create an object creator that asks the user for input and then creates an object. I have an array of Contructors for a given class, with these contructors, I let the user choose one and if that constructor has parameters, I would like the user to set those parameters on the fly.

With the constructor list from java.lang.reflect.Constructor; given below:

constructorList[0].getGenericParameterTypes()[0]

This returns an array of 'Type'

Is it possible to create an instance of this type given some value by the user? Say its an int type, if I wanted that int to be 10 -- (I know this wont work but theoretically)

constructorList[0].getGenericParameterTypes()[0] intValue = 10;

If its an object, how would I get the Class from Type? From there I guess I can just make a recursive call to the objectcreator I am making.





Aucun commentaire:

Enregistrer un commentaire