com.sippnex.firemaw.FiremawProcessor Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of firemaw-server Show documentation
Show all versions of firemaw-server Show documentation
Server-side Spring library for firemaw. A dynamic web entity manager
package com.sippnex.firemaw;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class FiremawProcessor implements IFiremawProcessor {
public FiremawDto serialize(Object object, Class clazz) {
try {
FiremawDto firemawEntity = new FiremawDto();
// Process class field annotations, including all super classes
Class currentClass = clazz;
do {
List currentPropertyList = new ArrayList();
for(Field field: currentClass.getDeclaredFields()) {
FiremawPropertyClass property = this.serializeField(object, field);
if(property != null) {
currentPropertyList.add(property);
}
}
firemawEntity.addPropertyRange(0, currentPropertyList);
currentClass = currentClass.getSuperclass();
} while(currentClass != null);
return firemawEntity;
} catch (Exception ex) {
ex.printStackTrace();
return null;
}
}
public Object deserialize(FiremawDto firemawDto, Class clazz) {
try {
Object object = clazz.newInstance();
List firemawPropertyClasses = firemawDto.getProperties();
// Process class field annotations, including all super classes
Class currentClass = clazz;
do {
List currentPropertyList = new ArrayList();
for(Field field: currentClass.getDeclaredFields()) {
Object value = this.deserializeField(firemawDto, field);
if(value != null) {
field.set(object, value);
}
}
currentClass = currentClass.getSuperclass();
} while(currentClass != null);
return object;
} catch (Exception ex) {
ex.printStackTrace();
return null;
}
}
private FiremawPropertyClass serializeField(Object object, Field field) throws Exception {
field.setAccessible(true);
FiremawProperty firemawProperty = field.getAnnotation(FiremawProperty.class);
if(firemawProperty != null) {
if(firemawProperty.type() == FiremawType.SubEntity) {
Class clazz = field.get(object).getClass();
FiremawDto firemawDto = this.serialize(field.get(object), clazz);
return new FiremawPropertyClass(firemawProperty, firemawDto);
} else if(firemawProperty.type() == FiremawType.SubEntityList) {
List