注解的应用:模拟junit框架

package annotation;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
* FileName: AnnotationDemo02
* Author: lps
* Date: 2022/4/23 14:18
* Sign:刘品水 Q:1944900433
*/
public class AnnotationDemo02 {


public void test1() {
System.out.println("====test1====");
}
@MyTest
public void test2() {
System.out.println("====test2====");
}

@MyTest
public void test3() {
System.out.println("====test3====");
}

/**
* 启动菜单:有注解的被调用
*/
public static void main(String[] args) throws InvocationTargetException, IllegalAccessException {
AnnotationDemo02 t = new AnnotationDemo02();
Class<AnnotationDemo02> c = AnnotationDemo02.class;
Method[] methods = c.getDeclaredMethods();
for (Method method : methods) {
if (method.isAnnotationPresent(MyTest.class)){
method.invoke(t);
}
}

}
}
package annotation;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
 * FileName: MyTest
 * Author:   lps
 * Date:     2022/4/23 14:10
 * Sign:刘品水 Q:1944900433
 */
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface MyTest {

}

 

posted @ 2022-04-23 14:43  刘品水  阅读(27)  评论(0编辑  收藏  举报