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

info.xiancloud.core.util.CloneUtil Maven / Gradle / Ivy

The newest version!
package info.xiancloud.core.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import java.util.Collection;

/**
 * Clone util.
 *
 * @author happyyangyuan
 */
public class CloneUtil {

    /**
     * clone a java bean. (We use fastjson.)
     *
     * @param from      the bean you want to clone from. This must be a standard bean object.
     * @param beanClass the  bean type
     * @param        the generic type.
     * @return the cloned object.
     */
    public static  T cloneBean(T from, Class beanClass) {
        if (from == null || from instanceof Collection || from.getClass().isArray()) {
            throw new IllegalArgumentException("Only java bean class is allowed here.");
        }
        JSONObject jsonObject = (JSONObject) JSON.toJSON(from);
        return jsonObject.toJavaObject(beanClass);
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy