注解Annotation

        注解是一种形式的元数据,其向程序提供数据,但该数据不属于程序的一部分。并且注解对其注解的程序代码的执行不产生直接的影响。

注解主要有一下几种作用

Information for the compiler —编译器可使用注解去侦测错误和关闭一些警告。如:override, Suppresswarnnings.

Compile-time and deployment-time processing — 一些软件工具可以使用注解生成XML文档等。如Document.

Runtime processing — 有些注解在运行时可以用于检查,比如MyBatis中通过注解来复制对象。


如何定义一个注解?


       

JAVA SE已经定义好的注解:分为两类一类是java语言使用的,还有一类是作用于其他注解的注解。

用于java语言的注解:

Deprecated

A program element annotated @Deprecated is one that programmers are discouraged from using, typically because it is dangerous, or because a better alternative exists. Compilers warn when a deprecated program element is used or overridden in non-deprecated code.


FunctionalInterface

An informative annotation type used to indicate that an interface type declaration is intended to be a functional interface as defined by the Java Language Specification. Conceptually, a functional interface has exactly one abstract method. Since default methods have an implementation,they are not abstract. If an interface declares an abstract method overriding one of the public methods of java.lang.Object, that also does not count toward the interface's abstract method count since any implementation of the interface will have an implementation from java.lang.Object or elsewhere.

Note that instances of functional interfaces can be created with lambda expressions, method references, or constructor references.

If a type is annotated with this annotation type, compilers are required to generate an error message unless:

  • The type is an interface type and not an annotation type, enum, or class.
  • The annotated type satisfies the requirements of a functional interface.

However,the compiler will treat any interface meeting the definition of a functional interface as a functional interface regardless of whether or not a FunctionalInterface annotation is present on the interface declaration.

 

Override

Indicates that a method declaration is intended to override a method declaration in a super type. If a method is annotated with this annotation type compilers are required to generate an error message unless at least one of the following conditions hold:

The method does override or implement a method declared in a super type.

The method has a signature that is override-equivalent to that of any public method declared in Object.  


SafeVarargs

http://book.51cto.com/art/201205/339154.htm


SuppressWarnings

Indicates that the named compiler warnings should be suppressed in the annotated element (and in all program elements contained in the annotated element). Note that the set of warnings suppressed in a given element is a super set of the warnings suppressed in all containing elements.For example, if you annotate a class to suppress one warning and annotate a method to suppress another, both warnings will be suppressed in the method.

As a matter of style, programmers should always use this annotation on the most deeply nested element where it is effective. If you want to suppress a warning in a particular method, you should annotate that method rather than its class.


用于注解的注解:

Documented

Indicates that annotations with a type are tobe documented by javadoc and similar tools by default. This type should be usedto annotate the declarations of types whose annotations affect the use ofannotated elements by their clients. If a type declaration is annotated withDocumented, its annotations become part of the public API of the annotatedelements.


Inherited

Indicates that anannotation type is automatically inherited. If an Inherited meta-annotation ispresent on an annotation type declaration, and the user queries the annotationtype on a class declaration, and the class declaration has no annotation forthis type, then the class's superclass will automatically be queried for theannotation type. This process will be repeated until an annotation for thistype is found, or the top of the class hierarchy (Object) is reached. If nosuperclass has an annotation for this type, then the query will indicate thatthe class in question has no such annotation.

Note that this meta-annotation type has no effect if the annotated type isused to annotate anything other than a class. Note also that thismeta-annotation only causes annotations to be inherited from superclasses;annotations on implemented interfaces have no effect.

 

Native

Indicates that a field defining a constant value may bereferenced from native code. The annotation may be used as a hint by tools thatgenerate native header files to determine whether a header file is required,and if so, what declarations it should contain.


Repeatable

The annotation type java.lang.annotation.Repeatable is used to indicate that the annotation type whosedeclaration it (meta-)annotates is repeatable. The value of @Repeatable indicates the containing annotation type for therepeatable annotation type.


Retention

Indicates how longannotations with the annotated type are to be retained. If no Retentionannotation is present on an annotation type declaration, the retention policydefaults to RetentionPolicy.CLASS.

A Retention meta-annotation has effect only if the meta-annotated type isused directly for annotation. It has no effect if the meta-annotated type is used as a member type in another annotation type.

 

Target

Indicates the contextsin which an annotation type is applicable. The declaration contexts and typecontexts in which an annotation type may be applicable are specified in JLS9.6.4.1, and denoted in source code by enum constants of java.lang.annotation.ElementType.

n  If an @Target meta-annotation is not present onan annotation type T , then an annotation of type T may be written as a modifier for any declaration except a typeparameter declaration.

If an @Target meta-annotationis present, the compiler will enforce the usage restrictions indicated by ElementType enum constants, in line with JLS 9.7.4.

For example, this @Target meta-annotationindicates that the declared type is itself a meta-annotation type. It can onlybe used on annotation type declarations:

    @Target(ElementType.ANNOTATION_TYPE)

    public @interface MetaAnnotationType {

        ...

    }

 

This @Target meta-annotationindicates that the declared type is intended solely for use as a member type incomplex annotation type declarations. It cannot be used to annotate anythingdirectly:

    @Target({})

    public @interface MemberType {

        ...

    }

 

It is a compile-time error for a single ElementType constant to appear more than once in an @Target annotation.For example, the following @Target meta-annotationis illegal:

    @Target({ElementType.FIELD,ElementType.METHOD, ElementType.FIELD})

    public @interface Bogus {

        ...

    }

类型注解与可插式的类型系统

可重复的注解


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值