反射操作泛型(Generic)
Java採用泛型擦除機制來引入泛型。Java中的泛型僅僅是給編譯器Javac使用的,確保數據的安全性和免去強制類型轉換的麻煩。但是編譯一旦完成,所有和泛型有關的類型全部被擦除。
爲了通過反射操作這些類型以迎合實際開發的需要,Java新增了ParameterizedType,GenericArrayType,TypeVariable和WildcardType幾種類型來代表不能被歸一到Class類中的類型但是又和原始類型齊名的類型。
- ParameterizedType:表示一種參數化的類型,比如Collection< String >
- GenericArrayType:表示一種元素類型是參數化類型或者類型變量的數組類型
- TypeVariable:是各種類型變量的公共父接口
WildcardType:代表一種通配符類型表達式,比如?、? extends Number、? super Integer。(wildcard是一個單詞:就是”通配符“)
public class TestReflection {
public static void testNoneReflect() {
Person user = new Person();
long start = System.currentTimeMillis();
for (long i = 0; i < Integer.MAX_VALUE; ++i) {
user.getName();
}
long count = System.currentTimeMillis() - start;
System.out.println("沒有反射的耗時" + count);
}
private void testNotAccess() {
// TODO Auto-generated method stub
Person user = new Person();
try {
Class<?> method = Class.forName("com.itheima.co.Person");
try {
Method method2 = method.getMethod("getName");
long start = System.currentTimeMillis();
long count = 0;
for (int i = 0; i < Integer.MAX_VALUE; i++) {
try {
method2.invoke(user, null);
count = System.currentTimeMillis() - start;
System.out.println("反應過來多長時間:" + count);
break;
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchMethodException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void testUseAccess() throws ClassNotFoundException, IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException {
Person user = new Person();
Method method = Class.forName("com.itheima.co.Person").getMethod("getName");
method.setAccessible(true);
long start = System.currentTimeMillis();
for (long i = 0; i < Integer.MAX_VALUE; ++i) {
method.invoke(user, null);
}
long count = System.currentTimeMillis() - start;
System.out.println("有訪問權限, 共消耗 <" + count + "> 毫秒");
}
private void testUserAcc() throws IllegalArgumentException,
IllegalAccessException, InvocationTargetException, SecurityException, NoSuchMethodException, ClassNotFoundException {
// TODO Auto-generated method stub
Person person = new Person();
Method method = Class.forName("com.itheima.co.Person").getMethod(
"getName");
method.setAccessible(true);
long start = System.currentTimeMillis();
for (int i = 0; i < Integer.MAX_VALUE; i++) {
method.invoke(person, null);
// break;
}
long count = System.currentTimeMillis() - start;
System.out.println("設置代碼檢查的東西:" + count);
}
/**
* @param args
* @throws ClassNotFoundException
* @throws NoSuchMethodException
* @throws InvocationTargetException
* @throws IllegalAccessException
* @throws SecurityException
* @throws IllegalArgumentException
*/
public static void main(String[] args) throws IllegalArgumentException, SecurityException, IllegalAccessException, InvocationTargetException, NoSuchMethodException, ClassNotFoundException {
/*testNoneReflect();*/
new TestReflection().testUserAcc();
/* try {
testUseAccess();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchMethodException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}*/
}
}