判斷一個對象是否存在有值的屬性、反射根據屬性名獲取屬性值、反射獲取所有屬性名以及值


public class ObjectFieldIsNotNullUtil {


    public static void main(String[] args)  {
        UserVO vo = new UserVO();
        vo.setToken("1");
        vo.setUserid("1");
        boolean b = notNull(vo,"token");
        System.out.println(b);
    }

    /**
     * 判斷一個對象的屬性有無值
     * @param o
     * @param as 需要過濾的屬性,逗號分隔
     * @return
     */
    public  static boolean  notNull(Object o,String as){
        try {
            for(Field f : o.getClass().getDeclaredFields()){
                f.setAccessible(true);
                Object o1 = f.get(o);
                if(o1!=null && !"".equals(o1)&& !as.contains(f.getName())){
                    return true;
                }
            }
        }catch (Exception e){
            return false;
        }

        return false;
    }


}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM