Java Annotation and custom Annotation

Annotation and custom annotation

This article is only used for personal review, Annotation and user-defined Annotation knowledge points.
Most of the content of this article is combined with the CSDN blogger's "program yuan bubble". If you like the content of this article, please go to the original author page to support the original author
Original link: https://blog.csdn.net/weixin_43884234/article/details/115055781

1. About notes

Annotations can enhance our Java code, and reflection technology can expand and implement many functions. They are widely used at the bottom of the three frameworks.

Now the mainstream development is based on annotation. With less code, the framework can automatically generate a lot of code according to annotations, so as to reduce the amount of code and make the program easier to read. For example, the most popular spring boot is implemented entirely based on annotation technology.

Moreover, the annotation technology is very exquisite. In the beginner stage, you will think that annotation is redundant and garbage. But after being proficient, you will sigh that it can surpass the functions of Java code and make java code powerful in an instant.

2. Annotation classification

Annotations fall into three categories

  • JDK's own annotation
  • Meta annotation
  • Custom annotation
2.1 JDK notes

JDK annotations, 5:

@Override: used to identify the override method
@Deprecated: indicates that this method is obsolete. (however, it can still be used normally)
@SuppressWarnings("deprecation") ignore warnings
@SafeVarargs JDK1.7, heap pollution, not commonly used
@Functional interface JDK1.8 appears, which is not commonly used in conjunction with Lambda expressions in functional programming
2.2 yuan notes

Annotations used to describe annotations, 5

@Where are Target annotations used: on classes, methods, properties, and so on

@Declaration cycle of Retention annotation: in source file, bytecode file and running

@Inherited allows child annotation inheritance

@Documented contains annotations when generating javadoc, which is not commonly used

@Repeatable annotation is a repeatable type annotation, which can be reused in the same place. It is not commonly used.

2.2.1 @Target ElementType...

Describes where the annotation exists

ElementType.TYPE applies to the elements of the class
ElementType.METHOD applies to the method level
ElementType.FIELD applies to a field or property (member variable)
ElementType.ANNOTATION_TYPE applies to annotation types
ElementType.CONSTRUCTOR applies to constructors
ElementType.LOCAL_VARIABLE applies to local variables
ElementType.PACKAGE applies to package declarations
ElementType.PARAMETER the parameter applied to the method

2.2.2 @Retention RetentionPolicy...

This annotation defines how long custom annotations are retained. For example, some annotations only appear in the source code and are discarded by the compiler; Others are compiled in class files; Annotations compiled in the class file may be ignored by the virtual machine, while others will be read when the class is loaded.

Why do you want to divide the bytecode file into two parts? If not, the reflection technology will not be available, so it will not be able to identify and process. There are three values:

SOURCE is valid in the SOURCE file (i.e. SOURCE file retention)
class is valid in the class file (i.e. class is reserved)
RUNTIME is valid at RUNTIME (i.e. RUNTIME retention)
2.3 user defined annotation

The syntax of annotations is different from that of conventional java

Create package: com. Custom annotation. Annotation
Create class: AnnotationTest.java

package replay .annotation;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/*This class is used to complete custom annotations*/
public class TestAnnotation {
}
//2. Mark the usage position of user-defined annotation through @ Target annotation
/**3.Specify where custom annotations can be used through meta annotation @ Target
 * We use "ElementType. Static constant" to specify where custom annotations can be added
 * Moreover, values can be written in multiple formats: @ Target({ElementType.XXX,ElementType.XXX}
 */
@Target({ElementType.METHOD,ElementType.TYPE})//Can be added to methods & classes

//3. Mark the life cycle of user-defined annotation through @ Retention annotation
/**4.Customize the annotation lifecycle through the meta annotation @ Retention rule
 * We use "RetentionPolicy. Static constant" to specify the life cycle of custom annotations
 * Note: only one value can be written: select 1 from SOURCE CLASS RUNTIME 3
 */

@Retention(RetentionPolicy.RUNTIME)//Valid until runtime
//1. Define custom annotation
/** 1.First of all, note that the syntax of annotation definition is different from Java
 * 2.Define the format of custom annotation: @ interface annotation name
 */

@interface Rice{
    //5. We can enhance the annotation -- add the attribute of the annotation
    /*5.Note: int age(); is not the definition of the method, but adds an age attribute to the custom annotation*/
    //int age(); / / add a common attribute age to the custom annotation. The type is int
    int age() default 0;//Assign a default value of 0 to the common attribute of the custom annotation
    /*6.You can also add a special attribute value to the annotation
     * Special attributes are defined in the same way as ordinary attributes, mainly in different ways
     * Note: the name of a special attribute must be value, but the type is not limited
     * Special attributes can also be given default values. The format is the same as that of ordinary attributes and cannot be abbreviated
     * */
    //String value(); / / define a special attribute value of type string
    String value() default "Lemon";//Define special attributes and give them default values
}

//4. Define a class to test custom annotations
//@Rice
class TestAnno{
    /*Test 1: add Rice annotation to the name attribute and eat method of TestAnno class respectively
     * Conclusion: an error is reported in the annotation on the attribute, indicating where the user-defined annotation can be added, which is determined by @ Target*/
    //@Rice / / error reported
    String name;
    /*Test 2: after we added an age attribute to the Rice annotation, the @ Rice annotation directly reported an error when used
     * Conclusion: when the annotation does not define attributes, it can be used directly
     *      After the attribute is defined in the annotation, the attribute must be assigned a value. Format: @ Rice(age = 10)*/
    /*Test 3: after giving the default value to the age attribute, you can directly use the @ Rice annotation
     * There is no need to assign a value to the age attribute, because the age attribute already has a default value of 0*/
    /*Test 4: after adding the special attribute value to Rice annotation, the attribute must be assigned a value
     * However, special attribute assignment can be abbreviated as @ Rice("Apple")
     * Test 5: if a special attribute is also given a default value, you can use this annotation directly
     * If you want to assign values to all attributes of an annotation, each assignment cannot be abbreviated*/
    @Rice(age=10,value="orange")
    //@Rice("Apple")
    //@Rice(age = 10)
    //@Rice(10) / / an error is reported. It cannot be abbreviated. Common attributes do not have this format
    public void eat(){
        System.out.println("I'm not active in cooking and have problems with my mind");
    }
}

Tags: Java

Posted on Sun, 28 Nov 2021 08:01:05 -0500 by alwaysinit