Learning java annotated notes

Xml annotations and belong to two different configuration modes, annotation may provide greater convenience, ease of maintenance changes, but high coupling, with respect to the XML comment is reversed.

Nature is an inherited annotation interface Annotation interface. In this regard, you can go to decompile any of the annotation class, you will get results.

A note on accurate sense, but it is a special comment, if it does not resolve the code as it may not even comment.

Analytical methods or a class tend to have two forms of annotation, a direct compilation of scanning, and a running of reflection. Be what we will say that the reflected, while scanning the compiler compiler refers to a class or method has been modified to process the java code compiled bytecode some notes will be detected, then it will be these annotations some processing.

"Meta-annotation" is used to modify the annotated notes, usually in the definition annotation

JAVA has several "meta-annotation":

  • @Target: the role of the target annotation
  • @Retention: Notes of Life Cycle
  • @Documented: whether the notes should be included in the JavaDoc documentation
  • @Inherited: whether to allow subclass inherits the comment

Which, @ Target ( value = { ElementType .field}), who is used to indicate the target @Target modified annotated ultimately the role that is specified in your comment in the end is used to modify the method? Modified class? Or used to modify field properties.

This annotated modified @Target comment will only act on the member field, it can not be used to modify the method or class. Which, ElementType is an enumeration type, there are some of the following values:

  • Allow comments on a modified role in the classes, interfaces, and enumerations: ElementType.TYPE
  • ElementType.FIELD: allowed to act on the attribute field
  • ElementType.METHOD: allowed to act on the method of
  • ElementType.PARAMETER: allowed to act on the method parameters
  • ElementType.CONSTRUCTOR: allowed to act on the constructor
  • ElementType.LOCAL_VARIABLE: allowed to act on the local local variables
  • ElementType.ANNOTATION_TYPE: allowed to act on the notes
  • ElementType.PACKAGE: allowed to act on the cladding

@ Retention ( value = RetentionPolicy.RUNTIME, RetentionPolicy still is an enumerated type, it has the following desirable enumeration values:

    • RetentionPolicy.SOURCE: current annotation compile seen, does not write class files

    • RetentionPolicy.CLASS: class loading phase discarded, will write class files
    • RetentionPolicy.RUNTIME: permanent preservation, can obtain reflection

 

@Retention annotation specifies that the annotated modified life cycle, only one is visible at compile time, it will be discarded after compiling a file will be compiled into class in the compiler, either class or method, and even field they are all property sheet, and JAVA virtual machine also defines several annotations attribute table for storing annotation information, but this can not be brought visibility area method, will be discarded when the class is loaded, the last one is visibility permanent.

@Documented comment modified comment, when we execute JavaDoc documentation package is saved into doc documents, on the contrary will be discarded when packaging. @Inherited annotation notes is a modified inheritable, also notes that we modify a class, and that class subclass will automatically inherit the parent class notes.

to sum up

  1. If the notes are difficult to understand, you put it similar to the label, the label in order to explain things, annotated to explain the code.
  2. The basic syntax of annotations, create like interface, but more than a @ symbol.
  3. Meta-annotation annotations.
  4. Property annotations.
  5. The main comment to the compiler and tool type of software used.
  6. Extract annotation need the help of Java technology reflection, reflecting slower, so also need to be careful when using annotations time care costs.

 

Guess you like

Origin www.cnblogs.com/zhuriblog/p/11567945.html