Hi there as I mentioned in title I want create program which read xml and create beans(Objects) from it.
<beans>
<bean id="person3" class="Person">
<property name="id" value="3"/>
<property name="firstname" value="Jhone"/>
<property name="lastname" value="Smith"/>
<property name="email" value="jhone.smith@gmail.com"/>
<property name="age" value="31"/>
</bean>
I read the nodes and their attributes and put them in the map. the problem is creating instances of that beans. here is the method
public Object createInstance(String className, LinkedHashMap<String,Object> attributes){
Class<?> tt = Class.forName(className);
Field[] declaredFields = tt.getDeclaredFields();
Class<?>[] types = Arrays.stream(declaredFields).map(Field::getType).toArray(Class<?> []::new);
Object[] values = attributes.values().toArray();
Object[] valuesMatch = new Object[types.length];
Constructor<?> constructor = tt.getConstructor(types);
constructor.newInstance(values); //throws Exception
so I got values, and types of that values. values are All of String type. the question is , is there any way to create an instance of Object by its type and String value? I need something like this
Object (Class class, String value){
the class type is Integer,
so String value will be parse to int
Integer i = Integer.valueOf(value)
return i;}
Aucun commentaire:
Enregistrer un commentaire