Object Suppliers
Mapping to destination classes without a no arg constructor.
Due to the fact that ShapeShift uses reflection behind the scenes, destination classes need a no arg constructor. But in some cases you have no control over the destination classes and cannot modify them to add a no arg constructor. This is where Object Suppliers comes into play, you can register object suppliers to the ShapeShift instance to add your own logic for instance generation.
We have two classes, the source class SimpleEntity and the destination class SimpleEntityDisplay.
@DefaultMappingTarget(SimpleEntityDisplay::class)
data class SimpleEntity(
@MappedField
val name: String,
@MappedField
val description: String,
val privateData: String
)@DefaultMappingTarget(SimpleEntityDisplay.class)
public class SimpleEntity {
@MappedField
private String name;
@MappedField
private String description;
private String privateData;
// Getters and Setters...
}The destination class does not have a no arg constructor.
data class SimpleEntityDisplay(
val name: String,
val description: String
)public class SimpleEntityDisplay {
private String name;
private String description;
public SimpleEntityDisplay(String name, String description) {
this.name = name;
this.description = description;
}
// Getters and Setters...
}To solve this issue we need to either use instance mapping or add an object supplier for the class.
Adding Object Suppliers
Adding object suppliers is available through the ShapeShiftBuilder class. Object suppliers can be added inline or as a separate class.
Class Object Suppliers
To create an object supplier class implement the Supplier interface.
And register it to the ShapeShift instance.
Inline Object Suppliers
It is also possible to add the object supplier logic inline.
Mapping with Object Suppliers
Now that we added an object supplier for the SimpleEntityDisplay class we can map to it as if it has a no arg constructor.
Last updated
Was this helpful?