ShapeShift
  • Overview
  • Introduction
    • Installation
    • Quick Start
  • API Documentation
    • Annotations
    • Kotlin DSL
    • Java Builder
  • Features
    • Transformers
    • Auto Mapping
    • Conditions
    • Decorators
    • Object Suppliers
    • Mapping Strategy
  • Guides
    • Implicit Transformers
    • Instance Mapping
    • Advanced Mapping
    • Spring Usage
    • Android Usage
Powered by GitBook
On this page

Was this helpful?

Export as PDF
  1. Guides

Instance Mapping

Mapping to destination classes without a no arg constructor.

PreviousImplicit TransformersNextAdvanced Mapping

Last updated 2 years ago

Was this helpful?

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 Instance Mapping comes into play, you can pass already-instantiated destination objects to the map method.

Version 0.0.7 of ShapeShift introduced new functionality as an improved solution for the no arg constructor issue.

@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...
}
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...
}

SimpleEntityDisplay does not have a no arg constructor. You can either add a no arg constructor or initiate a new instance of SimpleEntityDisplay and pass it to the map function.

val shapeShift = ShapeShiftBuilder().build()
val simpleEntity = SimpleEntity("test", "test description", "private data")
val simpleEntityDisplay = SimpleEntityDisplay("", "")
// Passing simpleEntityDisplay as a destination instance 
val result = shapeShift.map(simpleEntity, simpleEntityDisplay)
ShapeShift shapeShift = new ShapeShiftBuilder().build();
SimpleEntity simpleEntity = new SimpleEntity();
simpleEntity.setName("test");
simpleEntity.setDescription("test description");
simpleEntity.setPrivateData("private data");
SimpleEntityDisplay simpleEntityDisplay = new SimpleEntityDisplay("", "");
// Passing simpleEntityDisplay as a destination instance 
SimpleEntityDisplay result = shapeShift.map(simpleEntity, simpleEntityDisplay);
Object Suppliers