copy Java Bean With Out Null - Android java.lang.reflect

Android examples for java.lang.reflect:Java Bean

Description

copy Java Bean With Out Null

Demo Code


//package com.java2s;
import java.lang.reflect.Field;

import java.lang.reflect.Method;

public class Main {

    public static void copyBeanWithOutNull(Object from, Object to) {
        Class<?> beanClass = from.getClass();
        Method[] methodList = beanClass.getDeclaredMethods();
        Field[] fields = beanClass.getFields();
        for (int i = 0; i < fields.length; i++) {
            Field field = fields[i];
            field.setAccessible(true);/*w  ww  .  ja v a 2s  .c  o  m*/
            try {
                Object value = field.get(from);
                if (value != null) {
                    field.set(to, value);
                }
            } catch (Exception e) {
            }
        }
    }
}

Related Tutorials