annotation(注解) 解析与实例一

来源:互联网 发布:手机看图纸软件 编辑:程序博客网 时间:2024/06/05 05:25

What is annotation?

Annotations, a form of metadata, provide data about a program that is not part of the program itself. Annotations have no direct effect on the operation of the code they annotate.

常见用途

Information for the compiler — Annotations can be used by the compiler to detect errors or suppress warnings.
比如我们常见的@Override,@SuppressWarnings编译器通过注解,去检测错误或者抑制warning.

Compile-time and deployment-time processing — Software tools can process annotation information to generate code, XML files, and so forth.
编译时部署时通过工具处理注解,以方便生产代码。

Runtime processing — Some annotations are available to be examined at runtime.
运行时检测注解

具体使用

Compiler 检测@得知它后边跟了注解。

  • 注解可以跟元素elements,元素(name-value形式)。
  • 如果注解中仅有一个元素,则可以直接写value。@SuppressWarnings("unchecked")
  • 如果注解中没有元素,则可以直接省略。@EBook

  • 有重复类型的注解,被称为重复注解(Repeating annotations) ,jdk1.8引入。

@Author(name = "Jane Doe")@Author(name = "John Smith")

示例 :

@Author(   name = "Benjamin Franklin",   date = "3/27/2003",)class MyClass() { ... }@SuppressWarnings("unchecked")void myMethod() { ... }@Author(name = "Jane Doe")@EBookclass MyClass { ... }@Author(name = "Jane Doe")@Author(name = "John Smith")class MyClass { ... }
  • 对于注解的类型,都是在java.lang.annotation中定义,预定义的注解类型有:
    java.lang.Deprecated
    (implements java.lang.annotation.Annotation): 该方法(或类、接口等等)弃用。(使用时,注意javadoc中用 @deprecated)
public class Snippet {     // Javadoc comment follows    /**     * @deprecated explanation of why it was deprecated     */    @Deprecated    static void deprecatedMethod() {    }}

java.lang.@Override (implements java.lang.annotation.Annotation)
编译器通过这个注释去检测重写的正确性。

java.lang.@SuppressWarnings (implements java.lang.annotation.Annotation)用于抑制代码中提示的warning信息。

 // use a deprecated method and tell    // compiler not to generate a warning   @SuppressWarnings("deprecation")    void useDeprecatedMethod() {        // deprecation warning        // - suppressed        objectOne.deprecatedMethod();    }

java.lang.@SafeVarargs (implements java.lang.annotation.Annotation)
用于抑制可变参数使用报出的warning信息.

java.lang.FunctionalInterface (implements java.lang.annotation.Annotation)
jdk1.8引入,声明功能性接口。

元注解:用于声明其他注解的注解
java.lang.annotation.@Retention:表示如何保留被标识的注解:

  • RetentionPolicy.SOURCE – The marked annotation is retained only in the source level and is ignored by the compiler.
    源码级别注释保留,被 java compiler 忽略编译。
  • RetentionPolicy.CLASS – The marked annotation is retained by the compiler at compile time, but is ignored by the Java Virtual Machine (JVM).
    java compiler 编译时保留注解。被JVM忽略。
  • RetentionPolicy.RUNTIME – The marked annotation is retained by the JVM so it can be used by the runtime environment.
    运行时保留该注解,被JVM保留。

java.lang.annotation.@Documented
通过javadoc tool 处理该注释,仅作为文档。

java.lang.annotation.@Target
用来表示被标识的注解用于指定的类型。

  • ElementType.ANNOTATION_TYPE can be applied to an annotation type.
  • ElementType.CONSTRUCTOR can be applied to a constructor.
  • ElementType.FIELD can be applied to a field or property.
  • ElementType.LOCAL_VARIABLE can be applied to a local variable.
  • ElementType.METHOD can be applied to a method-level annotation.
  • ElementType.PACKAGE can be applied to a package declaration.
  • ElementType.PARAMETER can be applied to the parameters of a method.
  • ElementType.TYPE can be applied to any element of a class.

java.lang.annotation@Inherited
表示这个注解可以继承超类注解。注意:这个注解只能用来声明类
示例:

package inherited.test;import java.lang.annotation.ElementType;import java.lang.annotation.Inherited;import java.lang.annotation.Retention;import java.lang.annotation.RetentionPolicy;import java.lang.annotation.Target;@Inherited@Target(ElementType.TYPE)@Retention(RetentionPolicy.RUNTIME)public @interface InheritedAnnotationType {}
 package inherited.test;import java.lang.annotation.ElementType;import java.lang.annotation.Retention;import java.lang.annotation.RetentionPolicy;import java.lang.annotation.Target;@Target(ElementType.TYPE)@Retention(RetentionPolicy.RUNTIME)public @interface UninheritedAnnotationType {}package inherited.test;@UninheritedAnnotationTypeclass A {}package inherited.test;@InheritedAnnotationTypeclass B extends A {}package inherited.test;class C extends B {}
/** *  */package inherited.test;public class Test {    public static void main(String[] args) {    System.out.println(new A().getClass().getAnnotation(InheritedAnnotationType.class));    System.out.println(new B().getClass().getAnnotation(InheritedAnnotationType.class));    System.out.println(new C().getClass().getAnnotation(InheritedAnnotationType.class));    System.out.println("_________________________________");    System.out.println(new A().getClass().getAnnotation(UninheritedAnnotationType.class));    System.out.println(new B().getClass().getAnnotation(UninheritedAnnotationType.class));    System.out.println(new C().getClass().getAnnotation(UninheritedAnnotationType.class));}}

output:

null@inherited.test.InheritedAnnotationType()@inherited.test.InheritedAnnotationType()_________________________________@inherited.test.UninheritedAnnotationType()nullnull
1 0
原创粉丝点击