zoukankan      html  css  js  c++  java
  • Java注解

    1. 描述-@interface

    注解是面向编译器和虚拟机的,是一种描述信息,必须有编译器或虚拟机主动解析它,才能发挥作用。
    https://www.zhihu.com/question/47449512/answer/106034220
    http://www.importnew.com/17413.html
    

    1.1 元注解-注解其它注解

    @Documented	指明拥有这个注解的元素可以被javadoc此类的工具文档化.
    @Target		可以注解的程序元素(可多个),该元注解的取值可以为TYPE,METHOD,CONSTRUCTOR,FIELD等(ElementType)
    @Inherited	继承,如果被@Inherited元注解所修饰的某个注解对Parent类进行了修饰,则相当于Child类也被该注解所修饰了。
    @Retention	注解类的存在阶段,需正确,否则无法得到注解信息,取值(RetentionPolicy)
    				SOURCE--编译阶段被移除,不包含在class文件中
    				CLASS--编译时注解,包含在class文件中,但运行时移除
    				RUNTIME--运行时注解,运行时可以访问到
    

    1.2 内建注解

    @Override
    @Deprecated
    @SuppressWarnings	告诉编译器忽略特定的警告信息
    

    2. 示例

    a). 注解方法不能带有参数
    b). 注解方法返回值只能是基本类型或基本类型的数组
    c). 注解方法可以有默认值

    //定义
    @Documented		//元注解
    @Target(ElementType.METHOD)
    @Inherited
    @Retention(RetentionPolicy.RUNTIME)
    public @interface MethodInfo{			//注解使用@interface来标识
    	String author() default 'Pankaj';
    	String date();
    	int revision() default 1;
    	String comments();
    }
    
    //使用
    public class AnnotationExample {
    	@Override
    	@MethodInfo(author = 'Pankaj', comments = 'Main method', date = 'Nov 17 2012', revision = 1)
    	public String toString() {
    	    return 'Overriden toString method';
    	}
    }
    

    3. 注解的解析

    3.1 编译时解析(@Retention=CLASS的)

    a). 自定义一个派生自AbstractProcessor的"注解处理类"
    b). 重写process函数, 返回值表示注解的使用是都正确
    javac编译时指定继承自AbstractProcess的类,会自动调用其process函数。

    @Retention(value= RetentionPolicy.CLASS)
    public @interface ExpAnnotation {
        String author() default "desneo";
        String comments();
    }
    
     */
    @SupportedAnnotationTypes({"test.java.annotationtest.ExpAnnotation"})
    public class ExpAnnotationProcess extends AbstractProcessor {
        public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
            for (TypeElement te : annotations) {
                for (Element e : roundEnv.getElementsAnnotatedWith(te)) {
                    ExpAnnotation mi = e.getAnnotation(ExpAnnotation.class);
                    System.out.println("解析出的comments字段值:"+mi.comments());
                }
            }
            return false;
        }
    }
    
    public class TestClass {
        @ExpAnnotation(comments = "注解方法体测试")
        public void sayHello(String ss){
        }
    }
    
    //运行,需指定processor
     javac -encoding UTF-8 -processor test.java.annotationtest.ExpAnnotationProcess  test/java/annotationtest/*.java
     解析出的comments字段值:注解方法体测试
    

    3.2 运行时解析

    ExpAnnotation.java
    @Retention(value= RetentionPolicy.RUNTIME)
    public @interface ExpAnnotation {
        String author() default "desneo";
        String comments();
    }
    
    TestClass.java
    public class TestClass {
        @ExpAnnotation(comments = "注解方法体测试")
        public void sayHello(String ss){
            System.out.println(ss);
        }
    
        //解析注释
        public static void main(String[] args) {
            Class cls = TestClass.class;
            for( Method method : cls.getMethods() ){
                ExpAnnotation ano = method.getAnnotation(ExpAnnotation.class);
                if( ano != null){
                    System.out.println("comments:"+ano.comments());
                    System.out.println("method Name:" + method.getName());
                }
            }
        }
    }
    
    //运行 java test.java.annotationtest.TestClass
    comments:注解方法体测试
    method Name:sayHello
    
  • 相关阅读:
    KMP算法的Next数组详解(转)
    公开封尘已久的即时通讯源码(转)
    《C语言编写 学生成绩管理系统》
    随想录(从编程语言到库、框架、软件)
    Effective Objective-C 2.0 笔记三(Literal Syntax简写语法)
    Java Swing 探索(一)LayoutManager
    Codeforces Round #FF 446 C. DZY Loves Fibonacci Numbers
    ARM体系结构与编程
    div:给div加滚动栏 div的滚动栏设置
    DS18B20
  • 原文地址:https://www.cnblogs.com/Desneo/p/7256989.html
Copyright © 2011-2022 走看看