in java/computations-api/src/main/java/com/epam/computations/data/GenericRecordConverter.java [67:220]
private <T> void processField(Field field, GenericValueInfo from, T to) {
try {
Class<?> type = field.getType();
if (type == byte.class) {
field.setByte(to, from.getValue(field.getName()).byteValue());
} else if (type == short.class) {
field.set(to, from.getValue(field.getName()).shortValue());
} else if (type == int.class) {
field.setInt(to, from.getValue(field.getName()).intValue());
} else if (type == long.class) {
if (field.getAnnotation(Decimal.class) != null) {
field.setLong(to, from.getValue(field.getName()).decimalValue());
} else {
try {
field.setLong(to, from.getValue(field.getName()).longValue());
} catch (UnsupportedOperationException exc) {
field.setLong(to, from.getValue(field.getName()).decimalValue());
}
}
} else if (type == double.class) {
field.setDouble(to, from.getValue(field.getName()).doubleValue());
} else if (type == float.class) {
field.setFloat(to, from.getValue(field.getName()).floatValue());
} else if (type == boolean.class) {
field.setBoolean(to, from.getValue(field.getName()).byteValue() == 1);
} else if (type == char.class) {
field.setChar(to, from.getValue(field.getName()).charValue());
} else if (type == CharSequence.class) {
CharSequence charSequence = (CharSequence) field.get(to);
if (!(charSequence instanceof StringBuilder)) {
charSequence = new StringBuilder();
field.set(to, charSequence);
}
StringBuilder sb = (StringBuilder) charSequence;
sb.append(from.getValue(field.getName()).charSequenceValue());
} else if (field.getType().isEnum()) {
field.set(to, getEnumValue(field.getType(), from.getValue(field.getName()).charSequenceValue()));
} else if (List.class.isAssignableFrom(field.getType())) {
// ToDo
Class<?> underlineType = ArrayTypeUtil.getUnderline(field.getType());
if (underlineType == byte.class) {
// ToDo
} else if (underlineType == boolean.class) {
BooleanArrayList list = (BooleanArrayList) field.get(to);
if (list == null) {
list = new BooleanArrayList();
field.set(to, list);
}
for (GenericValueInfo value : (ListValueInfo) from) {
list.add(BooleanValueInfo.isTrue((BooleanValueInfo) value));
}
} else if (underlineType == char.class) {
CharacterArrayList list = (CharacterArrayList) field.get(to);
if (list == null) {
list = new CharacterArrayList(from.size());
field.set(to, list);
}
for (GenericValueInfo value : (ListValueInfo) from) {
list.add(value.charValue());
}
} else if (underlineType == short.class) {
ShortArrayList list = (ShortArrayList) field.get(to);
if (list == null) {
list = new ShortArrayList(from.size());
field.set(to, list);
}
for (GenericValueInfo value : (ListValueInfo) from) {
list.add(value.shortValue());
}
} else if (underlineType == int.class) {
IntegerArrayList list = (IntegerArrayList) field.get(to);
if (list == null) {
list = new IntegerArrayList(from.size());
field.set(to, list);
}
for (GenericValueInfo value : (ListValueInfo) from) {
list.add(value.intValue());
}
} else if (underlineType == long.class) {
LongArrayList list = (LongArrayList) field.get(to);
if (list == null) {
list = new LongArrayList(from.size());
field.set(to, list);
}
for (GenericValueInfo value : (ListValueInfo) from) {
list.add(value.longValue());
}
} else if (underlineType == float.class) {
FloatArrayList list = (FloatArrayList) field.get(to);
if (list == null) {
list = new FloatArrayList(from.size());
field.set(to, list);
}
for (GenericValueInfo value : (ListValueInfo) from) {
list.add(value.floatValue());
}
} else if (underlineType == double.class) {
DoubleArrayList list = (DoubleArrayList) field.get(to);
if (list == null) {
list = new DoubleArrayList(from.size());
field.set(to, list);
}
for (GenericValueInfo value : (ListValueInfo) from) {
list.add(value.doubleValue());
}
} else if (underlineType == Object.class) {
ObjectArrayList list = (ObjectArrayList) field.get(to);
if (list == null) {
list = (ObjectArrayList) field.getType().newInstance();
field.set(to, list);
}
list.clear();
ListValueInfo listValue = (ListValueInfo) from.getValue(field.getName());
for (int i = 0; i < listValue.size(); i++) {
ObjectValueInfo objectValue = (ObjectValueInfo) listValue.get(i);
if (objectValue != null && !objectValue.isNull()) {
String objectType = objectValue.getType().charSequenceValue().toString();
Class<?> clazz = classLoader.loadClass(objectType);
Object object = clazz.getConstructor().newInstance();
list.add(object);
for (Field field1 : getFields(clazz)) {
processField(field1, objectValue, object);
}
} else {
field.set(to, null);
}
}
} else {
throw new UnsupportedOperationException();
}
} else {
GenericValueInfo valueInfo = from.getValue(field.getName());
if (valueInfo != null && valueInfo.isNotNull()) {
ObjectValueInfo objectValue = (ObjectValueInfo) valueInfo;
String objectType = objectValue.getType().charSequenceValue().toString();
Class<?> clazz = classLoader.loadClass(objectType);
Object value = field.get(to);
if (value == null || value.getClass() != clazz) {
value = clazz.getConstructor().newInstance();
field.set(to, value);
}
for (Field field1 : getFields(clazz)) {
processField(field1, objectValue, value);
}
} else {
field.set(to, null);
}
}
} catch (InstantiationException | InvocationTargetException | NoSuchMethodException | IllegalAccessException | ClassNotFoundException e) {
e.printStackTrace();
}
}