Write your own annotation processor, little chestnut
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(ListuseCases,Class<?> cl){
for(Method m:cl.getDeclaredMethods()){//反射
Test t=m.getAnnotation(Test.class);//反射
if(t!=null){
Sy stem.out.println("found use case:"+t.id()+" "+t.description());
useCases.remove(new Integer(t.id()));//?????
}
}
for(int i:useCases){
Sy stem.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