Android AOP之注解處理解釋器詳解(二)
Android APO 注解處理解釋器
相關(guān)文章:
Android AOP注解Annotation詳解(一)
Android AOP之注解處理解釋器詳解(二)
Android AOP 注解詳解及簡單使用實(shí)例(三)
一、提取Annotation信息
當(dāng)開發(fā)者使用了Annotation修飾了類、方法、Field等成員之后,這些Annotation不會自己生效,必須由開發(fā)者提供相應(yīng)的代碼來提取并處理Annotation信息。這些處理提取和處理Annotation的代碼統(tǒng)稱為APT(Annotation Processing Tool)。
JDK主要提供了兩個類,來完成Annotation的提?。?/p>
- Java.lang.annotation.Annotation接口:這個接口是所有Annotation類型的父接口。
- java.lang.reflect.AnnotatedElement接口:該接口代表程序中可以被注解的程序元素。
1.1 Annotation接口
這個接口比較少用,這個接口里面有四個方法:
package java.lang.annotation;
public interface Annotation {
boolean equals(Object obj);
int hashCode();
String toString();
//返回該注解的Class,元素使用了多個注解的時候,可以進(jìn)行輸出判斷
Class<? extends Annotation> annotationType();
}
1.2 AnnotatedElement接口
該接口最常用的方法是isAnnotationPresent()、getAnnotation(Class annotationClass):
package java.lang.reflect;
import java.lang.annotation.Annotation;
public interface AnnotatedElement {
//判斷此元素上是否存在指定類型的注解,如果存在則返回true,否則返回false
default boolean isAnnotationPresent(Class<? extends Annotation> annotationClass) {
return getAnnotation(annotationClass) != null;
}
//返回此元素上存在的指定類型的注解,如果該類型的注解不存在,則返回null
<T extends Annotation> T getAnnotation(Class<T> annotationClass);
//返回此元素上存在的所有注解。
Annotation[] getAnnotations();
//返回此元素上存在的所有注解。不包括繼承
Annotation[] getDeclaredAnnotations();
default <T extends Annotation> Annotation getDeclaredAnnotation(Class<T> annotationClass) {
return AnnotatedElements.getDeclaredAnnotation(this, annotationClass);
}
default <T extends Annotation> T[] getDeclaredAnnotationsByType(Class<T> annotationClass) {
return AnnotatedElements.getDeclaredAnnotationsByType(this, annotationClass);
}
default <T extends Annotation> T[] getAnnotationsByType(Class<T> annotationClass) {
return AnnotatedElements.getAnnotationsByType(this, annotationClass);
}
}
二、栗子One
簡單獲取方法
2.1 定義注解MyTag
@Target(ElementType.METHOD) //修飾方法
@Retention(RetentionPolicy.RUNTIME) //運(yùn)行時可以獲取
public @interface MyTag {
}
2.2 定義解析器
public class MyTagParser {
public static void process(Object clazz) {
try {
for (Method method : clazz.getClass().getMethods()) {
if (method.isAnnotationPresent(MyTag.class)) {
//獲取到了,輸出
Log.e("tag","被mytag注解修飾的方法:" + method.getName());
} else {
Log.e("tag","沒有被mytag注解修飾的方法:" + method.getName());
}
}
} catch (Exception en) {
en.printStackTrace();
}
}
}
2.3 啟動Activity
public class MainActivity extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//調(diào)用解析器
MyTagParser.process(this);
}
@MyTag
public void testYes(){
}
public void testNo(){
}
}
2.4 結(jié)果
運(yùn)行就會看到輸出,表示已經(jīng)獲取到了對應(yīng)的實(shí)例
...... 02-18 15:23:41.622 12446-12446/? E/tag: 沒有被mytag注解修飾的方法:stopServiceAsUser 02-18 15:23:41.622 12446-12446/? E/tag: 沒有被mytag注解修飾的方法:takeKeyEvents 02-18 15:23:41.622 12446-12446/? E/tag: 沒有被mytag注解修飾的方法:testNo 02-18 15:23:41.622 12446-12446/? E/tag: 被mytag注解修飾的方法:testYes 02-18 15:23:41.632 12446-12446/? E/tag: 沒有被mytag注解修飾的方法:toString 02-18 15:23:41.632 12446-12446/? E/tag: 沒有被mytag注解修飾的方法:triggerSearch 02-18 15:23:41.632 12446-12446/? E/tag: 沒有被mytag注解修飾的方法:unbindService .......
三、栗子Two
取到方法里面的值
3.1 定義注解
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface MyTag {
String name() default "天平";
int age();
}
3.2 定義解析器
public class MyTagParser {
public static void parser(Object o){
Class clazz = o.getClass();
for(Method method:clazz.getMethods()){
if(method.isAnnotationPresent(MyTag.class)){
MyTag myTag = method.getAnnotation(MyTag.class);
Log.e("tag","方法名:"+method.getName()+"的注解值為"+myTag.name()+","+myTag.age());
}
}
}
}
3.3 定義activity
public class MainActivity extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MyTagParser.parser(this);
}
@MyTag(age = 20)
public void testYes(){
}
}
3.3 結(jié)果
將會輸出以下內(nèi)容,name和age都可以獲取到。
02-18 16:11:53.493 25662-25662/? E/tag: 方法名:testYes的注解值為天平,20
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
- 自定義Android注解系列教程之注解變量
- Android基于注解的6.0權(quán)限動態(tài)請求框架詳解
- Android 反射注解與動態(tài)代理綜合使用詳解
- Android注解使用之ButterKnife 8.0詳解
- Android中封裝SDK時常用的注解總結(jié)
- Android AOP 注解詳解及簡單使用實(shí)例(三)
- Android AOP注解Annotation詳解(一)
- Android注解框架對比分析
- Android注解ButterKnife的基本使用
- Android 中的注解詳細(xì)介紹
- Android 中的注解深入探究
- 深入分析安卓(Android)中的注解
- Android注解基礎(chǔ)介紹快速入門與解讀
相關(guān)文章
Android異步加載數(shù)據(jù)和圖片的保存思路詳解
這篇文章主要介紹了Android異步加載數(shù)據(jù)和圖片的保存思路詳解的相關(guān)資料,需要的朋友可以參考下2016-04-04
Android編程實(shí)現(xiàn)創(chuàng)建,刪除,判斷快捷方式的方法
這篇文章主要介紹了Android編程實(shí)現(xiàn)創(chuàng)建,刪除,判斷快捷方式的方法,結(jié)合實(shí)例形式分析了Android編程針對快捷方式的常用操作技巧,需要的朋友可以參考下2017-02-02
Android資源文件與層次式導(dǎo)航超詳細(xì)講解
這篇文章主要介紹了Android資源文件與層次式導(dǎo)航,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧2022-12-12
Android socket實(shí)現(xiàn)原理詳解 服務(wù)端和客戶端如何搭建
這篇文章主要為大家詳細(xì)介紹了Android socket實(shí)現(xiàn)原理詳解,以及服務(wù)端和客戶端的搭建方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-08-08
限時搶購秒殺系統(tǒng)架構(gòu)分析與實(shí)戰(zhàn)
這篇文章主要介紹了限時搶購秒殺系統(tǒng)架構(gòu)分析與實(shí)戰(zhàn) 的相關(guān)資料,需要的朋友可以參考下2016-01-01
淺析Android系統(tǒng)的架構(gòu)以及程序項(xiàng)目的目錄結(jié)構(gòu)
這篇文章主要介紹了Android系統(tǒng)的架構(gòu)以及程序項(xiàng)目的目錄結(jié)構(gòu),是安卓開發(fā)入門學(xué)習(xí)中的基礎(chǔ)知識,需要的朋友可以參考下2016-04-04
Android 圖片的三級緩存機(jī)制實(shí)例分析
這篇文章主要介紹了Android 圖片的三級緩存機(jī)制實(shí)例分析的相關(guān)資料,需要的朋友可以參考下2017-05-05

