JAVA通过反射获取和设置Bean属性(总结)

获取/设置Bean属性

package utils;

import lombok.extern.slf4j.Slf4j;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Slf4j
public class ObjectFiledUtil{
    
    /**
     * get field from T
     * @param obj T
     * @param fieldName field name
     * @return object
     */
    public static <T> Object getObjectField(T obj, String fieldName){
        if(null == obj) return null;
        Object object = null;
        try {
            //get the name of the property get method
            fieldName = fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1);
            Method method = obj.getClass().getMethod("get" + fieldName);
            object = method.invoke(obj);
        } catch (Exception e) {
            log.error("get object field from T error", e);
        }
        return object;
    }

    /**
     * get field from T list
     * @param list list
     * @param fieldName field name
     * @return object list
     */
    public static <T> List<Object> getObjectListField(List<T> list, String fieldName){
        if(null == list || list.size() == 0) return new ArrayList<>();
        List<Object> fieldList = new ArrayList<>();
        try {
            //get the name of the property get method
            fieldName = fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1);
            for(T t : list){
                Method m = t.getClass().getMethod("get" + fieldName);
                fieldList.add(m.invoke(t));
            }
        } catch (Exception e) {
            log.error("get object list field from T error", e);
        }
        return fieldList;
    }

    /**
     * set a single field value for T
     * @param obj T
     * @param fieldName field name
     * @param value field value
     */
    public static <T> void setObjectField(T obj, String fieldName, T value){
        if(null == obj) return;
        try {
            Field field = obj.getClass().getDeclaredField(fieldName); //get attribute
            if(null != field){
                field.setAccessible(true); //set accessibility
                field.set(obj, value);//set attribute value
            }
        } catch (Exception e) {
            log.error("set object field to T error", e);
        }
    }

    /**
     * set field value for T batch
     * @param obj T
     * @param map key:filed name, value:filed value
     */
    public static <T> void setObjectFieldBatch(T obj, Map<String, Object> map){
        if(null == obj) return;
        try {
            for(String key : map.keySet()){
                Field field = obj.getClass().getDeclaredField(key); //get attribute
                if(null != field){
                    field.setAccessible(true); //set accessibility
                    field.set(obj, map.get(key));//set attribute value
                }
            }
        } catch (Exception e) {
            log.error("set object field to T error", e);
        }
    }

    /**
     * set field value for T list batch
     * @param list list T
     * @param map key:filed name, value:filed value
     */
    public static <T> void setObjectFieldBatch(List<T> list, Map<String, Object> map){
        if(null == list || list.size() == 0) return;
        try {
            for(T t : list){
                setObjectFieldBatch(t, map);
            }
        } catch (Exception e) {
            log.error("set object field to T error", e);
        }
    }

    /**
     * List to List
     * @param list list
     * @return list
     */
    public static List<String> getStringListByObjectList(List<Object> list){
        if(null == list || list.size() == 0) return new ArrayList<>();
        return list.stream().map(String::valueOf).collect(Collectors.toList());
    }

}
 
  

                            
                        
                    
                    
                    

你可能感兴趣的:(项目demo,java,反射,stream)