public static boolean hasEmpty(Object object) throws Exception { Class<?> aClass = object.getClass(); Field[] declaredFields = aClass.getDeclaredFields(); for (Field field : declaredFields){ String name = field.getName(); name = name.substring(0, 1).toUpperCase() + name.substring(1); String type = field.getGenericType().toString(); Method method = aClass.getMethod("get" + name); if (type.equals("class java.lang.String")) { String value = (String) method.invoke(object); if (value==null) { return true; } }else if(type.equals("class java.lang.Integer")){ Integer value = (Integer) method.invoke(object); if (value==null) { return true; } } } return false; }
该方法方便了对实体类中每个参数的判断,但是使用了反射,会影响程序运行效率。