百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
B

bull

> 数据库
开源

BULL - Bean Utils 轻量库

196 stars0 点赞1 次浏览
访问官网GitHub

工具介绍

BULL - Bean Utils 轻量库

Bean Utils Light Library

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.

Start using

A tutorial that integrates the library can be found here.

All BULL modules are available on Maven Central:

  • Bean BOM

It contains all the modules available in the project

xml

    com.expediagroup.beans
    bull-bom
    x.y.z
    pom
    import
  • Bean Transformer

xml

    com.expediagroup.beans
    bull-bean-transformer
    x.y.z
  • Map Transformer

xml

    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

  • Suggestions

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:

xml

    ...
    
        
            ...
            
                org.apache.maven.plugins
                maven-compiler-plugin
                ${maven.compiler.plugin.version}
                
                    ${maven.compiler.source}
                    ${maven.compiler.target}
                    true
                    true
                
            
        
    
    ...

Maven build

Full build

bash
./mvnw clean install

or on Windows

bash
mvnw.cmd clean install

Skip test coverage and checkstyle check

bash
./mvnw clean install -P relaxed

or on Windows

bash
mvnw.cmd clean install -P relaxed

Check for dependencies update

mvn versions:display-dependency-updates -P check-for-updates

or on Windows

bash
mvnw.cmd versions:display-dependency-updates -P check-for-updates

Features:

  • support copy of immutable beans.
  • support copy of mutable beans.
  • support copy of hybrid beans (some fields private and some not).
  • support copy of Java Records.
  • support copy of Java beans without getter and setter methods.
  • support copy with Java primitive type.
  • support copy with Java Collection type. e.g. List => List
  • support copy with nested map fields. e.g. Map>
  • support copy with array containing primitive types. e.g. String[] => String[]
  • support copy with an array type. e.g. BeanA[] => BeanB[]
  • support copy with property name mapping. e.g. int id => int userId
  • support copy with recursion copy.
  • support validation through annotations.
  • support copy of beans with different field's name.
  • support lambda function field transformation.
  • support copy of java bean built through Builder.
  • easy usage, declarative way to define the property mapping (in case of different names), or simply adding the Lombok annotations.
  • allows setting the default value for all objects not existing in the source object.
  • allows skipping transformation for a given set of fields.
  • supports the retrieval of the value from getters if a field does not exist in the source object.
  • supports the automatic conversion of primitive types.

Feature samples

  • Bean Transformation
  • Bean Validation
  • Primitive Type conversion
  • Map Transformation
  • Supported Builder Pattern
  • How to use it in Kotlin

Bean transformation samples

Simple case:

…

And one line code as:

java
ToBean toBean = beanUtils.getTransformer().transform(fromBean, ToBean.class);

Different field names copy:

From class and To class with different field names:

…

And one line code as:

java
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 = 123

Mapping destination fields with correspondent fields contained inside one of the nested objects in the source object:

Assuming that the object FromSubBean is declared as follow:

java
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:

java
FieldMapping serialNumberMapping = new FieldMapping<>("subObject.serialNumber", "serialNumber");                                                             
FieldMapping creationDateMapping = new FieldMapping<>("subObject.creationDate", "creationDate");
                                                             
beanUtils.getTransformer()
         .withFieldMapping(serialNumberMapping, creationDateMapping)
         .transform(fromBean, ToBean.class);                                                               

Different field names defining constructor args:

…

And one line code as:

java
ToBean toBean = beanUtils.getTransformer().transform(fromBean, ToBean.class);

Different field names and types applying transformation through lambda function:

…
java
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:

java
FieldTransformer fieldTransformer = new FieldTransformer<>(List.of("identifier", "index"), BigInteger::negate);

Assign a default value in case of missing field in the source object:

Assign a default value in case of a missing field in the source object:

…

And one line code as:

java
ToBean toBean = beanUtils.getTransformer()
                    .setDefaultValueForMissingField(true).transform(fromBean, ToBean.class);

Disable the default value set for primitive types in case they are null:

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:

java
ToBean toBean = beanUtils.getTransformer()
                    .setDefaultValueForMissingPrimitiveField(false).transform(fromBean, ToBean.class);

in this case, the field id after the transformation will be null

Applying a transformation function in case of missing fields in the source object:

Assign a default value in case of a missing field in the source object:

…

And one line code as:

java
FieldTransformer notExistingFieldTransformer = new FieldTransformer<>("notExistingField", () -> "sampleVal");
ToBean toBean = beanUtils.getTransformer()
                    .withFieldTransformer(notExistingFieldTransformer)
                    .transform(fromBean, ToBean.class);

Apply a transformation function on a field contained in a nested object:

This example shows how a lambda transformation function can be applied to a nested object field.

Given:

java
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

java
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:

java
FieldTransformer nameTransformer = new FieldTransformer<>("nestedObject.name", StringUtils::capitalize);
ToBean toBean = beanUtils.getTransformer()
                    .withFieldTransformer(nameTransformer)
                    .transform(fromBean, ToBean.class);

Map a primitive type field in the source object into a nested object:

This example shows how to map a primitive field into a nested object into the destination one.

Given:

java
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

java
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:

java
ToBean toBean = beanUtils.getTransformer()
                    .withFieldMapping(new FieldMapping<>("x", "nestedObject.x"));

Apply a transformation function on all fields matching with the given one:

This

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

Javabeancopybeanutilsbullengineering-manager

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月18日
分类数据库
定价开源

> 相关工具

P
PostgreSQL
功能强大的开源关系型数据库
R
Redis
内存数据结构存储,常用作缓存与队列
M
MySQL
广泛使用的开源关系型数据库