第一种
/**
* 对象非空判断
* 支持类型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;
}
如有不对,欢迎指正!