Provide some processing APIs for Java objects, such as object copying(include bean copy,beans to Maps, lists to lists, strings to dates...), object cloning, getting or setting the values of Javabeans, etc
Provide some processing APIs for Java objects, such as object copying(include bean copy,beans to Maps, lists to lists, strings to dates...), object cloning, getting or setting the values of Javabeans, etc
//from bean to beanDemoUseruser = ...;
DemoConsumerconsumer = BeancpUtil.copy(user, DemoConsumer.class);
//copy bean to exist beanDemoUseruser = ...;
DemoConsumerconsumer = ...;
BeancpUtil.copy(user, consumer);
//basic type conversionBigDecimalnum = BeancpUtil.copy("123.4", BigDecimal.class);
//copy list to listList<DemoUser> userList = ...;
List<DemoConsumer> consumerList = BeancpUtil.copy(userList, BeancpUtil.type(List.class,DemoConsumer.class));
//copy bean to mapDemoUseruser = ...;
Map<String,Object> map = BeancpUtil.copy(user, Map.class);
...
Features:
Support mutual conversion between Javabeans and Maps
Support mutual conversion of List, array, and Set
Support conversion of basic types (number, date, string, enum, byte [], etc.)
Powerful performance, generated using bytecode
By default, it supports the conversion of public attributes, but it can also support the conversion of protected and private attributes through feature parameters
Provide APIs and annotations to support custom type conversions
Support clone for any Object
Support get or set properties for JavaBeans
Examples
Feature
//If the original value is null,will not set valueDemoUseruser = ...;
DemoConsumerconsumer = BeancpUtil.copy(user, DemoConsumer.class,BeancpFeature.SETVALUE_WHENNOTNULL);
Feature
description
SETVALUE_WHENNOTNULL
If the original value is null,will not set value
ALLWAYS_NEW
Copy a new object instead of the source object when assigning values
SETVALUE_TYPEEQUALS
Only when the type is consistent will value be set without type conversion
BEAN2MAP_UNDERLINE
When converting a Javabean to a map, the key of the map is in lowercase underlined form(userName->user_name)
BEAN2MAP_UNDERLINE_UPPER
When converting a Javabean to a map, the key of the map is in upper underlined form(userName->USER_NAME)
ACCESS_PROTECTED
Including protected attributes and default permission attributes
ACCESS_PRIVATE
Including protected default and private permission attributes
THROW_EXCEPTION
Throw an exception when encountering an exception
It can also be configured through global features with configAdd and configRemove
//The 'id' property of the DemoConsumer class is not assigned a valueDemoUseruser = ...;
DemoConsumerconsumer = BeancpUtil.copy(user, DemoConsumer.class,BeancpUtil.newContext().disallowKey(DemoConsumer.class, "id"));
api
description
disallowKey
Disallow the assignment of certain attributes in a class
allowOnly
Only the assignment of certain attributes in the class
It can be placed on the parameters of methods, fields, and constructors, and set one or more other names. It can also be placed on a non get set method, and the beancp framework will automatically parse it as a get set method
clone
//clone object //If the object is cloneable, the clone method will be called, and if not, it will be copied by property.DemoUseruser = ...
DemoUseruser2 = BeancpUtil.clone(user);
get and set property
//Obtain Javabean property valueStringage=BeancpUtil.getProperty(user,"age",String.class);
StringuserName = BeancpUtil.getProperty(user,"userName",String.class);
//By default, the underline format is also supported.The same goes for setPropertyStringuserName1 = BeancpUtil.getProperty(user,"user_name",String.class);
//Setting Property Value for JavabeansBeancpUtil.setProperty(user, "id", "a");
BeancpUtil.setProperty(user, "live", true);
Provide some processing APIs for Java objects, such as object copying(include bean copy,beans to Maps, lists to lists, strings to dates...), object cloning, getting or setting the values of Javabeans, etc