首页 > 其他分享 >注解-解析注解以及注解案例

注解-解析注解以及注解案例

时间:2022-08-29 11:33:33浏览次数:76  
标签:System public 案例 println 注解 解析 class Check

解析注解

在程序中使用(解析)注解:获取注解中定义的属性值

​   1.获取注解定义的位置的对象(Class,Method,Field)

​   2.获取指定的注解:getAnnotation(Class)

 //其实就是在内存中生成了一个该注解接口的子类实现对象
        /**
         public class ProImpl implements Pro{
            @Override
            public String className() {
                return "com.example.com.StrengThen.annotation.Demo1";
            }

            @Override
            public String methodName() {
                return "show";
            }
        }
         */

3.调用注解中的抽象方法获取配置的属性值

Pro注解类:

@Target({ElementType.TYPE})//可以作用于类上
@Retention(RetentionPolicy.RUNTIME)//保留class字节码文件中
public @interface APro {
    String className();
    String methodName();
}

方法类:

public class ADemo1 {
    public void show(){
        System.out.println("Adem1....show...");
    }
}
public class ADemo2 {
    public void show(){
        System.out.println("Adem2....show...");
    }
}

测试类:

@APro(className = "com.abstract_01.denG_016.DenG_02.ADemo1",methodName = "show")
public class AReflectTest {
    public static void main(String[] args) throws Exception {
        //解析注解
        //获取该类的字节码文件对象
        Class<AReflectTest> re = AReflectTest.class;
        //获取上边的注解对象
        //其实就是在内存中生成了一个该注解接口的子类实现对象
        /**
         public class ProImpl implements Pro{
         @Override
         public String className() {
            return "com.example.com.StrengThen.annotation.Demo1";
        }

         @Override
         public String methodName() {
                return "show";
            }
        }
         */
        //获取上边的注解对象
        APro annotation = re.getAnnotation(APro.class);//其实就是在内存中生成一个该注解接口的子类实现对象
        //获取注解对象中定义的抽象方法,获取返回值
        String className = annotation.className();//获取com.abstract_01.denG_016.DenG_02.ADemo1类
        String methodName = annotation.methodName();//获取com.abstract_01.denG_016.DenG_02类中的抽象方法
        System.out.println(className);
        System.out.println(methodName);

        //3.加载该类进内存
        Class<?> aClass = Class.forName(className);
        //4.创建对象
        Object o = aClass.newInstance();
        //5.获取方法对象
        Method method = aClass.getMethod(methodName);
        //6.执行方法
        method.invoke(o);

    }
}

运行结果:

 

 

 注解案例-简单的测试框架

  简单的测试框架

当主方法执行后,会自动自行被检测的所有方法(加了Check注解的方法),判断方法是否有异常,记录到文件中

计算机类:

public class Calculator {
    @Check
    public void add(){
        System.out.println("1 + 0 ="+(1 + 0));
    }
    @Check
    public void sub(){
        System.out.println("1 - 0 ="+(1 - 0));
    }
    @Check
    public void mul(){
        System.out.println("1 * 0 ="+(1 * 0));
    }
    @Check
    public void div(){
        System.out.println("1 / 0 ="+(1 / 0));
    }

    public void show(){
        System.out.println("永无bug...");
    }
}

Check注解类:

@Target({ElementType.METHOD})//作用于方法上
@Retention(RetentionPolicy.RUNTIME)//前被描述的注解,会保留到class字节码文件中,并被JVM读取到
public @interface Check {

}

测试类:

/**
 * 注解案例-简单的测试框架
 *
 *
 * 当主方法执行后,会自动自行被检测的所有方法(加了Check注解的方法),判断方法是否有异常,记录到文件中
 */
public class TestCheck {
    public static void main(String[] args) throws IOException {
        //1.创建计算机对象
        Calculator calculator = new Calculator();
        //2.获取字节码文件对象
        Class<? extends Calculator> aClass = calculator.getClass();
        //3.获取所有方法
        Method[] methods = aClass.getMethods();
        int number = 0;//出现异常的次数
        BufferedWriter bw = new BufferedWriter(new FileWriter("bug.txt"));
        for (Method method : methods) {
            //4.判断方法上是否有Check注解
            if (method.isAnnotationPresent(Check.class)){//isAnnotationPresent():判断当前方法里有没有指定的注解
                //5.有,执行
                try {
                    method.invoke(calculator);
                } catch (Exception e) {
                    //6.捕获异常

                    //记录到文件中
                    number++;
                    bw.write(method.getName()+"方法出异常了");
                    bw.newLine();
                    bw.write("异常名称"+ e.getCause().getClass().getSimpleName());
                    bw.newLine();
                    bw.write("异常的原因"+e.getCause().getMessage());
                    bw.newLine();
                    System.out.println("-----------------");
                }
            }
        }
        bw.write("本次测试一共出现"+number+"次异常");
        bw.flush();
        bw.close();
    }
}

 

标签:System,public,案例,println,注解,解析,class,Check
From: https://www.cnblogs.com/qihaokuan/p/16634928.html

相关文章