Quick start guide

In this quide we will see how to map fields between classes. We will only show mapping of simple class. The mapping of complex classes that can be made of collection, subclass is discuss in the main documentation.

In the remaining part of this document we will consider the following classes :

public class SimpleSource {
    
    private String id ;
    private String v2 ;
    private String v3 ;
    private Integer intVal ;
...
}
            
public class SimpleDest {
    
    private String id ;
    private String v2 ;
    private String v_3 ;
    private Integer int_val ;
...
}
            

Simple mapping

The following code define a mapping between SimpleSource class and SimpleDest class. Each field from SimpleSource will be mapped to the field with the same name of SimpleDest. The mapping will also check if the type of the fields are compatible (assignable).

ClassMapperBuilder builder= new ClassMapperBuilder()
builder.mapping(SimpleSource.class, SimpleDest.class);
SimpleSource src = new SimpleSource() ;
SimpleDest dest = new SimpleDest() ;
builder.getMapper().map(src,dest);
                

The previous code mapped the fields : id and v2 from the src instance to the dest instance.

It is important to note that the mapping is bidirectional. following code snipet assign the fields id and v2 from the dest instance to the src instance

builder.getMapper().map(dest,src);                
                

Custom mapping

As shown in the previous section the call to builder.mapping(Class1.class, Class2.class) map the fieldsid and v2 between id and id classes. Non trivial mapping can be defined as follow:

ClassMapperBuilder builder= new ClassMapperBuilder()
builder.mapping(SimpleSource.class, SimpleDest.class)
    .fieldMapping("v3","v_3")
    .fieldMapping("intVal","int_val");
SimpleSource src = new SimpleSource() ;
SimpleDest dest = new SimpleDest() ;
builder.getMapper().map(src,dest);
                

The call to .fieldMapping("v3","v_3"); and .fieldMapping("intVal","int_val"); add the mapping of the field v3 and intVal resprctively tov_3 and int_val.