zoukankan      html  css  js  c++  java
  • java--Annotation实现

    Annotation实现
    在java中一共提供了三个annotation:@Override,@Deprecated,@SupperessWarnings
    代码范例:使用Annotation
    package com.hbsi.proxy;
    @SuppressWarnings("unused")
    @Deprecated
    public class RealFood implements Food{
     @SuppressWarnings("unused")
     @Deprecated
     @Override
     public void eat() {
     int x;
     System.out.println("吃饭 ,,,,,RealFood");
     }
    }
    如果想要操作Annotation,那么首先应该可以去的全部的Annotation,如果要想取得Annotation则可以使用Class类中的方法
        • public Annotation[] getAnnotations​() 取得全部的Annotation
    代码示例:去的上面的类上的Annotation
    import java.lang.annotation.Annotation;
    import com.hbsi.proxy.RealFood;
    public class TestDemo {
     public static void main(String[] args) {
     Class<?> cls = RealFood.class;
     Annotation[] annotations = cls.getAnnotations();
     for (int i = 0; i < annotations.length; i++) {
     System.out.println(annotations[i]);
     }
     }
    }
    代码执行结果:
    @java.lang.Deprecated(forRemoval=false, since="")
    奇怪的是这个类上的 @SuppressWarnings("unused")注解并没有输出,因为是由Annotation的使用策略决定的,只有@Deprecated才一直支持到运行时使用。其他只是在编译时使用
    如果想知道Annotation 的级别,比如是运行时一直使用,还是只在编译时使用,则可以考虑这个类:
    其枚举如下
    CLASS
    注释将由编译器记录在类文件中,但VM不需要在运行时保留。类之中
    注释将由编译器记录在类文件中,并由VM在运行时保留,因此可以反射读取。运行之中
    注释将被编译器丢弃。源代码之中
    如上,如果现在要编写一个与程序有关的Annotation,应该使用RUNTIME范围。
     
    代码范例:定义一个简单Annotation
    import java.lang.annotation.Retention;
    import java.lang.annotation.RetentionPolicy;
    //此注解在运行范围内有效
    @Retention(value=RetentionPolicy.RUNTIME)
    public @interface MyAnnotation {
     public String name() default "hbsi";//定义name属性
     public String values();//定义values属性
    }
    而后就可以直接使用此注解:
     
    import com.hbsi.annotation.MyAnnotation;
    @MyAnnotation(name="hbsi",values="hebei")
    public class RealFood implements Food{
     @Override
     public void eat() {
     int x;
     System.out.println("吃饭 ,,,,,RealFood");
     }
    }

     再次运行上述代码中获得Annotation 的类得到的结果如下:

    @com.hbsi.annotation.MyAnnotation(name="hbsi", values="hebei")
    代码范例:
    import java.lang.annotation.Annotation;
    import com.hbsi.annotation.MyAnnotation;
    import com.hbsi.proxy.RealFood;
    public class TestDemo {
     public static void main(String[] args) {
     Class<?> cls = RealFood.class;
     Annotation[] annotations = cls.getAnnotations();
     for (int i = 0; i < annotations.length; i++) {
     MyAnnotation annotation = cls.getAnnotation(MyAnnotation.class);
     System.out.println(annotation.name());
     System.out.println(annotation.values());
     System.out.println(annotation.annotationType());
     }
     }
    }

     执行结果

    hbsi
    hebei
    interface com.hbsi.annotation.MyAnnotation
    从执行结果中可以看到,指定类如果配置了此注解并对注解中属性赋值了,那么是可以获得指定类中的为注解赋的值的信息的。
    如果要定义有Annotation必然要有一个类专门处理Annotation数据的取得。
  • 相关阅读:
    WCF bindings comparison z
    DevExpress打印功能 z
    使用Topshelf 5步创建Windows 服务 z
    Log4net中的RollingFileAppender z
    Log4Net在Windows服务中不能记录日志 z
    dev 注册方法 z
    async callback z
    多窗体之间方法调用 z
    [JS6] 通过用户事件事件执行脚本
    [JS5] 利用onload执行脚本
  • 原文地址:https://www.cnblogs.com/wzqjy/p/7865395.html
Copyright © 2011-2022 走看看