java如何判斷一個對象是否為空對象
最近項目中遇到一個問題,在用戶沒填數(shù)據(jù)的時候,我們需要接收從前端傳過來的對象為null,但是前端說他們一個一個判斷特別麻煩,只能傳個空對象過來,我第一個想法就是可以通過反射來判斷對象是否為空。
第一版:
User.java
public class User { ? ? private String username; ? ? private Boolean active; ? ? private Long id; ? ? // 省略get和set方法 }
ReflectUtil.java
public class ReflectUtil { ? ? public static boolean isObjectNull(Object obj){ ? ? ? ? if (obj != null) { ? ? ? ? ? ? Class<?> objClass = obj.getClass(); ? ? ? ? ? ? Method[] declaredMethods = objClass.getDeclaredMethods(); ? ? ? ? ? ? if (declaredMethods.length > 0) { ? ? ? ? ? ? ? ? int methodCount = 0; // get 方法數(shù)量 ? ? ? ? ? ? ? ? int nullValueCount = 0; // 結(jié)果為空 ? ? ? ? ? ? ? ? for (Method declaredMethod : declaredMethods) { ? ? ? ? ? ? ? ? ? ? String name = declaredMethod.getName(); ? ? ? ? ? ? ? ? ? ? if (name.startsWith("get") || name.startsWith("is")){ ? ? ? ? ? ? ? ? ? ? ? ? methodCount += 1; ? ? ? ? ? ? ? ? ? ? ? ? try { ? ? ? ? ? ? ? ? ? ? ? ? ? ? Object invoke = declaredMethod.invoke(obj); ? ? ? ? ? ? ? ? ? ? ? ? ? ? if (invoke == null) { ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? nullValueCount += 1; ? ? ? ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? ? ? } catch (IllegalAccessException | InvocationTargetException e){ ? ? ? ? ? ? ? ? ? ? ? ? ? ? e.printStackTrace(); ? ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? return methodCount == nullValueCount; ? ? ? ? ? ? } ? ? ? ? } ? ? ? ? return false; ? ? } }
TestReflect.java
public class TestReflect { ? ? public static void main(String[] args) { ? ? ? ? User user = new User(); ? ? ? ? System.out.println(ReflectUtil.isObjectNull(user)); ? ? } }
結(jié)果:
true
第一版 獲取一個類的聲明的方法,判斷方法如果以get或者is開頭就是get方法,然后通過反射調(diào)用改方法獲取結(jié)果,再判斷結(jié)果是否為空,如果結(jié)果為null的話就把nullValueCount+1,最后返回結(jié)果為空的值的數(shù)量和get方法數(shù)量比較的結(jié)果,如果兩者數(shù)量相同則說明該對象為空,反之不為空。
第一版也可以判斷一個對象是否為空,但前提是對象必須使用包裝類,沒有默認值的就不行了,當然你也可以根據(jù)類型和返回值結(jié)果來判斷對象是否為空,但是如果想忽略某個屬性不做判斷,改起來就有點麻煩了。 后來想知道spring 的BeanUtils 是怎么實現(xiàn)屬性復制的就看了一下,發(fā)現(xiàn)了新的方法,于是就有了第二版。
第二版:
/** ? ? ?* ?判斷對象是否為空, ? ? ?* @param obj ? ? ?* @param ignoreProperties 忽略的屬性 ? ? ?* @return 如果get 方法的數(shù)量等于 屬性為空的數(shù)量 返回true,否則false ? ? ?*/ ? ? public static boolean isNullObject(Object obj , String... ignoreProperties) throws IntrospectionException { ? ? ? ? if (obj != null) { ? ? ? ? ? ? Class<?> objClass = obj.getClass(); ? ? ? ? ? ? BeanInfo beanInfo = Introspector.getBeanInfo(objClass); ? ? ? ? ? ? PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors(); ? ? ? ? ? ? List<String> ignoreList = (ignoreProperties != null ? Arrays.asList(ignoreProperties) : null); ? ? ? ? ? ? int count = 1; // 結(jié)果為空的屬性數(shù)量 初始化為1 去除Object的getClass方法 ? ? ? ? ? ? int propertyCount = propertyDescriptors.length; // 屬性數(shù)量 ? ? ? ? ? ? if (ignoreList != null){ ? ? ? ? ? ? ? ? propertyCount -= ignoreList.size(); ? ? ? ? ? ? } ? ? ? ? ? ? for (PropertyDescriptor propertyDescriptor : propertyDescriptors) { ? ? ? ? ? ? ? ? Method readMethod = propertyDescriptor.getReadMethod(); ? ? ? ? ? ? ? ? String name = propertyDescriptor.getName(); ? ? ? ? ? ? ? ? if (readMethod != null && (ignoreList == null || !ignoreList.contains(name))) { ? ? ? ? ? ? ? ? ? ? Class<?> returnType = readMethod.getReturnType(); ? ? ? ? ? ? ? ? ? ? String typeName = returnType.getSimpleName(); ? ? ? ? ? ? ? ? ? ? Object invoke = null; ? ? ? ? ? ? ? ? ? ? try { ? ? ? ? ? ? ? ? ? ? ? ? invoke = readMethod.invoke(obj); ? ? ? ? ? ? ? ? ? ? ? ? if (invoke == null) { ? ? ? ? ? ? ? ? ? ? ? ? ? ? count+=1; ? ? ? ? ? ? ? ? ? ? ? ? }else { ? ? ? ? ? ? ? ? ? ? ? ? ? ? switch (typeName) { ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? case "String": ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? if ("".equals(invoke.toString().trim())) { ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? count += 1; ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? case "Integer": ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? if ((Integer) invoke <= 0) { ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? count += 1; ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? case "int": ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? if ((int) invoke <= 0) { ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? count += 1; ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? case "double": ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? if ((double) invoke <= 0.0d) { ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? count += 1; ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? case "Double": ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? if ((Double) invoke <= 0.0D) { ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? count += 1; ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? case "float": ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? if ((float) invoke <= 0.0f) { ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? count += 1; ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? case "Float": ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? if ((Float) invoke <= 0.0F) { ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? count += 1; ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? case "Long": ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? if ((Long) invoke <= 0L) { ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? count += 1; ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? case "long": ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? if ((long) invoke <= 0L) { ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? count += 1; ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? } catch (IllegalAccessException | InvocationTargetException e) { ? ? ? ? ? ? ? ? ? ? ? ? e.printStackTrace(); ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? } ? ? ? ? ? ? } ? ? ? ? ? ? return propertyCount == count; ? ? ? ? } ? ? ? ? return true; ? ? }
第一版和第二版思想基本都是一樣的,都是通過讀方法去判斷返回值是否為空,只不過第二版在第一版上加強了可以忽略屬性這個功能。
通過spring 的beanutils發(fā)現(xiàn)PropertyDescriptor這個類,從名字看來是個屬性描述器,描述屬性相關(guān)的東西,通過屬性描述器可以獲取bean的屬性名稱,讀寫方法,使用起來還挺方便。
通過Introspector內(nèi)省類的靜態(tài)方法getBeanInfo(Class<?> beanClass)獲取BeanInfo,然后通過BeanInfo對象的getPropertyDescriptors()就可以返回屬性描述器。
由于沒有太多研究就不多介紹了。
到此這篇關(guān)于java如何判斷一個對象是否為空對象的文章就介紹到這了,更多相關(guān)java 判斷對象是否為空內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
spring中jdbcTemplate.batchUpdate的幾種使用情況
本文主要介紹了spring中jdbcTemplate.batchUpdate的幾種使用情況,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2022-04-04Java中MyBatis傳入?yún)?shù)parameterType問題
這篇文章主要介紹了Java中MyBatis傳入?yún)?shù)parameterType問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-12-12詳解json string轉(zhuǎn)換為java bean及實例代碼
這篇文章主要介紹了詳解json string轉(zhuǎn)換為java bean及實例代碼的相關(guān)資料,這里提供實例代碼幫助大家理解,需要的朋友可以參考下2017-07-07springboot中使用undertow踩坑記(最新推薦)
這篇文章主要介紹了springboot中使用undertow踩坑記,springboot內(nèi)置類web中間件,將web服務器管理權(quán)交給了容器,本文分步驟給大家介紹的非常詳細,需要的朋友可以參考下2024-08-08