package com.itbuluoge.anno; import java.lang.reflect.Method; import java.util.ArrayList; import java.util.Collections; import java.util.List; public class Test { public static void trackUseCases(List<Integer> useCases,Class<?> cl) { for(Method m:cl.getDeclaredMethods()) { UseCase uc=m.getAnnotation(UseCase.class); if(uc!=null) { System.out.println("FOUND:"+uc.id()+" "+uc.description()); } } for(int i:useCases) { System.out.println("Warning.."+i); } } public static void Case(Class<?> cl) { for(Method m:cl.getDeclaredMethods()) { UseCase uc=m.getAnnotation(UseCase.class); if(uc!=null) { System.out.println("FOUND:"+uc.id()+" "+uc.description()); } } } /** * @param args */ public static void main(String[] args) { //List<Integer> useCases=new ArrayList<Integer>(); //Collections.addAll(useCases, 41,42); //trackUseCases(useCases,Testable.class); Case(Testable.class); } }
package com.itbuluoge.anno; import java.lang.annotation.Documented; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; import org.junit.Test; @Target(ElementType.METHOD) @Retention(RetentionPolicy.RUNTIME) @Documented @interface UseCase { public int id(); public String description() default "no des"; } public class Testable { @UseCase(id=19,description="中国人") public void say() { } @UseCase(id=19) public void listen() { } }
输出结果
时间: 2024-12-21 14:14:20