I am programming object serializer/deserializer classes which need to work on c# and java. I handled the issue in the c# part which is below:
Type listType = typeof(List<>);
Type constructedListType;
type = item.Attributes["Type"].Value;
field = item.Attributes["Field"].Value;
if ((type == "Int32") || (type == "Int64") || (type == "Int16") || (type == "UInt32") || (type == "UInt64") || (type == "UInt16") || (type == "String") || (type == "Datetime") || (type == "Double") || (type == "Single"))
{
constructedListType = listType.MakeGenericType(Type.GetType(String.Format("{0}.{1}", "System", type)));
}
else
{
constructedListType = listType.MakeGenericType(Type.GetType(String.Format("{0}.{1}", WorkingNamespace, type)));
}
IList instance = (IList)Activator.CreateInstance(constructedListType);
But I encounter a problem in Java generating generic arraylist with the type of the object that is come from xml.
**Editted
type = attrMap.getNamedItem("Type").getNodeValue();
field = attrMap.getNamedItem("Field").getNodeValue();
type = reverseTypeConvert(type);
Object oList;
if ((type.equals("Integer") || type.equals("Long") || type.equals("Float") || type.equals("Short") || type.equals("String") || type.equals("Double") || type.equals("Date"))) {
oList = Class.forName(String.format("java.util.ArrayList<%s.%s>","java.util",type)).newInstance();
}
else {
oList = Class.forName(String.format("java.util.ArrayList<%s.%s>",this.workingPackage,type)).getGenericSuperclass();
}
if (!field.equals("none")) {
Method setMethod = cObj.getClass().getMethod(String.format("%s%s", "set",field), oList.getClass());
setMethod.invoke(cObj, oList);
}
deSerializeObject(node.getChildNodes(),oList);
So, I couldn't find the counterpart of c# code in Java. Thanks for any help!
Ok everybody we handle the problem and I understand my mistake. Thank you very much.