try {
componentBeanInfo = Introspector.getBeanInfo(sourceObject.getClass());
final PropertyDescriptor[] props=componentBeanInfo.getPropertyDescriptors();
String [] parameters = getParameters(); //ObjectA.code="abc",ObjectA.type="single"
for (String parameter : parameters) {
boolean isNestedField = isNestedPropertyRead(parameter);
for(PropertyDescriptor prop : props){
if(isNestedField){
String[] fullParam = parameter.split("\\.");//ObjectA.code
String nestedObj = fullParam[0];//ObjectA
String nestObjField = fullParam[1];//code
if(nestedObj.equalsIgnoreCase(prop.getName())){
Class<?> classType = Class.forName(prop.getPropertyType().getName());
BeanInfo nestedBeanInfo;
nestedBeanInfo = Introspector.getBeanInfo(classType);
final PropertyDescriptor[] nestedProps =nestedBeanInfo.getPropertyDescriptors();
for(PropertyDescriptor nestedProp : nestedProps){
if(nestedProp.getName().equalsIgnoreCase(nestObjField)){
Class<?> nestedClassType = Class.forName(nestedProp.getPropertyType().getName());
Object value = convertToObject(nestedClassType,value(parameter));
try {
/*if(isNewProperty(prop.getName(),propNames)){
nestedObject = classType.newInstance();
}*/
nestedObject = classType.newInstance();
nestedProp.getWriteMethod().invoke(nestedObject, value);
prop.getWriteMethod().invoke(sourceObject,nestedObject );
} catch (IllegalArgumentException | InstantiationException | InvocationTargetException e) {
e.printStackTrace();
}
break;
}
}
break;
}
}
else if(prop.getName().equalsIgnoreCase(parameter)){
try {
Class<?> classType = Class.forName(prop.getPropertyType().getName());
Object value = convertToObject(classType,value(parameter));
prop.getWriteMethod().invoke(sourceObject, value);
break;
} catch (IllegalArgumentException | InvocationTargetException e) {
e.printStackTrace();
}
}
}
}
}catch (IntrospectionException | IllegalAccessException e) {
e.printStackTrace();
}
In above code snippet I'm facing issue w.r.t following lines -
nestedObject = classType.newInstance();
This creates new Instance every time and because of which i end up up setting values to new nested objects each time overriding the previous object. How can i avoid this and set values to already created object in loop. Can anyone please suggest how can i get it working?
Aucun commentaire:
Enregistrer un commentaire