I have this code which I need (it) to inject fields at runtime:
public void marshal(Object source, HierarchicalStreamWriter writer, MarshallingContext context) {
TestEntity entity = (TestEntity) source;
entity.getPropertyMap().forEach((fieldName, fieldValue) -> {
// "fieldName" is an arbritary name not known at compile time
injectFieldTo(source, fieldName, fieldValue);
});
entity.setPropertyMap(null);
super.marshal(source, writer, context);
}
Is there a way to do this in Java using ByteBuddy? And if not directly (even with just a clone object), what can be done as such fields can be injected into the object? What is the process?
TestEntity.java
public class TestEntity implements Serializable {
private String entityType;
private String entityId;
private String dateCreated;
private String dateUpdated;
private Boolean publicRead;
private Boolean publicWrite;
private Map<String, Object> propertyMap;
}