Java反射機制介紹
1.通過反射,我們可以構建實例,得到成員變量的值,得到方法并調用。
還可以獲得定義在成員變量、方法、方法參數(shù)上的注解。
接下來看代碼實現(xiàn),然后講原理。
1)構建無參實例:通過反射調用無參構造函數(shù)
//1.通過全類名加載字節(jié)碼對象
Class clazz = Class.forName("com.example.lib.Person");
//2.通過類的字節(jié)碼拿到定義的構造函數(shù)
Constructor constructor = clazz.getConstructor();
//3.通過構造方法創(chuàng)建對象
Object obj = constructor.newInstance();2)構建有參數(shù)實例:
//1.通過全類名加載字節(jié)碼對象
Class clazz = Class.forName("com.example.lib.Person");
//2.通過類的字節(jié)碼拿到定義的構造函數(shù)
Constructor constructor = clazz.getConstructor(int.class,String.class);
//3.通過構造方法創(chuàng)建對象
Object obj = constructor.newInstance(20,"xiaohua");3)通過反射獲取成員變量的值。
//4.通過屬性名獲取屬性
Field field = clazz.getDeclaredField("age");
field.setAccessible(true);
//5.調用get方法拿到對象obj屬性age的值
Integer age = (Integer) field.get(obj);4)通過反射調用對象的方法。
//4.通過方法名和參數(shù)類型,拿到方法
method = clazz.getMethod("setAge", int.class);
//5.調用方法 obj是哪個對象身上的方法。
method.invoke(obj, 21);
method = clazz.getMethod("getAge");
method.invoke(obj);5).通過反射獲取靜態(tài)變量的值。
//1.通過全類名加載字節(jié)碼對象
Class clazz = Class.forName("com.example.lib.Person");
//2.獲取靜態(tài)屬性ID
Field field = clazz.getField("ID");
field.setAccessible(true);
//3.拿到靜態(tài)屬性ID的值。
// 因為靜態(tài)變量存在方法區(qū),在對象創(chuàng)建之前,就已經(jīng)加裝到了內存
//所以,沒有對象,也可以獲取變量的值,這里傳null也是可以的。
int id = (int) field.get(null);2.通過反射獲取定義的注解的值
1)獲取成員變量的注解以及值。
@Target({ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
public @interface BindView {
int value();
}public class MainActivity {
@BindView(10000)
TextView textView;
} //10通過反射拿到定義在屬性上的注解
Class clazz = MainActivity.class;
Field textView = clazz.getDeclaredField("textView");
BindView bindView = textView.getAnnotation(BindView.class);
int txtId = bindView.value();3)通過反射獲取定義在方法和方法參數(shù)上的注解以及值
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Post {
String value() default "";
}public interface NetWorkInterface {
@Post("http://www.baidu.com")
Call getPerson(@Queue("name") String name, @Queue("200") int price);
} //11通過反射拿到方法上定義的注解
clazz = NetWorkInterface.class;
Method method = clazz.getMethod("getPerson", String.class, int.class);
//獲取Post注解
Post post = method.getAnnotation(Post.class);
//獲取值
String url = post.value(); //12通過反射拿到參數(shù)上的注解
//為是個二維數(shù)組,因為方法參數(shù)會有多個,一個參數(shù)有可能定義多個注解
Annotation[][] annotations = method.getParameterAnnotations();
for (Annotation[] ans : annotations) {
for (Annotation an : ans) {
if (an instanceof Queue) {
Queue queue = (Queue) an;
String value = queue.value();
}
}
}4)獲取方法的參數(shù)和返回值類型。
//13.拿到方法參數(shù)的類型。
Type[] types = method.getGenericParameterTypes();
for (Type type : types) {
System.out.println(type.toString());
}
//14.獲取方法返回值類型
Type type = method.getGenericReturnType();3總結:通過反射,可以拿到對象身上的成員變量的值、調用方法,獲取定義在成員變量、方法和 方法參數(shù)上的注解。Retrofit 就用到了注解加反射技術,和動態(tài)代理(這個技術稍后分享)
4.通過反射,可以做到以上事情。反射的原理是啥?
1)我們寫的源代碼是.java文件,通過javac編譯后成為.class文件,即字節(jié)碼文件。
2)程序執(zhí)行時,JVM會類加載字節(jié)碼文件到內存,嚴格意義上說是加載到方法區(qū),并轉換成
java.lang.Class對象。萬事萬物皆對象,Class被稱為類對象,描述類在元數(shù)據(jù)空間的數(shù)據(jù)結構,包含類中定義的屬性、方法、構造方法、注解、接口等信息。
所有反射的第一步是拿到類對象Class對象。拿到了Class對象,也就拿到了類中定義的一切。
Class clazz = Class.forName("com.example.lib.Person");
這行代碼就是通過類加載器把Person類加載到內存,并得到對應的Class 對象。
到此這篇關于Java反射機制介紹的文章就介紹到這了,更多相關Java反射內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Springboot2.x結合Mabatis3.x下Hikari連接數(shù)據(jù)庫報超時錯誤
本文針對Springboot2.x與Mybatis3.x結合使用時,Hikari連接數(shù)據(jù)庫出現(xiàn)超時錯誤的問題進行了深入分析,并提供了一系列有效的解決方法,感興趣的可以了解一下2023-11-11
解決IDEA2020.2插件lombok報錯問題(親測有效)
這篇文章主要介紹了解決IDEA2020.2插件lombok報錯問題,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-08-08
maven profile實現(xiàn)多環(huán)境配置的示例
這篇文章主要介紹了maven profile實現(xiàn)多環(huán)境配置的示例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2021-01-01

