Javaオブジェクトコピーの空でないプロパティ

 オブジェクトのコピーについては、org.springframework.beans.BeanUtilsクラスによって提供されるメソッドcopyProperties(Object source、Object target、String ... ignoreProperties)を参照してください。spring とApacheはどちらも、対応するツールクラスメソッドBeanUtils.copyPropertiesを提供します。

パッケージcom.mixislink.utils; 

import org.springframework.beans.BeanUtils; 
import org.springframework.beans.BeanWrapper; 
import org.springframework.beans.BeanWrapperImpl; 

import java.util.HashSet; 
import java.util.Set; 

/ * * 
 * @Author WuSong 
 * @Date 2019-02-25 
 * @Time 14:30:37 
 * / 
public class BeansUtil { 
    / ** 
     * @Description <p>オブジェクトで属性がnullの属性名を取得します</ P > 
     * 
     @paramsourceコピーするオブジェクト* @ return 
     * / 
    public static String [] getNullPropertyNames(Object source){ 
        final BeanWrapper src = new BeanWrapperImpl(source);
        java.beans.PropertyDescriptor [] pds = src.getPropertyDescriptors(); 

        Set <String> emptyNames = new HashSet <String>(); 
        for(java.beans.PropertyDescriptor pd:pds){ 
            Object srcValue = src.getPropertyValue(pd.getName()); 
            if(srcValue == null)
                emptyNames.add(pd.getName()); 
        } 
        String [] result = new String [emptyNames.size()]; 
        emptyNames.toArray(result);を返します。
    } 

    / ** 
     * @Description <p>拷贝非空対象ほど值</ P> 
     * @paramsource源対象
     * @ paramtarget目✓対象
     * / 
    public static void copyPropertiesIgnoreNull(Object source、Object target){
        BeanUtils.copyProperties(source、target、getNullPropertyNames(source)); 
    } 

}

 

https://blog.csdn.net/zml_2015/article/details/55192785を参照してください

おすすめ

転載: blog.csdn.net/qq_36961530/article/details/87917247