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.

Instance Mapping is another solution for the no arg constructor issue. The advantage of Object Suppliers is that they also work when mapping classes containing subclasses while Instance Mapping only solves the issue for the top level class instance.

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
)

The destination class does not have a no arg constructor.

data class SimpleEntityDisplay(
    val name: String,
    val description: String
)

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.

class SimpleEntityDisplaySupplier : Supplier<SimpleEntityDisplay> {
    override fun get(): SimpleEntityDisplay {
        return SimpleEntityDisplay("", "")
    }
}

And register it to the ShapeShift instance.

val shapeShift = ShapeShiftBuilder()
    .withObjectSupplier(SimpleEntityDisplaySupplier())
    .build()

Inline Object Suppliers

It is also possible to add the object supplier logic inline.

val shapeShift = ShapeShiftBuilder()
    .withObjectSupplier { SimpleEntityDisplay("", "") }
    .build()

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.

val simpleEntity = SimpleEntity("test", "test description", "private data")
val simpleEntityDisplay = SimpleEntityDisplay("", "")
val result = shapeShift.map<SimpleEntityDisplay>(simpleEntity)

Last updated