Skip to content

Overview

avurro edited this page Apr 12, 2016 · 20 revisions

Before describing the features of the framework is good to do a little introduction: the Destination is the instance that captures the data, the Source is the instance that contains the data, the goal is to get the Destination starting from the Source.
The configurations are associated with the instance variables and may be present in the Destination and Source, then we will call target field the target field of the configuration and mapped field the field that contains the configuration.
As a prerequisite classes used for mapping must comply with the Javabean conventions.
However is possible to define get and set custom methods.

Below are shown the classes that will be used in the following examples:

class Destination{                      class Source{

    private String id;                      private String id;
    private String destinationField;        private String sourceField;
    private String other;                   private String other;

    // getters and setters...               // getters and setters...
}                                       }

As mentioned in the introduction, JMapper allows you to configure the mapping using Annotations, XML and API. In the next chapter we will learn to configure classes with annotation and especially with @JMap.

IMPORTANT!
The XML configuration and API have greater visibility of the equivalent annotation.
The configuration can be written in both classes, the result is the same.

Annotated configuration

The annotation used to configure the single field is @JMap, it has several parameters, including: value.
Value is the default parameter of the annotations and therefore isn't necessary to explain it if used individually.

class Destination{                      
    
    /* implicit mapping when mapped field name and target field name match */
    @JMap 
    private String id;                      
    /* explicit mapping when mapped field name and target field name don't match */
    @JMap("sourceField") 
    private String destinationField;       
    private String other;                  

    // getters and setters...                
}                                     

XML configuration

this is the xml configuration equivalent to the previous one written with annotation:

<jmapper>
  <class name="it.jmapper.bean.Destination">
    <attribute name="id">
      <value name="id"/>
    </attribute>
    <attribute name="destinationField">
      <value name="sourceField">
    </attribute>
  </class>
</jmapper>

For writing automated XML, read the XML handling section.

API configuration

This is the version of the configuration via API.
It suggest to make the import of the static methods. ```java ... import static com.googlecode.jmapper.api.JMapperAPI.*; ...

JMapperAPI jmapperAPI = new JMapperAPI() .add(mappedClass(Destination.class) .add(attribute("id") .value("id")) .add(attribute("destinationField") .value("sourceField")));


#Example

Once set up, we go immediately to a practical example

```java
JMapper <Destination, Souce> jmapper;

// with annotation
jmapper = new JMapper <>(Destination.class, Source.class);

// with xml configuration: 
jmapper = new JMapper <>(Destination.class, Source.class, "path/configuration.xml");

// with API
jmapper = new JMapper <>(Destination.class, Source.class, jmapperAPI);

Source source = new Source("id", "sourceField", "other");

Destination destination = jmapper.getDestination(source);

IMPORTANT! for a detailed explanation regarding the constructors go to the wiki page for more information

The class to use is JMapper, takes as input two classes: the first from the left, belonging to the Destination and the second to Source, then just invoke the method getDestination passing an instance of Source to create an instance of Destination.
The obtained destination is the following:

Destination [id="id", destinationField="source field", other=null]
Clone this wiki locally