第十八节:第七部分:java高级:注解的应用场景:模拟junit框架

发布于:2025-07-22 ⋅ 阅读:(13) ⋅ 点赞:(0)

什么是注解的解析以及如何解析注解

什么是注解的解析以及如何解析注解

代码:

解析注解的案例

解析注解的案例

代码:

MyTest2(注解)

package com.itheima.day10_annotation;

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

@Target({ElementType.TYPE,ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface MyTest2 {
    String value();
    double aaa() default 100;
    String[] bbb();
}

Demo

package com.itheima.day10_annotation;
@MyTest2(value = "蜘蛛精",aaa = 99.5,bbb = {"至尊宝","黑马"})
public class Demo {
    @MyTest2(value = "孙悟空",aaa = 99.9,bbb = {"紫霞","牛夫人"})
    public void test1(){

    }
}

AnnotationTest2(主程序)

package com.itheima.day10_annotation;

import org.junit.Test;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.Arrays;

/*
* 目标:掌握注解的解析(解析类上的注解、解析方法上的注解)。
* */
public class AnnotationTest2 {
    @Test
    public void parseClass() {
        // 1、先得到Class对象
        Class c = Demo.class;
        // 2、解析类上的注解
        // 判断类上是否包含了某个注解
        if (c.isAnnotationPresent(MyTest2.class)) {
            MyTest2 mytest2 =
                    (MyTest2) c.getDeclaredAnnotation(MyTest2.class);
            System.out.println(mytest2.value());
            System.out.println(mytest2.aaa());
            System.out.println(Arrays.toString(mytest2.bbb()));

        }

    }

    @Test
    public void parseMethod() throws Exception {
        // 1、先得到Class对象
        Class c = Demo.class;
        Method m = c.getDeclaredMethod("test1");
        //解析方法上的注解
        // 判断方法对象上是否包含了某个注解
        if (m.isAnnotationPresent(MyTest2.class)) {
            MyTest2 mytest2 =
                    (MyTest2) m.getDeclaredAnnotation(MyTest2.class);
            System.out.println(mytest2.value());
            System.out.println(mytest2.aaa());
            System.out.println(Arrays.toString(mytest2.bbb()));

        }
    }
}

结果1

模拟Junit框架的案例

模拟Junit框架

MyTest(注解)

package com.itheima.day10_annotation;

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

@Target(ElementType.METHOD)// 注解只能注解方法。
@Retention(RetentionPolicy.RUNTIME)//让当前注解可以一直存活着。
public @interface MyTest {
    
}

AnnotationTest3(主程序)

package com.itheima.day10_annotation;

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

/*
* 目标:模拟Junit框架的设计。
* */
public class AnnotationTest3 {
    //@MyTest
    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 Exception {
        AnnotationTest3 a = new AnnotationTest3();
        //启动程序!
        // 1、得到Class对象
        Class c = AnnotationTest3.class;
        //2、提取这个类中的全部成员方法
        Method[] methods = c.getDeclaredMethods();
        // 3、遍历这个数组中的每个方法,看方法上是否存在@MyTest注解,存在
        //  触发该方法执行。
        for (Method method : methods) {
            if (method.isAnnotationPresent(MyTest.class)) {
                //说明当前方法上是存在@MyTest,触发当前方法执行
                method.invoke(a);
            }
        }

    }
}

结果


网站公告

今日签到

点亮在社区的每一天
去签到