@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface MyTest2 {
}
public class AnnotationTest2 {
@MyTest2
public void test1(){
System.out.println("====test1====");
}
public void test2(){
System.out.println("====test2====");
}
@MyTest2
public void test3(){
System.out.println("====test3====");
}
public void test4(){
System.out.println("====test4====");
}
public static void main(String[] args) throws InvocationTargetException, IllegalAccessException {
final AnnotationTest2 annotationTest2 = new AnnotationTest2();
//得到class对象
final Class<AnnotationTest2> annotationTest2Class = AnnotationTest2.class;
//获得全部成员方法
final Method[] declaredMethods = annotationTest2Class.getDeclaredMethods();
for (Method declaredMethod : declaredMethods) {
if (declaredMethod.isAnnotationPresent(MyTest2.class)) {
//存在@MyTest2则触发执行
declaredMethod.invoke(annotationTest2);
}
}
}
}