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

com.godmonth.util.BeanCopyUtils Maven / Gradle / Ivy

package com.godmonth.util;

import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.lang3.exception.ContextedRuntimeException;

/**
 * 
 * @author shenyue
 * 
 */
public class BeanCopyUtils {
	private BeanCopyUtils() {
	}

	public static void copyProperties(Object from, Object to, String... propertyNames) {
		for (String propertyName : propertyNames) {
			try {
				PropertyUtils.setProperty(to, propertyName, PropertyUtils.getProperty(from, propertyName));
			} catch (Exception e) {
				throw new ContextedRuntimeException(e);
			}
		}
	}

	public static  T copyProperties(T from, String... propertyNames) {
		try {
			@SuppressWarnings("unchecked")
			T to = (T) from.getClass().newInstance();
			copyProperties(from, to, propertyNames);
			return to;
		} catch (Exception e) {
			throw new ContextedRuntimeException(e);
		}
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy