All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.github.aileben.common.tools.utils.ObjectUtil Maven / Gradle / Ivy

package io.github.aileben.common.tools.utils;

import java.lang.reflect.Field;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

/**
 * 用于对Object进行解析并且转换成Map键值对的形式
 *
 * @author fypeng
 * @version 1.0
 */
public class ObjectUtil {

    private static final String JAVAP = "java.";
    private static final String JAVADATESTR = "java.util.Date";
    

    /**
     * 获取利用反射获取类里面的值和名称
     *
     * @param obj
     * @return
     * @throws IllegalAccessException
     * @throws SecurityException 
     * @throws NoSuchFieldException 
     */
    public static Map objectToMap(Object obj,String...args) throws IllegalAccessException, NoSuchFieldException, SecurityException {
    	 Map reMap = new HashMap<>();
         if (obj == null){
             return null;
         }
         List fields = new ArrayList<>();
         List childFields;
         List fieldsName = new ArrayList<>();
         Class tempClass = obj.getClass();
         while (tempClass != null) {//当父类为null的时候说明到达了最上层的父类(Object类).
             fields.addAll(Arrays.asList(tempClass.getDeclaredFields()));
             tempClass = tempClass.getSuperclass(); //得到父类,然后赋给自己
         }
         childFields = Arrays.asList(obj.getClass().getDeclaredFields());
         for (Field field : childFields) {
             fieldsName.add(field.getName());
         }
         
         List lists = Lists.newArrayList(args);
         for (Field field : fields) {
             if (fieldsName.contains(field.getName())) {
                 Field f = obj.getClass().getDeclaredField(field.getName());
                 f.setAccessible(true);
                 Object o = f.get(obj);
                 if(lists != null && !lists.isEmpty()) {
                	 if(lists.contains(field.getName())) {
                		 continue;
                	 }
                 }
                 reMap.put(field.getName(), o);
             } else {
                 Field f = obj.getClass().getSuperclass().getDeclaredField(field.getName());
                 f.setAccessible(true);
                 Object o = f.get(obj);
                 if(lists != null && !lists.isEmpty()) {
                	 if(lists.contains(field.getName())) {
                		 continue;
                	 }
                 }                         
                 reMap.put(field.getName(), o);
             }
         }
         return reMap;
    }

    /**
     * 利用递归调用将Object中的值全部进行获取
     *
     * @param timeFormatStr 格式化时间字符串默认2017-03-10 10:21
     * @param obj           对象
     * @param excludeFields 排除的属性
     * @return
     * @throws IllegalAccessException
     */
    public static Map objectToMapString(String timeFormatStr, Object obj, String... excludeFields) throws IllegalAccessException {
        Map map = Maps.newHashMap();
        if (excludeFields.length!=0){
            List list = Arrays.asList(excludeFields);
            objectTransfer(timeFormatStr, obj, map, list);
        }else{
            objectTransfer(timeFormatStr, obj, map,null);
        }
        return map;
    }


    /**
     * 递归调用函数
     *
     * @param obj           对象
     * @param map           map
     * @param excludeFields 对应参数
     * @return
     * @throws IllegalAccessException
     */
    private static Map objectTransfer(String timeFormatStr, Object obj, Map map, List excludeFields) throws IllegalAccessException {
        boolean isExclude=false;
        //默认字符串
        String formatStr = "YYYY-MM-dd HH:mm:ss";
        //设置格式化字符串
        if (timeFormatStr != null && !timeFormatStr.isEmpty()) {
            formatStr = timeFormatStr;
        }
        if (excludeFields!=null){
            isExclude=true;
        }
        Class clazz = obj.getClass();
        //获取值
        for (Field field : clazz.getDeclaredFields()) {
            String fieldName = clazz.getSimpleName() + "." + field.getName();
            //判断是不是需要跳过某个属性
            if (isExclude&&excludeFields.contains(fieldName)){
                continue;
            }
            //设置属性可以被访问
            field.setAccessible(true);
            Object value = field.get(obj);
            Class valueClass = value.getClass();
            if (valueClass.isPrimitive()) {
                map.put(fieldName, value.toString());
            } else if (valueClass.getName().contains(JAVAP)) {//判断是不是基本类型
                if (valueClass.getName().equals(JAVADATESTR)) {
                    //格式化Date类型
                    SimpleDateFormat sdf = new SimpleDateFormat(formatStr);
                    Date date = (Date) value;
                    String dataStr = sdf.format(date);
                    map.put(fieldName, dataStr);
                } else {
                    map.put(fieldName, value.toString());
                }
            } else {
                objectTransfer(timeFormatStr, value, map,excludeFields);
            }
        }
        return map;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy