加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

编写自己的注解处理器小栗子

发布时间:2020-12-14 06:38:08 所属栏目:Java 来源:网络整理
导读:1、代码 import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; import java.lang.reflect.Method; import java.util.ArrayList; import ja
1、代码
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**

  • Created by cxh on 17/3/18.
  • 自己写注解处理器
    */

//编写自定义注解
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@interface Test{
public int id();
public String description() default "no descrition >>>";
}
//应用自定义注解
class PasswordUtils{
@Test(id=23,description = "pass1")
public boolean pass11(){
System.out.println("this is pass1");
return true;
}
@Test(id=22,description = "pass2")
public String pass2(){
System.out.println("this is pass2");
return "pass2";
}
@Test(id=21)
public int pass3(){
System.out.println("this is pass3");
return 1;
}
}
//如果没有一个可以读取注解的工具,那么注解就不会比注释更有用.使用注解的过程中,很重要的一个部分就是:创建与使用注解处理器.
//注解处理器
public class UseCaseTracker {
public static void trackUseCases(List useCases,Class<?> cl){
for(Method m:cl.getDeclaredMethods()){//反射
Test t=m.getAnnotation(Test.class);//反射
if(t!=null){
System.out.println("found use case:"+t.id()+" "+t.description());
useCases.remove(new Integer(t.id()));//?????
}
}
for(int i:useCases){
System.out.println("warning :missing use case:"+i);
}
}

public static void main(String[] args) {
    List<Integer> list=new ArrayList<Integer>();
    Collections.addAll(list,24,23,22,21);
    trackUseCases(list,PasswordUtils.class);
}

}

2、运行结果:
found use case:23 pass1
found use case:22 pass2
found use case:21 no descrition >>>
warning :missing use case:24

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读