Skip to content

Constructors

avurro edited this page Mar 22, 2016 · 8 revisions

There are many signatures of the JMapper constructor, so as to allow the definition of only the necessary values.

Annotation configuration

new JMapper <>(Destination.class, Source.class);

In this case JMapper search the annotated configuration in both classes. If both classes are configured, the framework will consider the configuration of the Destination Class.

XML configuration

If you wrote the xml configuration file, you have to pass the path to the constructor, as below:

new JMapper <>(Destination.class, Source.class, "xml/jmapper.xml");

If for a field or class there isn't a configuration, JMapper will search among the annotations if they exist.

Remember the xml file must be accessible from your classpath at runtime. If you want define an external path, you need to add this prefix: file: for example:
Windows "file:/C:/path/jmapper.xml"
Unix "file:/Users/path/jmapper.xml"

XML configuration as String

If you have an xml in String format, you can pass it as input to the JMapper

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

new JMapper<>(Destination.class, Source.class, xml);

API configuration

It suggest to make the import of the static methods.

...
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"))
    );

new JMapper <>(Destination.class, Source.class, jmapperAPI);

As XML, if for a field or class there isn't a configuration, JMapper will search among the annotations if they exist.

Explicit configuration definition

If you want to define which configuration to be analyze, simply pass to the constructor the ChooseConfig enumeration.
For each constructor previously described there is an equal constructor plus ChooseConfig parameter.

For example:

new JMapper <>(Destination.class, Source.class, ChooseConfig.SOURCE);
ChooseConfig.SOURCE it evaluates the configuration of Source
ChooseConfig.DESTINATION it evaluates the configuration of Destination
Clone this wiki locally