org.apache.commons.beanutils.BeanUtilsBean2 Java Examples

The following examples show how to use org.apache.commons.beanutils.BeanUtilsBean2. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: BeanConfigurationFactory.java    From neoscada with Eclipse Public License 1.0 6 votes vote down vote up
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 );
    }
}
 
Example #2
Source File: BeanProUtils.java    From spring-boot-start-current with Apache License 2.0 6 votes vote down vote up
/**
 * 拷贝属性
 *
 * @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;
}
 
Example #3
Source File: BeanUtils.java    From talent-aio with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * 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);
	}
}
 
Example #4
Source File: CommonsExample.java    From pragmatic-java-engineer with GNU General Public License v3.0 5 votes vote down vote up
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);
}
 
Example #5
Source File: PersistentListener.java    From spring-microservice-boilerplate with MIT License 5 votes vote down vote up
@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) {}
}
 
Example #6
Source File: ValidFlagListener.java    From spring-microservice-boilerplate with MIT License 5 votes vote down vote up
@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) {}
}
 
Example #7
Source File: ConversionUtils.java    From metron with Apache License 2.0 5 votes vote down vote up
@Override
protected ConvertUtilsBean initialValue() {
  ConvertUtilsBean ret = BeanUtilsBean2.getInstance().getConvertUtils();
  ret.deregister();
  ret.register(false, true, 1);
  return ret;
}
 
Example #8
Source File: CacheRefreshTimer.java    From oxTrust with MIT License 5 votes vote down vote up
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;
}
 
Example #9
Source File: BeanConfigurationFactory.java    From neoscada with Eclipse Public License 1.0 4 votes vote down vote up
public void update ( final Map<String, String> parameters ) throws Exception
{
    new BeanUtilsBean2 ().populate ( this.targetBean, parameters );
}