集册 Java实例教程 对src类和所有超类定义的所有字段执行浅表副本。

对src类和所有超类定义的所有字段执行浅表副本。

欢马劈雪     最近更新时间:2020-01-02 10:19:05

384
对src类和所有超类定义的所有字段执行浅表副本。

/*

 * Copyright (c) 2012 Data Harmonisation Panel

 * 

 * All rights reserved. This program and the accompanying materials are made

 * available under the terms of the GNU Lesser General Public License as

 * published by the Free Software Foundation, either version 3 of the License,

 * or (at your option) any later version.

 * 

 * You should have received a copy of the GNU Lesser General Public License

 * along with this distribution. If not, see <http://www.gnu.org/licenses/>.

 * 

 * Contributors:

 *     HUMBOLDT EU Integrated Project #030962

 *     Data Harmonisation Panel <http://www.dhpanel.eu>

 */

//package com.nowjava;


import java.lang.reflect.Field;// from 时 代      J a v a   公   众 号 - nowjava.com


import java.lang.reflect.Modifier;


public class Main {

    /**

     * Performs a shallow copy of all fields defined by the class of src and all

     * superclasses.

     * 

     * @param <T> the type of the source and destination object

     * @param src the source object

     * @param dst the destination object

     * @throws IllegalArgumentException if a field is unaccessible

     * @throws IllegalAccessException if a field is not accessible

     */

    public static <T> void shallowEnforceDeepProperties(T src, T dst)

            throws IllegalArgumentException, IllegalAccessException {

        Class<?> cls = src.getClass();

        shallowEnforceDeepProperties(cls, src, dst);

    }


    private static <T> void shallowEnforceDeepProperties(Class<?> cls,

            T src, T dst) throws IllegalArgumentException,
            /*
            时代Java - nowjava.com 提 供
            */

            IllegalAccessException {

        if (cls == Object.class || cls == null) {

            return;

        }


        Field[] fields = cls.getDeclaredFields();

        for (Field f : fields) {

            if (Modifier.isStatic(f.getModifiers())

                    || Modifier.isFinal(f.getModifiers())) {

                continue;

            }


            
展开阅读全文