org.springframework.cglib.beans.BeanCopier Java Examples

The following examples show how to use org.springframework.cglib.beans.BeanCopier. 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: BeanTest.java    From mica with GNU Lesser General Public License v3.0 6 votes vote down vote up
public static void test2() {
	BeanCopier beanCopier = BeanCopier.create(User.class, UserVO.class, true);
	User user = new User();
	user.setId(1);
	user.setName("如梦技术");
	user.setAge(18);

	UserVO userVO = new UserVO();
	// 此处 Converter 可使用 lambda 简化。
	beanCopier.copy(user, userVO, new Converter() {
		@Override
		public Object convert(Object o, Class aClass, Object o1) {
			return null;
		}
	});
	System.out.println(userVO);
}
 
Example #2
Source File: AutoModuleScope.java    From phoenix.webui.framework with Apache License 2.0 6 votes vote down vote up
@Override
public Object get(String name, ObjectFactory<?> objectFactory)
{
    Object object = objMap.get(name);
    if(object == null)
    {
        object = objectFactory.getObject();
        AutoModuleProxy proxy = new AutoModuleProxy(object, recordReportWriters, util);
        Object proxyObject = proxy.getProxy();
        
        BeanCopier beanCopier = BeanCopier.create(object.getClass(), proxyObject.getClass(), false);
        beanCopier.copy(object, proxyObject, null);
        
        putAware(proxyObject);

        object = proxyObject;
        objMap.put(name, object);
    }

    return object;
}
 
Example #3
Source File: UserCopy1Test.java    From mica-example with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) {
	// 设置 cglib 源码生成目录
	String sourcePath = "/Users/lcm/git/mica/mica-example/web-example/src/test/java";
	System.setProperty(DebuggingClassWriter.DEBUG_LOCATION_PROPERTY, sourcePath);

	// 1. 初始化 user,赋值
	User user = new User();
	user.setId(250);
	user.setName("如梦技术");
	user.setAge(30);
	user.setBirthday(LocalDateTime.now());

	// 2. 初始化 userVo
	UserVo userVo = new UserVo();
	// 3. 构造 BeanCopier,不是用类型转换
	BeanCopier copier = BeanCopier.create(User.class, UserVo.class, false);
	// 4. 拷贝对象,不是用类型转换,转换器可以使用 null
	copier.copy(user, userVo, null);

	// 5. 打印结果:UserVo(name=如梦技术, age=30, birthday=null)
	System.out.println(userVo);
}
 
Example #4
Source File: FastBeanUtil.java    From sds with Apache License 2.0 6 votes vote down vote up
/**
 * 浅拷贝
 * 对象内的属性只会复制引用,比如Map、List都是拷贝引用,并不会重新创建
 *
 * @param source
 * @param target
 */
public static void copy(Object source, Object target) {
    if(source == null || target == null) {
        return ;
    }

    ClassPair classPair = new ClassPair(source.getClass(), target.getClass());
    BeanCopier beanCopier = beanCopierMap.get(classPair);

    if(beanCopier == null) {
        BeanCopier newBeanCopier = BeanCopier.create(source.getClass(), target.getClass(), false);
        BeanCopier oldBeanCopier = beanCopierMap.putIfAbsent(classPair, newBeanCopier);

        beanCopier = oldBeanCopier != null ? oldBeanCopier : newBeanCopier;
    }

    beanCopier.copy(source, target, null);
}
 
Example #5
Source File: BeanConvertUtil.java    From MicroCommunity with Apache License 2.0 6 votes vote down vote up
/**
 * 对象A转为对象B
 * 这个也支持map转bean
 *
 * @param orgBean 原始对象
 * @param dstBean 目标对象类
 * @param <T1>    原始对象
 * @param <T2>    目标对象
 * @return 目标对象
 */
public static <T1, T2> T2 covertBean(T1 orgBean, T2 dstBean) {

    try {
        //BeanUtils.copyProperties(dstBean, orgBean);
        if (orgBean instanceof Map) {
            BeanMap beanMap = BeanMap.create(dstBean);
            //beanMap.putAll((Map)orgBean);
            objectFieldsPutMap(dstBean, beanMap, (Map) orgBean);
            return dstBean;
        }
        final BeanCopier beanCopier = BeanCopier.create(orgBean.getClass(), dstBean.getClass(), true);
        beanCopier.copy(orgBean, dstBean, new Java110Converter());
    } catch (Exception e) {
        e.printStackTrace();
        throw new RuntimeException("bean转换bean失败", e);
    }
    return dstBean;
}
 
Example #6
Source File: BeanUtils.java    From cola-cloud with MIT License 5 votes vote down vote up
/**
 * copy 对象属性到另一个对象,默认不使用Convert
 *
 * @param clazz 类名
 * @return {T}
 */
public static <T> T copy(Object src, Class<T> clazz) {
  BeanCopier copier = BeanCopier.create(src.getClass(), clazz, false);
  T to = newInstance(clazz);
  copier.copy(src, to, null);
  return to;
}
 
Example #7
Source File: BeanUtil.java    From javautils with Apache License 2.0 5 votes vote down vote up
/**
 * 对象拷贝复制
 * @param source
 * @param target
 */
public static void copy(Object source, Object target){
    if(source != null && target != null){
        BeanCopier copier = BeanCopier.create(source.getClass(), target.getClass(), false);
        copier.copy(source, target, null);
    }
}
 
Example #8
Source File: BeanUtils.java    From xmanager with Apache License 2.0 5 votes vote down vote up
/**
 * 拷贝对象
 * @param src 源对象
 * @param dist 需要赋值的对象
 */
public static void copy(Object src, Object dist) {
	BeanCopier copier = BeanCopier
			.create(src.getClass(), dist.getClass(), false);

	copier.copy(src, dist, null);
}
 
Example #9
Source File: BeanUtils.java    From xmanager with Apache License 2.0 5 votes vote down vote up
/**
 * copy 对象属性到另一个对象,默认不使用Convert
 * @param src
 * @param clazz 类名
 * @return T
 */
public static <T> T copy(Object src, Class<T> clazz) {
	BeanCopier copier = BeanCopier.create(src.getClass(), clazz, false);

	T to = newInstance(clazz);
	copier.copy(src, to, null);
	return to;
}
 
Example #10
Source File: BeanTest.java    From mica with GNU Lesser General Public License v3.0 5 votes vote down vote up
public static void test1() {
	BeanCopier beanCopier = BeanCopier.create(User.class, UserVO.class, false);
	User user = new User();
	user.setId(1);
	user.setName("如梦技术");
	user.setAge(18);

	UserVO userVO = new UserVO();
	beanCopier.copy(user, userVO, null);
	System.out.println(userVO);
}
 
Example #11
Source File: BeanCopierUtils.java    From blog-sample with Apache License 2.0 5 votes vote down vote up
private static BeanCopier getBeanCopier(Class<?> sourceClazz, Class<?> targetClazz) {
    String key = generatorKey(sourceClazz, targetClazz);
    BeanCopier copier;
    if(CACHE.containsKey(key)) {
        copier = CACHE.get(key);
    } else {
        copier = BeanCopier.create(sourceClazz, targetClazz, false);
        CACHE.put(key, copier);
    }
    return copier;
}
 
Example #12
Source File: UserCopy2Test.java    From mica-example with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) {
	// 设置 cglib 源码生成目录
	String sourcePath = "/Users/lcm/git/mica/mica-example/web-example/src/test/java";
	System.setProperty(DebuggingClassWriter.DEBUG_LOCATION_PROPERTY, sourcePath);

	// 1. 初始化 user,赋值
	User user = new User();
	user.setId(250);
	user.setName("如梦技术");
	user.setAge(30);
	user.setBirthday(LocalDateTime.now());

	// 2. 初始化 userVo
	UserVo userVo = new UserVo();
	// 3. 构造 BeanCopier,不是用类型转换
	BeanCopier copier = BeanCopier.create(User.class, UserVo.class, true);
	// 4. 拷贝对象,不是用类型转换,转换器可以使用 null
	copier.copy(user, userVo, new Converter() {
		@Override
		public Object convert(Object o, Class aClass, Object o1) {
			if (o == null) {
				return null;
			}
			return ConvertUtil.convert(o, aClass);
		}
	});

	// 5. 打印结果:UserVo(name=如梦技术, age=30, birthday=19-4-30 下午9:45)
	System.out.println(userVo);
}
 
Example #13
Source File: BeanCopyBenchmark.java    From mica-jmh with MIT License 5 votes vote down vote up
@Benchmark
public ToUser cglibBeanCopy() {
	ToUser toUser = BeanUtil.newInstance(ToUser.class);
	BeanCopier beanCopier = BeanCopier.create(FormUser.class, ToUser.class, false);
	beanCopier.copy(user, toUser, null);
	return toUser;
}
 
Example #14
Source File: BeanUtil.java    From BlogManagePlatform with Apache License 2.0 4 votes vote down vote up
private static BeanCopier getCopier(Object source, Object target) {
	return COPIER_CACHE.computeIfAbsent(StrUtil.concat(source.getClass().getName(), target.getClass().getName()), i -> BeanCopier.create(source
		.getClass(), target.getClass(), false));
}
 
Example #15
Source File: BeanCopierUtils.java    From blog-sample with Apache License 2.0 4 votes vote down vote up
public static void copyProperties(Object source, Object target) {
    BeanCopier copier = getBeanCopier(source.getClass(), target.getClass());
    copier.copy(source, target, null);
}
 
Example #16
Source File: Builder.java    From scaffold-cloud with MIT License 4 votes vote down vote up
public static <T, M> T build(M object, Class<T> clazz, Map map) {
    try {
        T t = clazz.newInstance();
        if (object == null) {
            return null;
        }
        // 使用cglib.BeanCopier完成对象属性的拷贝
        final BeanCopier copier = BeanCopier.create(object.getClass(), clazz, false);
        copier.copy(object, t, null);

        //判断是否需要进行转义
        if (map == null) {
            return t;
        }

        //进行转义
        Iterator<Map.Entry<String, Map>> it = map.entrySet().iterator();
        while (it.hasNext()) {
            Map.Entry<String, Map> entry = it.next();
            //需转义字段名
            String key = entry.getKey();
            //转义map
            Map valueMap = entry.getValue();
            //获取源转义字段数据
            Object destValue = ReflectUtil.getFieldValue(t, key);
            //设置值
            ReflectUtil.setFieldValue(t, key, valueMap.get(String.valueOf(destValue)));
        }

        return t;
    } catch (Exception e) {
        logger.warn(e.getMessage(), e);
        //do nothing
    }
    return null;
}
 
Example #17
Source File: CglibBeanCopierProperties.java    From java-tutorial with MIT License 3 votes vote down vote up
/**
 * Cglibʵ��
 *
 * @param source
 * @param target
 * @throws Exception
 */
@Override
public void copyProperties(Object source, Object target) throws Exception {

    BeanCopier beanCopier = BeanCopier.create(source.getClass(), target.getClass(), false);
    beanCopier.copy(source, target, null);
}
 
Example #18
Source File: CustomBeanUtils.java    From spring-boot-cookbook with Apache License 2.0 3 votes vote down vote up
/**
 * 将source的属性copy到目标类的实例中。
 * 避免Spring的BeanUtils.copyProperties会把null的字段值也copy过去的问题
 *
 * @param source
 * @param clazz
 * @param <T>
 * @return
 */
public static <T> T copyProperties(Object source, Class<T> clazz) {
    Assert.notNull(source, "Source must not be null");
    Assert.notNull(clazz, "clazz must not be null");
    T target = instantiate(clazz);
    BeanCopier copier = BeanCopier.create(source.getClass(), clazz, false);
    copier.copy(source, target, null);
    return target;
}
 
Example #19
Source File: BeanUtils.java    From cola-cloud with MIT License 2 votes vote down vote up
/**
 * 拷贝对象
 *
 * @param src 源对象
 * @param dist 需要赋值的对象
 */
public static void copy(Object src, Object dist) {
  BeanCopier copier = BeanCopier.create(src.getClass(), dist.getClass(), false);
  copier.copy(src, dist, null);
}