com.aosa.main.utils.tools.AOSACopyUtil.java Source code

Java tutorial

Introduction

Here is the source code for com.aosa.main.utils.tools.AOSACopyUtil.java

Source

/**
 * CopyUtil.java ,By AOSA_TECH MAMPlus at 2011-8-11 ?06:53:44
 * Copyright (c) 2011, AOSA_TECH, Ltd. All right reserved.
 * AOSA_TECH PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 * @Project MAMPlus
 * @version V1.0.0
 */
package com.aosa.main.utils.tools;

import java.beans.PropertyDescriptor;
import java.util.Collection;

import org.apache.commons.beanutils.PropertyUtils;

import com.aosa.main.utils.aosaexception.AOSARuntimeException;

/**
 * TitleCopyUtil      <br>
 * Description<code>VO  PO ?</code>      <br>
 * @author mutou at 2011-8-30 t?11:34:05   <br>
 * @version V1.0.0      <br>
 * @see
 */
public class AOSACopyUtil {
    /**
     * Description<code>Copy properties of orig to dest Exception the Entity and Collection Type</code>      <br>
     * By mutou at 2011-8-30 ?11:35:29   <br>
     * Object      <br>
     * @param dest
     * @param orig
     * @return the dest bean
     * @throws
     */
    @SuppressWarnings("rawtypes")
    public static Object copyProperties(Object dest, Object orig) {
        if (dest == null || orig == null) {
            return dest;
        }
        PropertyDescriptor[] destDesc = PropertyUtils.getPropertyDescriptors(dest);
        try {
            for (int i = 0; i < destDesc.length; i++) {
                Class destType = destDesc[i].getPropertyType();
                Class origType = PropertyUtils.getPropertyType(orig, destDesc[i].getName());
                if (destType != null && destType.equals(origType) && !destType.equals(Class.class)) {
                    if (!Collection.class.isAssignableFrom(origType)) {
                        try {
                            Object value = PropertyUtils.getProperty(orig, destDesc[i].getName());
                            PropertyUtils.setProperty(dest, destDesc[i].getName(), value);
                        } catch (Exception ex) {
                        }
                    }
                }
            }
            return dest;
        } catch (Exception ex) {
            throw new AOSARuntimeException(ex);
        }
    }

    /**
     * Description<code>Copy properties of orig to dest Exception the Entity and Collection Type</code>      <br>
     * By mutou at 2011-8-30 ?11:36:11   <br>
     * Object      <br>
     * @param dest
     * @param orig
     * @param ignores
     * @return the dest bean
     * @throws
     */
    @SuppressWarnings("rawtypes")
    public static Object copyProperties(Object dest, Object orig, String[] ignores) {
        if (dest == null || orig == null) {
            return dest;
        }

        PropertyDescriptor[] destDesc = PropertyUtils.getPropertyDescriptors(dest);
        try {
            for (int i = 0; i < destDesc.length; i++) {
                if (contains(ignores, destDesc[i].getName())) {
                    continue;
                }
                Class destType = destDesc[i].getPropertyType();
                Class origType = PropertyUtils.getPropertyType(orig, destDesc[i].getName());
                if (destType != null && destType.equals(origType) && !destType.equals(Class.class)) {
                    if (!Collection.class.isAssignableFrom(origType)) {
                        Object value = PropertyUtils.getProperty(orig, destDesc[i].getName());
                        PropertyUtils.setProperty(dest, destDesc[i].getName(), value);
                    }
                }
            }
            return dest;
        } catch (Exception ex) {
            throw new AOSARuntimeException(ex);
        }
    }

    /**
     * @param ignores
     * @param name
     * @return ignored({@code boolean})
     */
    static boolean contains(String[] ignores, String name) {
        boolean ignored = false;
        for (int j = 0; ignores != null && j < ignores.length; j++) {
            if (ignores[j].equals(name)) {
                ignored = true;
                break;
            }
        }
        return ignored;
    }
}