Java Model Converter
This utility library helps to convert from one POJO to another using public getters and setters via reflection mechanism.
Table of contents
Getting started
To add library to your project perform next steps:
Maven
Add the following dependency to your pom.xml:
<dependency>
<groupId>com.github.vladislavsevruk</groupId>
<artifactId>model-converter</artifactId>
<version>1.0.2</version>
</dependency>
Gradle
Add the following dependency to your build.gradle:
implementation 'com.github.vladislavsevruk:model-converter:1.0.2'
Usage
ModelConverter uses POJO's getters and setters with matching names to get and set values from one POJO to another. Getters and setters name can be in both classic and fluent styles, e.g. getter with name getField
can match setters with setField
or field
names (same setter with name setField
can match getters with getField
or field
names). If return type of getter doesn't match parameter type of setter it'll be converted to required type if any of predefined converters can perform such conversion. You can add your custom converter if you want to set custom conversion logic or override logic of existent one.
Non-parameterized classes
Let's assume that we have following POJO:
public class DonorModel {
private Set<String> field1;
private Integer field2;
// getters and setters
}
and we want to convert it to following POJO:
public class AcceptorModel {
private List<String> field1;
private String field2;
// getters and setters
}
All you have to do is to use ModelConverter.convert(Object, Class<T>)
method:
// creating POJO with values to convert
DonorModel donorModel = new DonorModel();
donorModel.setField1(Collections.singleton("specificValue"));
donorModel.setField2(2);
// converting POJO to another one
AcceptorModel acceptorModel = new ModelConverter().convert(donorModel, AcceptorModel.class);
// verifying field values
Assertions.assertEquals(1, acceptorModel.getField1().size());
Assertions.assertEquals("specificValue", acceptorModel.getField1().get(0));
Assertions.assertEquals("2", acceptorModel.getField2());
Parameterized classes
Let's assume that we have following generic POJO:
public class GenericDonorModel<T> {
private Set<String> field1;
private T field2;
// getters and setters
}
and we want to convert it to following generic POJO:
public class GenericAcceptorModel<T> {
private List<String> field1;
private T field2;
// getters and setters
}
All you have to do is to use ModelConverter.convert(Object, TypeProvider<T>)
method:
// creating POJO with values to convert
GenericDonorModel<Integer> donorModel = new GenericDonorModel<>();
donorModel.setField1(Collections.singleton("specificValue"));
donorModel.setField2(2);
// converting POJO to another one
GenericAcceptorModel<String> acceptorModel = new ModelConverter()
.convert(donorModel, new TypeProvider<GenericAcceptorModel<String>>() {});
// verifying field values
Assertions.assertEquals(1, acceptorModel.getField1().size());
Assertions.assertEquals("specificValue", acceptorModel.getField1().get(0));
Assertions.assertEquals("2", acceptorModel.getField2());
Adding custom converters
If you want to set custom conversion logic for any type or override logic of existent one you can implement ClassTypeConverter for non-parameterized type or ParameterizedTypeConverter for parameterized type and add it to TypeConverterStorage using one of add*
methods:
ConversionContextManager.getContext().getTypeConverterStorage().add(new SomeCustomTypeConverter());
Adding custom mappings
By default converter matches donor and acceptor methods that have same names (in classic or fluent styles) but you can add custom mapping to CustomGetterSetterMappingStorage to point getter to certain setter using one of addGetterSetterMapping
methods (necessary converter will still be added if necessary):
public class DonorModel {
private String donorField1;
private Integer donorField2;
// getters and setters
}
public class AcceptorModel {
private String acceptorField;
// getters and setters
}
Method donorMethod = DonorModel.class.getMethod("getDonorField1");
Method acceptorMethod = AcceptorModel.class.getMethod("setAcceptorField", String.class);
ConversionContextManager.getContext().getCustomGetterSetterMappingStorage()
.addGetterSetterMapping(donorMethod, acceptorMethod);
License
This project is licensed under the MIT License, you can read the full text here.