第一種
/**
* 對象非空判斷
* 支持類型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;
}
如有不對,歡迎指正!