Java annotation development application

1. Annotate conceptual knowledge

注解(Annotation), also called metadata. A code-level description. It is a feature introduced by JDK1.5 and later versions, and it is at the same level as classes, interfaces, and enumerations. It can be declared in front of packages, classes, fields, methods, local variables, method parameters, etc., to describe these elements, comment, and this information is stored in Annotation's "name=value" pair.

2. Basic Annotation

When using Annotation, add a @symbol in front of it, and use the Annotation as a modifier to modify the program elements it supports.

Three basic Annotations:

  • @Override: Qualifies to override the parent class method, this annotation can only be used for methods
  • @Deprecated: used to indicate that a program element (class, method, etc.) is obsolete
  • @SupperessWarnings: suppress compiler warnings

3. Meta annotations

JDK meta-annotations are used to decorate other Annotation definitions.
JDK1.5 provides annotation types specifically on annotations, which are the following four:

  • @Retention
  • @Target
  • @Documented
  • @Inherited

,
@Retentioncan only be used to modify an Annotation definition to specify how long the Annotation can be retained. @Retention contains a member variable of type RetentionPolicy. When using @Rentention, you must specify a value for the value member variable:

  • RetentionPolicy.SOURCE: The compiler discards comments for this policy directly
  • RetentionPolicy.ClASS: The compiler will record the annotation in the class file. When running the java program, the JVM will not retain the annotation, which is the default value.
  • RetentionPolicy.RUNTIME: The compiler will record the annotation in the class file. When running the java program, the JVM will retain the annotation, and the program can obtain the annotation through reflection

@Target, used to modify the Annotation definition, used to specify which program elements the modified Annotation can be used to modify. @Target also contains a member variable called Value. @Target specifies the scope of objects modified by Annotation: Annotation can be used for packages, types (classes, interfaces, enumerations, Annotation types), type members (methods, constructors, member variables, enumeration values), method parameters and Local variables (such as loop variables, catch parameters). The use of target in the declaration of the Annotation type can be more explicit about the target of its modification.
The values ​​(ElementType) are:
1.CONSTRUCTOR: used to describe the constructor
2.FIELD: used to describe the field
3.LOCAL_VARIABLE: used to describe the local variable
4.METHOD: used to describe the method
5.PACKAGE: used to describe the package
6 .PARAMETER: used to describe parameters
7.TYPE: used to describe classes, interfaces (including annotation types) or enum declarations

@Documented, used to specify that the Annotation class modified by this meta Annotation will be extracted into a document by the javadoc tool. Annotations defined as Documented must set the Retention value to RUNTIME.

@Inherited, the Annotation modified by it will have inheritance, if a class uses the Annotation modified by @Inherited, its subclass will automatically have the annotation (in practical application, it is used less).

4. Custom annotations

Using @interfacecustom annotations will automatically inherit the java.lang.annotation.Annotation interface, and the compiler will automatically complete other details. When defining an annotation, other annotations or interfaces cannot be inherited. @interfaceUsed to declare an annotation where each method actually declares a configuration parameter. The name of the method is the name of the parameter, and the return value type is the type of the parameter (the return value can only be the basic type, Class, String, enum), and the default value of the parameter can be declared by default.

Define the annotation format:

public @interface annotation name {definition body}

Example code:

@Target(ElementType.FIELD)//作用范围在属性上
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface MyAnnotation {
    public enum WeekDay{SUN,MON,TUE,WED,THU,FRI,SAT};
    WeekDay wd() default WeekDay.SUN;
    public int id() default -1;
    public String name() default "";
}

Use of custom annotations:

@MyAnnotation(wd = WeekDay.MON)
public int age;

Annotation element default value:
Annotation element must have a definite value, either specified in the default value of the definition annotation, or specified when using the annotation, the value of the annotation element of non-basic type cannot be null. Therefore, it is a common practice to use an empty string or 0 as the default value. This constraint makes it difficult for the processor to express the presence or absence of an element, because in the declaration of each annotation, all elements are present and have corresponding values. In order to circumvent this constraint, we can only define some special Values, such as the empty string or a negative number, indicate that an element does not exist at a time, which has become an idiom when defining annotations.
If no default value is specified in the definition annotation, or when used, no value is specified, the following error will be reported:
annotationException

For more knowledge about annotations, please check the blog: http://www.cnblogs.com/peida/archive/2013/04/24/3036689.html
The blogger wrote in great detail and very good, and picked the blogger part For the content, I hope the blogger will forgive me, and help him promote it and share knowledge. . .

A brain map of the blogger above, very good, please share:
javaAnnotation

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325945056&siteId=291194637