有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

不知道参数类型时如何调用java反射

我希望将参数名称、类型和值保存在xml文件中,并通过反射调用和设置对象值。例如:

<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"));

如果您知道如何将对象值“500”解析为paramClass,请回答该线程来帮助我

谢谢


共 (0) 个答案