Java Code Examples for org.springframework.beans.BeanUtils

Following code examples demonstrate how to use org.springframework.beans.BeanUtils from spring-framework. These examples are extracted from various highly rated open source projects. You can directly use these code snippets or view their entire linked source code. These snippets are extracted to provide contextual information about how to use this class in the real world. These samples also let you understand some good practices on how to use org.springframework.beans.BeanUtils and various code implementation of this class.
Example 1
Project : spring-framework Source File : SettleServiceImpl.java View Source Code on GitHub

	public QueryResult<SettleView> search(SettleQuery query) throws Exception {
		QueryResult<SettleView> result = new QueryResult<>();
		List<Settle> settle = settleMapper.findDatebaseList();
		for (Settle settle2 : settle) {
			SettleView settleView = new SettleView();
			BeanUtils.copyProperties(settle2, settleView);
			result.addData(settleView);
		}
		result.setDraw(query.getPage().getDraw());
		result.setRecordsFiltered(query.getPage().getStart());
		result.setRecordsTotal(settle.size());
		return result;
	}
			
Example 2
Project : spring-framework Source File : CloneUtileUse.java View Source Code on GitHub

	public static void main(String[] args) {
		ConcretePrototypeD concretePrototypeD = new ConcretePrototypeD();
		concretePrototypeD.setName("this");

		ConcretePrototypeD newPrototype = new ConcretePrototypeD();
		BeanUtils.copyProperties(concretePrototypeD, newPrototype);

		System.out.println(concretePrototypeD);
		System.out.println(newPrototype);
	}
			
Example 3
Project : spring-framework Source File : TestBeanUtils.java View Source Code on GitHub

    public static void main(String[] args) throws InvocationTargetException, IllegalAccessException {
        TestBeanUtilsObject testBeanUtilsObject1_a = new TestBeanUtilsObject();
        testBeanUtilsObject1_a.setName("aaa");
        TestBeanUtilsObject testBeanUtilsObject1_b = new TestBeanUtilsObject();
        org.springframework.beans.BeanUtils.copyProperties(testBeanUtilsObject1_a, testBeanUtilsObject1_b);
        System.out.println(testBeanUtilsObject1_b);

        TestBeanUtilsObject testBeanUtilsObject2_a = new TestBeanUtilsObject();
        testBeanUtilsObject2_a.setName("bbb");
        TestBeanUtilsObject testBeanUtilsObject2_b = new TestBeanUtilsObject();
        org.apache.commons.beanutils.BeanUtils.copyProperties(testBeanUtilsObject2_b,  testBeanUtilsObject2_a);
        System.out.println(testBeanUtilsObject2_b);
    }
			
Example 4
Project : spring-framework Source File : Beans.java View Source Code on GitHub

    public static void copyProperties(final Object source, final Object target) {
        BeanUtils.copyProperties(source, target);
        PropertyDescriptor[] targetPds = getPropertyDescriptors(target.getClass());
        for (PropertyDescriptor targetPd : targetPds) {
            Method writeMethod = targetPd.getWriteMethod();
            if (writeMethod != null) {
                PropertyDescriptor sourcePd = getPropertyDescriptor(source.getClass(), targetPd.getName());
                if (sourcePd != null) {
                    Method readMethod = sourcePd.getReadMethod();
                    if (readMethod != null) {
                        boolean writeOptional = Optional.class.equals(writeMethod.getParameterTypes()[0]);
                        boolean readOptional = Optional.class.equals(readMethod.getReturnType());
                        if (writeOptional != readOptional) {
                            try {
                                if (!Modifier.isPublic(readMethod.getDeclaringClass().getModifiers())) {
                                    readMethod.setAccessible(true);
                                }
                                Object value = readMethod.invoke(source);
                                if (!Modifier.isPublic(writeMethod.getDeclaringClass().getModifiers())) {
                                    writeMethod.setAccessible(true);
                                }
                                if (readOptional && !writeOptional) {
                                    Optional optValue = (Optional) value;
                                    if (optValue == null || !optValue.isPresent()) {
                                        writeMethod.invoke(target, new Object[]{null});
                                    } else {
                                        writeMethod.invoke(target, optValue.get());
                                    }
                                } else if (!readOptional && writeOptional) {
                                    writeMethod.invoke(target, Optional.ofNullable(value));
                                }
                            } catch (Throwable ex) {
                                throw new FatalBeanException(
                                        "Could not copy property '" + targetPd.getName() + "' from source to target", ex);
                            }
                        }
                    }
                }
            }
        }
    }
			
Example 5
Project : spring-framework Source File : InputDTOConverter.java View Source Code on GitHub

    public O convert() {
        O output;
        try {
            output = outputClass.newInstance();
        } catch (InstantiationException | IllegalAccessException e) {
            throw new RuntimeCastException(e);
        }
        BeanUtils.copyProperties(this, output);
        return output;
    }
			
Example 6
Project : spring-framework Source File : VOUtils.java View Source Code on GitHub

    public static<T> List<VO> toVOList(List<T> list){
        List<VO> vos= Lists.newArrayList();
        if (list==null||list.isEmpty()){
            return vos;
        }
        VO vo=null;
        JSONObject object=new JSONObject();
        for(T bean:list){
            vo=new VO();
            org.springframework.beans.BeanUtils.copyProperties(bean, vo);
            vos.add(vo);
        }
        return  vos;
    }
			
Example 7
Project : spring-framework Source File : BeanCopy.java View Source Code on GitHub

	public static void copyProperties(SchoolEntity source, SchoolData target){
		
		org.springframework.beans.BeanUtils.copyProperties(source, target);
		ArrayList<SchoolDetailData> details = new ArrayList<SchoolDetailData>();
		for (SchoolDetailEntity entity : source.getListOfSchoolDetail()){
			SchoolDetailData data = new SchoolDetailData();
			org.springframework.beans.BeanUtils.copyProperties(entity, data);
			details.add(data);
		}
		target.setListOfSchoolDetail(details);
		
	}
			
Example 8
Project : spring-framework Source File : BeanCopy.java View Source Code on GitHub

	public static void copyProperties(StudentProfileData source, StudentProfileResponse target) {

		org.springframework.beans.BeanUtils.copyProperties(source, target);

		ArrayList<FavSchoolResponse> favSchoolResponse = new ArrayList<FavSchoolResponse>();
		for (FavSchoolData data : source.getListOfFavSchool()) {
			FavSchoolResponse res = new FavSchoolResponse();
			org.springframework.beans.BeanUtils.copyProperties(data, res);
			SchoolResponse schoolRes = new SchoolResponse();
			org.springframework.beans.BeanUtils.copyProperties(data.getSchool(), schoolRes);
			res.setSchool(schoolRes);
			favSchoolResponse.add(res);

		}

		target.setListOfFavSchool(favSchoolResponse);

	}
			
Example 9
Project : spring-framework Source File : BeanCopy.java View Source Code on GitHub

	public static void copyProperties(StudentEntity source, StudentProfileData target) {

		org.springframework.beans.BeanUtils.copyProperties(source, target);

		ArrayList<FavSchoolData> schoolData = new ArrayList<FavSchoolData>();
		for (FavSchoolEntity entity : source.getListOfFavSchool()) {

			SchoolData data = new SchoolData();
			org.springframework.beans.BeanUtils.copyProperties(entity.getSchool(), data);

			FavSchoolData favData = new FavSchoolData();
			org.springframework.beans.BeanUtils.copyProperties(entity, favData);

			favData.setSchool(data);
			schoolData.add(favData);

		}

		target.setListOfFavSchool(schoolData);

	}
			
Example 10
Project : spring-framework Source File : Test1.java View Source Code on GitHub

	public void beanUtis() throws Exception {
		long s = System.currentTimeMillis();
		for (int i = 0; i < 100000; i++) {
			TestBeanVO t1 = getTestBean();
			TestBeanVO t2 = new TestBeanVO();
			BeanUtils.copyProperties(t2, t1);
		}
		System.out.println(System.currentTimeMillis() - s);
	}
			
Example 11
Project : spring-framework Source File : Test1.java View Source Code on GitHub

	public void springUtils() {
		long s = System.currentTimeMillis();
		for (int i = 0; i < 100000; i++) {
			TestBeanVO t1 = getTestBean();
			TestBeanVO t2 = new TestBeanVO();
			org.springframework.beans.BeanUtils.copyProperties(t1, t2);
		}
		System.out.println(System.currentTimeMillis() - s);
	}
			
Example 12
Project : spring-framework Source File : BeanUtilsUnitTest.java View Source Code on GitHub

    public void test() {
        VatSaleTransactionDto dto = new VatSaleTransactionDto();
        dto.setBusinessType("2");
        dto.setCreateTime(new Date());
        dto.setTicket(new RaffleTicket());
        dto.setVatRate(new BigDecimal("0.1"));

        VatSaleTransaction dest = new VatSaleTransaction();
        BeanUtils.copyProperties(dto, dest);

        assertEquals(dto.getBusinessType(), dest.getBusinessType());
        assertEquals(dto.getCreateTime().getTime(), dest.getCreateTime().getTime());
        assertEquals(dto.getVatRate().doubleValue(), dest.getVatRate().doubleValue(), 0);
        assertEquals(dto.getTicket(), dest.getTicket());
        assertEquals(0, dest.getVatTotalAmount().doubleValue(), 0);

    }
			
Example 13
Project : spring-framework Source File : BeanUtilsCustom.java View Source Code on GitHub

    public static List<String> getNullPropertiesList(Object object){
        Class<?> clazz = object.getClass();
        PropertyDescriptor[] descriptors = org.springframework.beans.BeanUtils.getPropertyDescriptors(clazz);
        return Arrays.stream(descriptors).map(d -> {
            try {
                if (d.getReadMethod().invoke(object) != null)
                    return null;
            } catch (Exception ignored) {

            }
            return d.getName();
        }).filter(StringUtils::hasText).collect(Collectors.toList());
    }
			
Example 14
Project : spring-framework Source File : BeanUtilsCustom.java View Source Code on GitHub

    public static Object getProperty(Object object, String key, Object... args){
        Class<?> objectClass = object.getClass();
        PropertyDescriptor descriptor = BeanUtils.getPropertyDescriptor(objectClass, key);
        if (descriptor == null){
            throw new ServiceException(objectClass + " 没有 " + key + " 字段");
        }
        Method readMethod = descriptor.getReadMethod();
        if (readMethod == null){
            throw new ServiceException(objectClass + " 的 " + key + " 没有 get 方法");
        }
        Object value;
        try {

            value = readMethod.invoke(object, args);
        } catch (Exception e) {
            throw new ServiceException(e);
        }
        return value;
    }
			
Example 15
Project : spring-framework Source File : BeanUtilsCustom.java View Source Code on GitHub

    public static List<String> getNotNullPropertiesList(Object object){
        Class<?> clazz = object.getClass();
        PropertyDescriptor[] descriptors = org.springframework.beans.BeanUtils.getPropertyDescriptors(clazz);
        return Arrays.stream(descriptors).map(d -> {
            Method readMethod = d.getReadMethod();
            Object value = null;
            try {
                value = readMethod.invoke(object);
            } catch (Exception ignored) {

            }

            if (value != null)
                return d.getName();
            return null;
        }).filter(s -> !StringUtils.isEmpty(s) && !s.equals("class")).collect(Collectors.toList());
    }
			
Example 16
Project : spring-framework Source File : BeanUtilsCustom.java View Source Code on GitHub

    public static void setProperty(Object object, String key, Object... args){
        Class<?> objectClass = object.getClass();
        PropertyDescriptor descriptor = BeanUtils.getPropertyDescriptor(objectClass, key);
        if (descriptor == null){
            throw new ServiceException(objectClass + " 没有 " + key + " 字段");
        }
        Method writeMethod = descriptor.getWriteMethod();
        if (writeMethod == null){
            throw new ServiceException(objectClass + " 的 " + key + " 没有 set 方法");
        }
        try {
            writeMethod.invoke(object, args);
        } catch (Exception e) {
            throw new ServiceException(e);
        }
    }