java中对象非空判断

第一种

	/**
     * 对象非空判断
     * 支持类型Integer、String、Double、Long、Float、List、Object、Object[]、Date
     * @param obj
     * @return
     * @throws Exception
     */
    public static boolean isNotNull(Object obj) throws Exception {
        try {
            Field[] fields = obj.getClass().getDeclaredFields();
            for (Field f: fields) {
                f.setAccessible(true);
                PropertyDescriptor pd = new PropertyDescriptor(f.getName(),obj.getClass());
                Method getMethod = pd.getReadMethod();
                if(f.getType() == Integer.class || f.getType() == int.class){
                    if ((Integer)getMethod.invoke(obj) == null || (Integer)getMethod.invoke(obj) == 0){
                        return false;
                    }
                }
                if(f.getType() == String.class || f.getType() == char.class){
                    if (StringUtils.isEmpty((String)getMethod.invoke(obj))){
                        return false;
                    }
                }
                if(f.getType() == Double.class || f.getType() == double.class){
                    if ((Double)getMethod.invoke(obj)-0.0<1e-6){
                        return false;
                    }
                }
                if(f.getType() == Long.class || f.getType() == long.class){
                    if ((Long)getMethod.invoke(obj) == null || (Long)getMethod.invoke(obj) == 0){
                        return false;
                    }
                }
                if(f.getType() == Float.class || f.getType() == float.class){
                    if ((Float)getMethod.invoke(obj)>-0.000001&&(Float)getMethod.invoke(obj)<0.000001){
                        return false;
                    }
                }
                if(f.getType() == List.class){
                    if (null == (List)getMethod.invoke(obj) || ((List) getMethod.invoke(obj)).size() == 0){
                        return false;
                    }
                }
                if(f.getType() == Object.class){
                    if (null == getMethod.invoke(obj) || StringUtils.isEmpty((String)getMethod.invoke(obj))){
                        return false;
                    }
                }
                if(f.getType() == Object[].class){
                    if (null == (Object[])getMethod.invoke(obj) || ((Object[]) getMethod.invoke(obj)).length == 0){
                        return false;
                    }
                }
                if(f.getType() == Date.class){
                    if (null == (Date)getMethod.invoke(obj)){
                        return false;
                    }
                }
            }
        } catch (Exception e){
            throw e;
        }
        return true;
    }

第二种

	/**
     * 对象非空判断
     * @param obj
     * @return
     * @throws Exception
     */
    public static boolean isNotNull(Object obj) throws Exception {
        try {
            Field[] fields = obj.getClass().getDeclaredFields();
            for (Field f: fields) {
                f.setAccessible(true);
                Object object = f.get(obj);
                if (object instanceof CharSequence) {
                    if (!org.springframework.util.ObjectUtils.isEmpty(object)) {
                        return false;
                    }
                } else {
                    if (null != object) {
                        return false;
                    }
                }
            }
        } catch (Exception e){
            throw e;
        }
        return true;
    }

第三种

	/**
     * 判断对象是否非空
     *
     * @param o
     * @return
     */
    public static boolean isNull(Object o) {
        return null == o;
    }

    /**
     * 判断集合是否非空
     *
     * @param list
     * @return
     */
    public static boolean isNull(List<?> list) {
        return null == list || list.size() == 0;
    }

    /**
     * 判断集合是否非空
     *
     * @param set
     * @return
     */
    public static boolean isNull(Set<?> set) {
        return null == set || set.size() == 0;
    }

    /**
     * 判断集合是否为空
     *
     * @param map
     * @return
     */
    public static boolean isNull(Map<?, ?> map) {
        return null == map || map.size() == 0;
    }

    /**
     * 判断Long是否为空
     *
     * @param lg
     * @return
     */
    public static boolean isNull(Long lg) {
        return null == lg || lg == 0;
    }

    /**
     * 判断Integer是否为空
     *
     * @param it
     * @return
     */
    public static boolean isNull(Integer it) {
        return null == it || it == 0;
    }

    /**
     * 判断文件是否为空
     * @param file
     * @return
     */
    public static boolean isNull(File file) {
        return null == file || !file.exists();
    }

    /**
     * 判断数组是否为空
     *
     * @param strs
     * @return
     */
    public static boolean isNull(Object[] strs) {
        return null == strs || strs.length == 0;
    }

如有不对,欢迎指正!

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值