Java源码示例:org.apache.commons.beanutils.BeanUtilsBean2
示例1
public Dictionary<String, ?> getProperties ()
{
try
{
final Dictionary<String, Object> result = new Hashtable<String, Object> ();
final Map<?, ?> properties = new BeanUtilsBean2 ().describe ( this.targetBean );
for ( final Map.Entry<?, ?> entry : properties.entrySet () )
{
if ( entry.getValue () != null )
{
result.put ( entry.getKey ().toString (), entry.getValue () );
}
}
return result;
}
catch ( final Exception e )
{
logger.warn ( "Failed to get dictionary", e );
return new Hashtable<String, Object> ( 1 );
}
}
示例2
/**
* 拷贝属性
*
* @param sources 源
* @param targetClass 目标 class
* @param <T> 目标类型
* @return 目标集合
*/
public static < T > List< T > copyPropertiesToList ( List< ? > sources , Class<T> targetClass) {
AssertUtils.isTrue( CollectionUtils.isEmpty( sources ), "sources is null" );
AssertUtils.isTrue( targetClass == null, "targetClass is null" );
List< T > targets = new ArrayList<>( sources.size() );
for ( Object source : sources ) {
T target;
try {
target = targetClass.newInstance();
BeanUtilsBean2.getInstance().copyProperties( target , source );
} catch ( InstantiationException | IllegalAccessException | InvocationTargetException e ) {
throw new RuntimeException( e.getMessage() , e );
}
targets.add( target );
}
return targets;
}
示例3
/**
* Copy properties.
*
* @param desc 不允许为空
* @param src 不允许为空
*/
@SuppressWarnings({ "unchecked", "rawtypes" })
public static void copyProperties(Object desc, Object src)
{
BeanUtilsBean2 beanUtilsBean2 = new BeanUtilsBean2();
try
{
if (desc instanceof Map)
{
Map<String, Object> map = beanToMap(src);
((Map) desc).putAll(map);
} else
{
beanUtilsBean2.copyProperties(desc, src);
}
} catch (Exception e)
{
log.error(e.getMessage(), e);
}
}
示例4
public static void beanUtils() throws InvocationTargetException, IllegalAccessException, NoSuchMethodException, InstantiationException {
BeanUtilsBean beanUtilsBean = new BeanUtilsBean2();
beanUtilsBean.getConvertUtils().register(false, false, 0);//错误不抛出异常、不使用Null做默认值,数组的默认大小为0
User user = new User();
user.setName("test");
TestUser testUser = new TestUser();
beanUtilsBean.copyProperties(testUser, user);
System.out.println(testUser);
}
示例5
@PrePersist
public void onCreate(Object object) {
final String ID = "id";
final String CREATED_AT = "createdAt";
final String LAST_MODIFIED_AT = "lastModifiedAt";
BeanUtilsBean beanUtilsBean = BeanUtilsBean2.getInstance();
try {
if (Objects.equals(beanUtilsBean.getProperty(object, ID), CommonsConstant.ZERO)) {
beanUtilsBean.setProperty(object, CREATED_AT, System.currentTimeMillis());
beanUtilsBean.setProperty(object, LAST_MODIFIED_AT, System.currentTimeMillis());
}
} catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException ignore) {}
}
示例6
@PrePersist
public void onCreate(Object object) {
final String ID = "id";
final String VALID_FLAG = "validFlag";
BeanUtilsBean beanUtilsBean = BeanUtilsBean2.getInstance();
try {
if (Objects.equals(beanUtilsBean.getProperty(object, ID), CommonsConstant.ZERO)) {
beanUtilsBean.setProperty(object, VALID_FLAG, ValidFlag.VALID);
}
} catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException ignore) {}
}
示例7
@Override
protected ConvertUtilsBean initialValue() {
ConvertUtilsBean ret = BeanUtilsBean2.getInstance().getConvertUtils();
ret.deregister();
ret.register(false, true, 1);
return ret;
}
示例8
private GluuInumMap getMarkInumMapEntryAsRemoved(GluuInumMap currentInumMap, String date) {
GluuInumMap clonedInumMap;
try {
clonedInumMap = (GluuInumMap) BeanUtilsBean2.getInstance().cloneBean(currentInumMap);
} catch (Exception ex) {
log.error("Failed to prepare GluuInumMap for removal", ex);
return null;
}
String suffix = "-" + date;
String[] primaryKeyValues = ArrayHelper.arrayClone(clonedInumMap.getPrimaryKeyValues());
String[] secondaryKeyValues = ArrayHelper.arrayClone(clonedInumMap.getSecondaryKeyValues());
String[] tertiaryKeyValues = ArrayHelper.arrayClone(clonedInumMap.getTertiaryKeyValues());
if (ArrayHelper.isNotEmpty(primaryKeyValues)) {
markInumMapEntryKeyValuesAsRemoved(primaryKeyValues, suffix);
}
if (ArrayHelper.isNotEmpty(secondaryKeyValues)) {
markInumMapEntryKeyValuesAsRemoved(secondaryKeyValues, suffix);
}
if (ArrayHelper.isNotEmpty(tertiaryKeyValues)) {
markInumMapEntryKeyValuesAsRemoved(tertiaryKeyValues, suffix);
}
clonedInumMap.setPrimaryKeyValues(primaryKeyValues);
clonedInumMap.setSecondaryKeyValues(secondaryKeyValues);
clonedInumMap.setTertiaryKeyValues(tertiaryKeyValues);
clonedInumMap.setStatus(GluuStatus.INACTIVE);
return clonedInumMap;
}
示例9
public void update ( final Map<String, String> parameters ) throws Exception
{
new BeanUtilsBean2 ().populate ( this.targetBean, parameters );
}