annotation(注解) 解析与实例一

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")
@EBook
class 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;
@UninheritedAnnotationType
class A {

}



package inherited.test;
@InheritedAnnotationType
class 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()
null
null
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值