公共的工具类

package com.yxb.base.util;
import com.alibaba.fastjson.JSON;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import javax.servlet.http.HttpServletRequest;

public class CommonUtil {
    public CommonUtil() {
    }

    public static String getIp(HttpServletRequest request) {
        String ip = request.getHeader("X-Forwarded-For");
        if (StringUtil.isNotEmpty(ip) && !"unKnown".equalsIgnoreCase(ip)) {
            int index = ip.indexOf(",");
            return index != -1 ? ip.substring(0, index) : ip;
        } else {
            ip = request.getHeader("X-Real-IP");
            return StringUtil.isNotEmpty(ip) && !"unKnown".equalsIgnoreCase(ip) ? ip : request.getRemoteAddr();
        }
    }

    public static <T> String beanToString(T value) {
        if (value == null) {
            return null;
        } else {
            Class<?> clazz = value.getClass();
            if (clazz != Integer.TYPE && clazz != Integer.class) {
                if (clazz == String.class) {
                    return (String)value;
                } else {
                    return clazz != Long.TYPE && clazz != Long.class ? JSON.toJSONString(value) : "" + value;
                }
            } else {
                return "" + value;
            }
        }
    }

    public static <T> T stringToBean(String str, Class<T> clazz) {
        if (str != null && str.length() > 0 && clazz != null) {
            if (clazz != Integer.TYPE && clazz != Integer.class) {
                if (clazz == String.class) {
                    return str;
                } else {
                    return clazz != Long.TYPE && clazz != Long.class ? JSON.toJavaObject(JSON.parseObject(str), clazz) : Long.valueOf(str);
                }
            } else {
                return Integer.valueOf(str);
            }
        } else {
            return null;
        }
    }

    public static Object getFieldValueByName(String fieldName, Object o) {
        if (o == null) {
            return null;
        } else {
            try {
                String firstLetter = fieldName.substring(0, 1).toUpperCase();
                String getter = "get" + firstLetter + fieldName.substring(1);
                Method method = o.getClass().getMethod(getter);
                Object value = method.invoke(o);
                return value;
            } catch (Exception var6) {
                return null;
            }
        }
    }

    public static String[] getFiledName(Object o) {
        Field[] fields = o.getClass().getDeclaredFields();
        String[] fieldNames = new String[fields.length];

        for(int i = 0; i < fields.length; ++i) {
            System.out.println(fields[i].getType());
            fieldNames[i] = fields[i].getName();
        }

        return fieldNames;
    }

    public static List<Map<String, String>> getFiledsInfo(Object o) {
        if (o == null) {
            return new ArrayList();
        } else {
            Field[] fields = o.getClass().getDeclaredFields();
            String[] fieldNames = new String[fields.length];
            List<Map<String, String>> list = new ArrayList();
            Map infoMap = null;

            for(int i = 0; i < fields.length; ++i) {
                infoMap = new HashMap();
                infoMap.put("type", fields[i].getType().toString());
                infoMap.put("name", fields[i].getName());
                list.add(infoMap);
            }

            return list;
        }
    }

    public static Object[] getFiledValues(Object o) {
        String[] fieldNames = getFiledName(o);
        Object[] value = new Object[fieldNames.length];

        for(int i = 0; i < fieldNames.length; ++i) {
            value[i] = getFieldValueByName(fieldNames[i], o);
        }

        return value;
    }

    public static void setProperty(Object obj, String PropertyName, Object value) {
        try {
            Class c = obj.getClass();
            Field f = c.getDeclaredField(PropertyName);
            f.setAccessible(true);
            f.set(obj, value);
        } catch (Exception var5) {
            var5.printStackTrace();
        }

    }

    public static String formatIDCard(String idCard) {
        return !StringUtil.isBlank(idCard) && !idCard.contains("****") && idCard.length() >= 15 ? idCard.substring(0, 6) + "********" + idCard.substring(14) : idCard;
    }

    public static String formatTelephone(String telePhone) {
        return !StringUtil.isBlank(telePhone) && !telePhone.contains("***") && telePhone.length() >= 8 ? telePhone.substring(0, 3) + "****" + telePhone.substring(7) : telePhone;
    }

    public static String formatBankCardNo(String cardNo) {
        return !StringUtil.isBlank(cardNo) && !cardNo.contains("****") && cardNo.length() >= 16 ? cardNo.substring(0, 5) + "**********" + cardNo.substring(15) : cardNo;
    }

    public static boolean checkIsBaseObject(Object param) {
        return param instanceof Integer || param instanceof String || param instanceof Double || param instanceof Float || param instanceof Long || param instanceof Boolean || param instanceof Date || param instanceof Byte || param instanceof Short;
    }

    public static Map<String, Object> ObjToMap(Object obj) {
        HashMap map = null;

        try {
            map = new HashMap();
            Field[] fields = obj.getClass().getDeclaredFields();
            Field[] var3 = fields;
            int var4 = fields.length;

            for(int var5 = 0; var5 < var4; ++var5) {
                Field field = var3[var5];
                field.setAccessible(true);
                map.put(field.getName(), field.get(obj));
            }
        } catch (SecurityException var7) {
            var7.printStackTrace();
        } catch (IllegalArgumentException var8) {
            var8.printStackTrace();
        } catch (IllegalAccessException var9) {
            var9.printStackTrace();
        }

        return map;
    }

    public static String randomUUID() {
        return UUID.randomUUID().toString().replace("-", "");
    }
}

上一篇:java 利用反射读取注解功能


下一篇:手写SpringMVC简单的demo案例