/**
* 空值檢查
*
* @param pInput 要檢查的字符串
* @return boolean 返回檢查結(jié)果,但傳入的字符串為空的場(chǎng)合,返回真
*/
public static boolean isNull (Object pInput) {
// 判斷參數(shù)是否為空或者’’
if (pInput == null || "’’".equals(pInput)) {
return true;
} else if ("java.lang.String".equals(pInput.getClass().getName())){
// 判斷傳入的參數(shù)的String類型的
// 替換各種空格
String tmpInput = Pattern.compile("[\\r|\\n|\\u3000]")
.matcher((String)pInput).replaceAll("");
// 匹配空
return Pattern.compile("^(\\s)*$")
.matcher(tmpInput).matches();
} else {
// 方法類
Method method = null;
try {
// 訪問(wèn)傳入?yún)?shù)的size方法
method = pInput.getClass().getMethod("size");
// 判斷size大小
// size為0的場(chǎng)合
if (Integer.parseInt(String.valueOf(method.invoke(pInput))) == 0) {
return true;
} else {
return false;
}
} catch (Exception e) {
// 訪問(wèn)失敗
try {
// 訪問(wèn)傳入?yún)?shù)的getItemCount方法
method = pInput.getClass().getMethod("getItemCount");
// 判斷size大小
// getItemCount為0的場(chǎng)合
if (Integer.parseInt(String.valueOf(method.invoke(pInput))) == 0) {
return true;
} else {
return false;
}
} catch (Exception ex) {
// 訪問(wèn)失敗
try{
// 判斷傳入?yún)?shù)的長(zhǎng)度
// 長(zhǎng)度為0的場(chǎng)合
if (Array.getLength(pInput) == 0) {
return true;
} else {
return false;
}
} catch (Exception exx) {
// 訪問(wèn)失敗
try{
// 訪問(wèn)傳入?yún)?shù)的hasNext方法
method = Iterator.class.getMethod("hasNext");
// 轉(zhuǎn)換hasNext的值
return !Boolean.valueOf(String.valueOf(method.invoke(pInput)))? true : false;
} catch (Exception exxx) {
// 以上場(chǎng)合不滿足返回假
return false;
}
}
}
}
}
}
以下是測(cè)試代碼
import java.awt.List;
import java.lang.reflect.Array;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.Map;
import java.util.Set;
import java.util.SortedMap;
import java.util.TreeMap;
import java.util.TreeSet;
import java.util.Vector;
import java.util.WeakHashMap;
import java.util.regex.Pattern;
import java.util.Iterator;
public class TestNull {
@SuppressWarnings("unchecked")
public static void main(String args[]) throws Exception{
char ch [][] = new char[10][10];
System.out.println("char ch[]= " + isNull(ch));
byte be [] = new byte[10];
System.out.println("byte be[]= " + isNull(be));
float[] ft = new float[10];
System.out.println("float ft[]= " + isNull(ft));
double ad[] = new double[10];
System.out.println("double ad[]= " + isNull(ad));
int ai[][][] = new int [10][10][10];
System.out.println("int ai[]= " + isNull(ai));
Object ob = null;
System.out.println("Object= " + isNull(ob));
String a [] =null;
System.out.println("String a []= " + isNull(a));
List aa = new List();
System.out.println("List= " + isNull(aa));
ArrayList aaa = new ArrayList();
System.out.println("ArrayList= " + isNull(aaa));
Map map = new HashMap();
System.out.println("HashMap= " + isNull(map));
String a2 [][][][] = new String[10][10][10][20];
System.out.println("String a2 [][][][]= " + isNull(a2));
HashMap map2 = new HashMap();
System.out.println("HashMap= " + isNull(map2));
Vector keys = new Vector();
System.out.println("Vector= " + isNull(keys));
Hashtable ht = new Hashtable();
System.out.println("Hashtable= " + isNull(ht));
LinkedList lt = new LinkedList();
System.out.println("LinkedList= " + isNull(lt));
TreeSet tt = new TreeSet();
System.out.println("TreeSet= " + isNull(tt));
Set ss = new TreeSet();
System.out.println("TreeSet= " + isNull(ss));
Iterator it = new ArrayList().iterator();
System.out.println("Iterator= " + isNull(it));
LinkedHashMap llp = new LinkedHashMap();
System.out.println("LinkedHashMap= " + isNull(llp));
LinkedHashSet llt = new LinkedHashSet();
System.out.println("LinkedHashSet= " + isNull(llt));
WeakHashMap wp =new WeakHashMap();
System.out.println("WeakHashMap= " + isNull(wp));
String sra = "’’,a,b,c";
System.out.println(sra.split(",")[0]);
System.out.println("sra= " + isNull(sra.split(",")[0]));
SortedMap m= new TreeMap();
System.out.println("SortedMap= " + isNull(m));
}
* 空值檢查
*
* @param pInput 要檢查的字符串
* @return boolean 返回檢查結(jié)果,但傳入的字符串為空的場(chǎng)合,返回真
*/
public static boolean isNull (Object pInput) {
// 判斷參數(shù)是否為空或者’’
if (pInput == null || "’’".equals(pInput)) {
return true;
} else if ("java.lang.String".equals(pInput.getClass().getName())){
// 判斷傳入的參數(shù)的String類型的
// 替換各種空格
String tmpInput = Pattern.compile("[\\r|\\n|\\u3000]")
.matcher((String)pInput).replaceAll("");
// 匹配空
return Pattern.compile("^(\\s)*$")
.matcher(tmpInput).matches();
} else {
// 方法類
Method method = null;
try {
// 訪問(wèn)傳入?yún)?shù)的size方法
method = pInput.getClass().getMethod("size");
// 判斷size大小
// size為0的場(chǎng)合
if (Integer.parseInt(String.valueOf(method.invoke(pInput))) == 0) {
return true;
} else {
return false;
}
} catch (Exception e) {
// 訪問(wèn)失敗
try {
// 訪問(wèn)傳入?yún)?shù)的getItemCount方法
method = pInput.getClass().getMethod("getItemCount");
// 判斷size大小
// getItemCount為0的場(chǎng)合
if (Integer.parseInt(String.valueOf(method.invoke(pInput))) == 0) {
return true;
} else {
return false;
}
} catch (Exception ex) {
// 訪問(wèn)失敗
try{
// 判斷傳入?yún)?shù)的長(zhǎng)度
// 長(zhǎng)度為0的場(chǎng)合
if (Array.getLength(pInput) == 0) {
return true;
} else {
return false;
}
} catch (Exception exx) {
// 訪問(wèn)失敗
try{
// 訪問(wèn)傳入?yún)?shù)的hasNext方法
method = Iterator.class.getMethod("hasNext");
// 轉(zhuǎn)換hasNext的值
return !Boolean.valueOf(String.valueOf(method.invoke(pInput)))? true : false;
} catch (Exception exxx) {
// 以上場(chǎng)合不滿足返回假
return false;
}
}
}
}
}
}
以下是測(cè)試代碼
import java.awt.List;
import java.lang.reflect.Array;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.Map;
import java.util.Set;
import java.util.SortedMap;
import java.util.TreeMap;
import java.util.TreeSet;
import java.util.Vector;
import java.util.WeakHashMap;
import java.util.regex.Pattern;
import java.util.Iterator;
public class TestNull {
@SuppressWarnings("unchecked")
public static void main(String args[]) throws Exception{
char ch [][] = new char[10][10];
System.out.println("char ch[]= " + isNull(ch));
byte be [] = new byte[10];
System.out.println("byte be[]= " + isNull(be));
float[] ft = new float[10];
System.out.println("float ft[]= " + isNull(ft));
double ad[] = new double[10];
System.out.println("double ad[]= " + isNull(ad));
int ai[][][] = new int [10][10][10];
System.out.println("int ai[]= " + isNull(ai));
Object ob = null;
System.out.println("Object= " + isNull(ob));
String a [] =null;
System.out.println("String a []= " + isNull(a));
List aa = new List();
System.out.println("List= " + isNull(aa));
ArrayList aaa = new ArrayList();
System.out.println("ArrayList= " + isNull(aaa));
Map map = new HashMap();
System.out.println("HashMap= " + isNull(map));
String a2 [][][][] = new String[10][10][10][20];
System.out.println("String a2 [][][][]= " + isNull(a2));
HashMap map2 = new HashMap();
System.out.println("HashMap= " + isNull(map2));
Vector keys = new Vector();
System.out.println("Vector= " + isNull(keys));
Hashtable ht = new Hashtable();
System.out.println("Hashtable= " + isNull(ht));
LinkedList lt = new LinkedList();
System.out.println("LinkedList= " + isNull(lt));
TreeSet tt = new TreeSet();
System.out.println("TreeSet= " + isNull(tt));
Set ss = new TreeSet();
System.out.println("TreeSet= " + isNull(ss));
Iterator it = new ArrayList().iterator();
System.out.println("Iterator= " + isNull(it));
LinkedHashMap llp = new LinkedHashMap();
System.out.println("LinkedHashMap= " + isNull(llp));
LinkedHashSet llt = new LinkedHashSet();
System.out.println("LinkedHashSet= " + isNull(llt));
WeakHashMap wp =new WeakHashMap();
System.out.println("WeakHashMap= " + isNull(wp));
String sra = "’’,a,b,c";
System.out.println(sra.split(",")[0]);
System.out.println("sra= " + isNull(sra.split(",")[0]));
SortedMap m= new TreeMap();
System.out.println("SortedMap= " + isNull(m));
}

