All Downloads are FREE. Search and download functionalities are using the official Maven repository.

cn.enilu.flash.core.lang.Beans Maven / Gradle / Ivy

package cn.enilu.flash.core.lang;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeanWrapper;
import org.springframework.beans.PropertyAccessorFactory;

import java.beans.PropertyDescriptor;
import java.util.Arrays;

/**
 * Bean工具类
 *
 * @author enilu([email protected])
 *
 */
public final class Beans {
	private Beans() {
	}

	/**
	 * 为bean的指定属性设置值
	 * @param bean
	 * @param property
	 * @param value
	 */
	public static void set(Object bean, String property, Object value) {
		BeanWrapper wrapper = PropertyAccessorFactory
				.forBeanPropertyAccess(bean);
		wrapper.setPropertyValue(property, value);
	}

	/**
	 * 获取bean的执行属性名的属性值
	 * @param bean
	 * @param property
	 * @param 
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public static  R get(Object bean, String property) {
		BeanWrapper wrapper = PropertyAccessorFactory
				.forBeanPropertyAccess(bean);
		return (R) wrapper.getPropertyValue(property);
	}

	/**
	 * 浅copy 指定bean
	 * @param from
	 * @param klass
	 * @param excludedProperties
	 * @param 
	 * @param 
	 * @return
	 */
	public static  R copy(T from, Class klass,
			String... excludedProperties) {
		R to = BeanUtils.instantiate(klass);
		return copy(from, to, excludedProperties);
	}

	/**
	 * 浅copy, 不逐级复制.
	 * 
	 * @param from
	 * @param to
	 * @param excludedProperties
	 * @return
	 */
	public static  R copy(T from, R to, String... excludedProperties) {
		BeanWrapper wrapper = PropertyAccessorFactory
				.forBeanPropertyAccess(from);
		BeanWrapper toWrapper = PropertyAccessorFactory
				.forBeanPropertyAccess(to);
        Arrays.sort(excludedProperties);

		for (PropertyDescriptor descriptor : wrapper.getPropertyDescriptors()) {
			if (descriptor.getWriteMethod() == null
					|| descriptor.getReadMethod() == null) {
				continue;
			}

			String name = descriptor.getName();
			boolean found = Arrays.binarySearch(excludedProperties, name) >= 0;
			if (found) {
				continue;
			}

			toWrapper.setPropertyValue(name, wrapper.getPropertyValue(name));
		}
		return to;
	}

	public static  T extend(T target, R source, String... includedProperties) {
		return extend(target, source, false, includedProperties);
	}

    /**
     *
     * @param target
     * @param source
	 * @param ignoreNullProperty 是否忽略source里值为空的属性
     * @param includedProperties 未指定时extend所有属性
     * @param 
     * @param 
     * @return
     */
	public static  T extend(T target, R source, boolean ignoreNullProperty,
			String... includedProperties) {
		BeanWrapper targetWrapper = PropertyAccessorFactory
				.forBeanPropertyAccess(target);
		BeanWrapper sourceWrapper = PropertyAccessorFactory
				.forBeanPropertyAccess(source);

        Arrays.sort(includedProperties);

		for (PropertyDescriptor descriptor : sourceWrapper
				.getPropertyDescriptors()) {
			if (descriptor.getWriteMethod() == null
					|| descriptor.getReadMethod() == null) {
				continue;
			}

			String name = descriptor.getName();
			boolean found = includedProperties.length == 0
					|| Arrays.binarySearch(includedProperties, name) >= 0;
			if (!found) {
				continue;
			}

			if (ignoreNullProperty && sourceWrapper.getPropertyValue(name) == null) {
				continue;
			}
			
			if (!targetWrapper.isWritableProperty(name)) {
				continue;
			}
			
			targetWrapper.setPropertyValue(name,
					sourceWrapper.getPropertyValue(name));
		}
		return target;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy