Package org.internna.iwebmvc.utils

Source Code of org.internna.iwebmvc.utils.BeanUtils

/*
* Copyright 2002-2007 the original author or authors.
*
* Licensed under the Apache license, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*      http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.internna.iwebmvc.utils;

import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Hibernate;
import org.springframework.beans.FatalBeanException;

/**
* Clones beans considering CGLIB proxies.
*
* @author Jose Noheda
* @since 1.0
*/
public final class BeanUtils {

    private static Log log = LogFactory.getLog(BeanUtils.class);

    /**
     * Default private constructor for a Utility class.
     */
    private BeanUtils() { }

    /**
     * Create a new bean and copy all properties from source to it.
     *
     * @param source any object
     */
    public static <T> T copyProperties(final T source) {
        return source != null ? copyProperties(source, null) : null;
    }

    /**
     * Create a new bean and copy all properties from source to it except those
     * listed.
     *
     * @param source Object to clone
     * @param ignoreProperties list of excluded properties
     * @return an object of the same type as the source
     */
    @SuppressWarnings("unchecked")
    public static <T> T copyProperties(final T source, final List<String> ignoreProperties) {
        Object target = null;
        try {
            if (Hibernate.isInitialized(source)) {
                target = ClassUtils.getActualClass(source).newInstance();
                copyProperties(source, target, ignoreProperties);
            }
        } catch (Exception ex) {
            if (log.isWarnEnabled()) log.warn("Could instantiate new target object for source[" + source + "]", ex);
        }
        return (T) target;
    }
   
    /**
     * Copy all properties from source to target.
     *
     * @param source
     * @param target
     */
    public static void copyProperties(final Object source, Object target) {
        copyProperties(source, target, null);
    }

    /**
     * Copy all properties from source to target excluding those listed.
     *
     * @param source
     * @param target
     * @param ignoreProperties
     */
    public static void copyProperties(final Object source, Object target, final List<String> ignoreProperties) {
        Assert.notNull(source, "Source must not be null");
        Assert.notNull(target, "Target must not be null");
        Class<?> clazz = target.getClass();
        PropertyDescriptor[] targetPds = org.springframework.beans.BeanUtils.getPropertyDescriptors(clazz);
        for (PropertyDescriptor targetPd : targetPds) {
            String name = targetPd.getName();
            Method writeMethod = targetPd.getWriteMethod();
            if ((ignoreProperties == null) || (!ignoreProperties.contains(name))) {
                if (Hibernate.isPropertyInitialized(source, name) & (writeMethod != null)) {
                    PropertyDescriptor sourcePd = org.springframework.beans.BeanUtils.getPropertyDescriptor(clazz, name);
                    try {
                        Method readMethod = sourcePd.getReadMethod();
                        Object value = readMethod.invoke(source);
                        if (Hibernate.isInitialized(value)) writeMethod.invoke(target, new Object[]{value});
                    } catch (Exception ex) {
                        throw new FatalBeanException("Could not copy property[" + name + "] from source to target", ex);
                    }
                }
            }
        }
    }

}
TOP

Related Classes of org.internna.iwebmvc.utils.BeanUtils

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.