Skip to content

Instantly share code, notes, and snippets.

@novoj
Created October 29, 2016 14:36
Show Gist options
  • Save novoj/fe4336602e8e37f098794ea7cf67e87a to your computer and use it in GitHub Desktop.
Save novoj/fe4336602e8e37f098794ea7cf67e87a to your computer and use it in GitHub Desktop.
private static class SerializableProxyDescriptor implements Serializable {
private static final long serialVersionUID = 8401525823871149500L;
private final Class mainClass;
private final Class[] interfaces;
private final Object target;
private final DeserializationProxyFactory deserializationProxyFactory;
private final Map<String, Object> pojoFields = new HashMap<String, Object>();
private SerializableProxyDescriptor(Class mainClass, Class[] interfaces, Object target, DeserializationProxyFactory deserializationProxyFactory) {
this.mainClass = mainClass;
this.interfaces = interfaces;
this.target = target;
this.deserializationProxyFactory = deserializationProxyFactory;
}
public void printPojoFields(Object pojo) {
try {
Class currentClass = pojo.getClass();
while(currentClass != null && !currentClass.equals(Object.class) && !currentClass.getPackage().getName().startsWith("com.fg.metadata")) {
for(Field field : currentClass.getDeclaredFields()) {
try {
if (!field.getName().contains("CGLIB") && !Modifier.isStatic(field.getModifiers())) {
makeAccessible(field);
addFieldValueToSerialize(field.getName(), getField(field, pojo));
}
} catch(Exception ignored) {
log.warn("Cannot access field: " + mainClass.getName() + "#" + field.getName());
}
}
currentClass = currentClass.getSuperclass();
}
} catch (Exception ex) {
log.warn("Cannot serialize additional fields of: " + pojo.getClass().getName() + " (" + ex.getMessage() + ")");
}
}
@SuppressWarnings({"ReadResolveAndWriteReplaceProtected"})
public Object readResolve() throws ObjectStreamException {
//noinspection unchecked
final Object pojo = deserializationProxyFactory.deserialize(target, mainClass, interfaces);
//init all pojo fields
try {
final Object targetPojo = pojo instanceof Advised ? ((Advised)pojo).getTargetSource().getTarget() : pojo;
for(Map.Entry<String, Object> pojoField : pojoFields.entrySet()) {
final Field field = findField(targetPojo.getClass(), pojoField.getKey());
if(field != null) {
makeAccessible(field);
setField(field, targetPojo, pojoField.getValue());
}
}
} catch(Exception ex) {
log.warn("Cannot deserialize class " + pojo.getClass().getName() + " (" + ex.getMessage() + ")");
}
return pojo;
}
private void addFieldValueToSerialize(String fieldName, Object value) {
if (!JavAssistMethodHandler.class.isInstance(value)) {
try {
SerializationUtils.serialize(value);
this.pojoFields.put(fieldName, value);
} catch(Exception ignored) {
//cannot serialize field
log.warn("Cannot serialize field: " + mainClass.getName() + "#" + fieldName);
}
}
}
}
public interface DeserializationProxyFactory extends Serializable {
Object deserialize(Object target, Class mainClass, Class[] interfaces);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment