BULL - Bean Utils 轻量库
BULL is a Java Bean to Java Bean transformer that recursively copies data from one object to another, it is generic, flexible, reusable, configurable, and incredibly fast. It's the only library able to transform Mutable, Immutable, and Mixed bean without any custom configuration.
A tutorial that integrates the library can be found here.
All BULL modules are available on Maven Central:
It contains all the modules available in the project
com.expediagroup.beans
bull-bom
x.y.z
pom
import
com.expediagroup.beans
bull-bean-transformer
x.y.z
Map Transformer
com.expediagroup.beans
bull-map-transformer
x.y.z
The project provides two different builds, one compatible with jdk 8 (or above),
one with jdk 11 and on with jdk 17 or above.
In case you need to integrate it in a:
jdk 8 please refer to CHANGELOG-JDK8
jdk 11 please refer to CHANGELOG-JDK11
jdk 15 CHANGELOG
Some jdk versions remove the Java Bean constructor's argument names from the compiled code and this may cause problems to the library.
On top of that, it's suggested to configure the maven-compiler-plugin, inside your project, as follow:
...
...
org.apache.maven.plugins
maven-compiler-plugin
${maven.compiler.plugin.version}
${maven.compiler.source}
${maven.compiler.target}
true
true
...
Full build
./mvnw clean installor on Windows
mvnw.cmd clean install./mvnw clean install -P relaxedor on Windows
mvnw.cmd clean install -P relaxedmvn versions:display-dependency-updates -P check-for-updatesor on Windows
mvnw.cmd versions:display-dependency-updates -P check-for-updatesList => ListMap>String[] => String[]BeanA[] => BeanB[]int id => int userId…And one line code as:
ToBean toBean = beanUtils.getTransformer().transform(fromBean, ToBean.class);From class and To class with different field names:
…And one line code as:
beanUtils.getTransformer().withFieldMapping(new FieldMapping<>("name", "differentName")).transform(fromBean, ToBean.class); it is also possible to map a field in the source class into multiple fields in the destination object.
Given the following source class:
public class SourceClass {
private final String name;
private final int id;
}the following destination class:
public class DestinationClass {
private final String name;
private final int id;
private final int index;
}and the following operations:
var sourceObj = new SourceClass("foo", 123);
var multipleFieldMapping = new FieldMapping<>("id", "index", "identifier");
var destObj = new BeanUtils().getBeanTransformer()
.withFieldMapping(multipleFieldMapping)
.transform(sourceObj, DestinationClass.class);
System.out.println("name = " + destObj.getName());
System.out.println("id = " + destObj.getId());
System.out.println("index = " + destObj.getIndex());the output will be:
name = foo
id = 123
index = 123Assuming that the object FromSubBean is declared as follow:
public class FromSubBean {
private String serialNumber;
private Date creationDate;
// getters and setters...
}and our source object and destination object are described as follow:
…the fields: serialNumber and creationDate needs to be retrieved from subObject, this can be done by defining the whole path to the end property:
FieldMapping serialNumberMapping = new FieldMapping<>("subObject.serialNumber", "serialNumber");
FieldMapping creationDateMapping = new FieldMapping<>("subObject.creationDate", "creationDate");
beanUtils.getTransformer()
.withFieldMapping(serialNumberMapping, creationDateMapping)
.transform(fromBean, ToBean.class); …And one line code as:
ToBean toBean = beanUtils.getTransformer().transform(fromBean, ToBean.class);…FieldTransformer fieldTransformer = new FieldTransformer<>("identifier", BigInteger::negate);
FieldTransformer localeTransformer = new FieldTransformer<>("locale", Locale::forLanguageTag);
beanUtils.getTransformer()
.withFieldMapping(new FieldMapping<>("id", "identifier"))
.withFieldTransformer(fieldTransformer).transform(fromBean, ToBean.class)
.withFieldTransformer(localeTransformer);It's also possible to apply the same transformation function on multiple fields. Taking as an example the above bean and assuming that we would negate both the id and the identifier, the transformer function has to be defined as follows:
FieldTransformer fieldTransformer = new FieldTransformer<>(List.of("identifier", "index"), BigInteger::negate);Assign a default value in case of a missing field in the source object:
…And one line code as:
ToBean toBean = beanUtils.getTransformer()
.setDefaultValueForMissingField(true).transform(fromBean, ToBean.class);BULL by default sets the default value for all primitive types fields in case their value is in the source object. Given the following Java Bean:
…in case the field id in the FromBean object is null, the value assigned the correspondent field in the ToBean object will be 0.
To disable this you can simply do:
ToBean toBean = beanUtils.getTransformer()
.setDefaultValueForMissingPrimitiveField(false).transform(fromBean, ToBean.class);in this case, the field id after the transformation will be null
Assign a default value in case of a missing field in the source object:
…And one line code as:
FieldTransformer notExistingFieldTransformer = new FieldTransformer<>("notExistingField", () -> "sampleVal");
ToBean toBean = beanUtils.getTransformer()
.withFieldTransformer(notExistingFieldTransformer)
.transform(fromBean, ToBean.class);This example shows how a lambda transformation function can be applied to a nested object field.
Given:
public class FromBean { public class ToBean {
private final String name; private final String name;
private final FromSubBean nestedObject; private final ToSubBean nestedObject;
// all args constructor // all args constructor
// getters... // getters...
} }and
public class ToSubBean {
private final String name;
private final long index;
}Assuming that the lambda transformation function should be applied only to field: name contained into the ToSubBean object, the transformation function has to be defined as
follow:
FieldTransformer nameTransformer = new FieldTransformer<>("nestedObject.name", StringUtils::capitalize);
ToBean toBean = beanUtils.getTransformer()
.withFieldTransformer(nameTransformer)
.transform(fromBean, ToBean.class);This example shows how to map a primitive field into a nested object into the destination one.
Given:
public class FromBean { public class ToBean {
private final String name; private final String name;
private final FromSubBean nestedObject; private final ToSubBean nestedObject;
private final int x;
// all args constructor // all args constructor
// getters... // getters...
} }and
public class ToSubBean {
private final int x;
// all args constructor
} // getters... Assuming that the value x should be mapped into the field: x contained into the ToSubBean object, the field mapping has to be defined as
follow:
ToBean toBean = beanUtils.getTransformer()
.withFieldMapping(new FieldMapping<>("x", "nestedObject.x"));This
暂无开放 Issues,或尚未同步最近议题。