欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時(shí)間:2017年03月07日 09:55:27   作者:tpnet  
這篇文章主要介紹了Android AOP之注解處理解釋器詳解(二)的相關(guān)資料,需要的朋友可以參考下

Android APO 注解處理解釋器

相關(guān)文章:

Android AOP注解Annotation詳解(一)
Android AOP之注解處理解釋器詳解(二)
Android AOP 注解詳解及簡(jiǎn)單使用實(shí)例(三)

一、提取Annotation信息

當(dāng)開發(fā)者使用了Annotation修飾了類、方法、Field等成員之后,這些Annotation不會(huì)自己生效,必須由開發(fā)者提供相應(yīng)的代碼來(lái)提取并處理Annotation信息。這些處理提取和處理Annotation的代碼統(tǒng)稱為APT(Annotation Processing Tool)。

JDK主要提供了兩個(gè)類,來(lái)完成Annotation的提?。?/p>

  • 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

簡(jiǎn)單獲取方法

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 啟動(dòng)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)行就會(huì)看到輸出,表示已經(jīng)獲取到了對(duì)應(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é)果

將會(huì)輸出以下內(nèi)容,name和age都可以獲取到。

02-18 16:11:53.493 25662-25662/? E/tag: 方法名:testYes的注解值為天平,20

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

相關(guān)文章

最新評(píng)論