当前位置: 移动技术网 > IT编程>移动开发>Android > Android AOP之注解处理解释器详解(二)

Android AOP之注解处理解释器详解(二)

2019年07月24日  | 移动技术网IT编程  | 我要评论

汝州市人民政府,鱼游到了纸上ppt,无敌

android apo 注解处理解释器

相关文章:

android aop注解annotation详解(一)
android aop之注解处理解释器详解(二)
android aop 注解详解及简单使用实例(三)

一、提取annotation信息

当开发者使用了annotation修饰了类、方法、field等成员之后,这些annotation不会自己生效,必须由开发者提供相应的代码来提取并处理annotation信息。这些处理提取和处理annotation的代码统称为apt(annotation processing tool)。

jdk主要提供了两个类,来完成annotation的提取:

  • 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,元素使用了多个注解的时候,可以进行输出判断
 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) //运行时可以获取
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);

  //调用解析器
  mytagparser.process(this);

 }

 @mytag
 public void testyes(){

 }

 public void testno(){

 }

}

2.4 结果

运行就会看到输出,表示已经获取到了对应的实例

......
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 结果

将会输出以下内容,name和age都可以获取到。

02-18 16:11:53.493 25662-25662/? e/tag: 方法名:testyes的注解值为天平,20

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网