lundi 8 février 2016

How to call java reflection when you don't know the parameter type

I want to save the parameter name, type and value in an xml file and call and set object values through reflection. For example:

<object type="ObjectType">
    <param name="param" type="Integer" value="500"></param>
    <param name="param2" type="BigDecimal" value="500"></param>
</object>

Class c = Class.forName("ObjectType");
Object parsedObj = c.newInstance();

Class paramClass = Class.forName("java.math.BigDecimal");
Method method = c.getDeclaredMethod(attribute.getParam(), new Class[]{paramClass});

// Here is the problem, I do not know how to cast the object to correct object type.

method.invoke(parsedObj, new BigDecimal("500"));

If you know how to parse the object value "500" to paramClass, please help me by answering the thread.

Thanks.





Aucun commentaire:

Enregistrer un commentaire