国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統(tǒng) > Android > 正文

Android AOP之注解處理解釋器詳解(二)

2019-12-12 03:27:44
字體:
供稿:網(wǎng)友

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主要提供了兩個(gè)類,來完成Annotation的提取:

  • Java.lang.annotation.Annotation接口:這個(gè)接口是所有Annotation類型的父接口。
  • java.lang.reflect.AnnotatedElement接口:該接口代表程序中可以被注解的程序元素。

1.1 Annotation接口

這個(gè)接口比較少用,這個(gè)接口里面有四個(gè)方法:

package java.lang.annotation;public interface Annotation { boolean equals(Object obj); int hashCode(); String toString(); //返回該注解的Class,元素使用了多個(gè)注解的時(shí)候,可以進(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)行時(shí)可以獲取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注解修飾的方法:stopServiceAsUser02-18 15:23:41.622 12446-12446/? E/tag: 沒有被mytag注解修飾的方法:takeKeyEvents02-18 15:23:41.622 12446-12446/? E/tag: 沒有被mytag注解修飾的方法:testNo02-18 15:23:41.622 12446-12446/? E/tag: 被mytag注解修飾的方法:testYes02-18 15:23:41.632 12446-12446/? E/tag: 沒有被mytag注解修飾的方法:toString02-18 15:23:41.632 12446-12446/? E/tag: 沒有被mytag注解修飾的方法:triggerSearch02-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

感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!

發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 株洲县| 宁国市| 罗山县| 宁远县| 茶陵县| 嘉黎县| 灌云县| 平阳县| 深泽县| 达尔| 武胜县| 武山县| 青河县| 山阴县| 沁阳市| 茌平县| 突泉县| 尼木县| 石家庄市| 伽师县| 象山县| 安阳市| 平乐县| 长武县| 东至县| 沙河市| 靖州| 普格县| 青河县| 宣武区| 成武县| 孝义市| 滦南县| 资源县| 博野县| 罗平县| 叶城县| 盐津县| 汉源县| 溧阳市| 仙游县|