Detailed annotations and simple to use Java-

annotation:

Concept: description of the process. To the computer to see

Notes: The text description of the program. To the programmer's

definition:

Annotation (Annotation), also known as metadata. Describe a code level. It is a feature of JDK1.5 and later introduced the classes, interfaces, enumerations are on the same level. It can be declared in front of the packages, classes, fields, methods, local variables, parameters and the like of the method for these elements will be described, the comment.

  • Concept description:

    1. New features after JDK1.5

    2. Description of the process

    3. Use Notes: @ Name annotation

  • Role Category:

    ① written document: a document generated by annotating code identification document generation doc [documentation]

    ② code analysis: analysis of the code by annotating code using reflection [ID]

    ③ compilation checking: by annotating code identified so that the compiler can compile basic checking [Override]

  • JDK predefined number of notes

    • @Override: a method for detecting the labeled annotation whether inherited from the parent class (interface)

    • @Deprecated: This comment marked content, expressed obsolete

    • @SuppressWarnings: suppress warnings

         * 一般传递参数all  @SuppressWarnings("all")
      

Custom annotation

  • format:

       元注解
    
       public @interface 注解名称{
    
           属性列表;
    
       }
    
  • Essence: annotation is essentially a interface that inherits default Annotation Interface

      * public interface MyAnno extends java.lang.annotation.Annotation {}
    
  • Properties: abstract interface methods

    • Claim:

      1. Return Value Type attribute have the following values

        • Basic data types

        • String

        • enumerate

        • annotation

        • More than one type of array

      2. The property is defined when you use need to give the property assignment

        1. If the definition of property, use the default keyword to attribute default initialization value, when using annotations, you can not assign attributes.

        2. If there is only a need to assign attribute, and value is the name of the property, the value may be omitted, directly defined value.

        3. Assigning an array using {value} package. If the array has only one value, it can be omitted {}

Annotations are used to describe annotation: a meta-annotation

  • @Target: annotation can describe the role of location

    • ElementType values:

    • TYPE: can be at class

    • METHOD: the method can be applied to

    • FIELD: can act on the member variables

  • @Retention: Description stage annotations are reserved

       * @Retention(RetentionPolicy.RUNTIME):当前被描述的注解,会保留到class字节码文件中,并被JVM读取到
    
       * @Documented:描述注解是否被抽取到api文档中
    
       * @Inherited:描述注解是否被子类继承
    
  • The program uses (Analysis) Note: getting a property value defined in the annotation

    1. Acquiring location defined annotation object (Class, Method, Field)

    2. Gets the specified comment

		getAnnotation(Class)
		
		//其实就是在内存中生成了一个该注解接口的子类实现对象
		
        	public class ProImpl implements Pro{

                	public String className(){

                        	return "cn.itcast.annotation.Demo1";

                   	}

                   	public String methodName(){

                         return "show";

                    	}

            	}
  1. Abstract method call annotation get property values ​​configured
Released three original articles · won praise 0 · Views 27

Guess you like

Origin blog.csdn.net/loulan_lu/article/details/105014747