preface

Starting with JDK5, Java has added support for metadata, also known as annotations. Annotations are somewhat different from annotations and can be understood as special tags in code that can be read and processed at compile, class load, and run time. Annotations allow developers to embed supplementary information in source code without changing the original code and logic. The blogger will introduce Java annotations from the following aspects. I hope you can use annotations in your daily development and feel the charm of annotations!

  1. Notes Brief Introduction
  2. Custom annotations

1 Brief introduction of notes

Annotations are often used when writing Java, for example,

Deprecated content is no longer recommended. @Override can only annotate methods, indicating that the method overrides a method in its parent class. Documented content can appear in Javadoc. @SuppressWarnings Warnings generated by the annotated content, for which the compiler will remain silent.

In addition to these annotations that mark classes and methods, there are four more special meta-annotations. @target@Retention @Documented @Inherited These four annotations are annotations that are specifically used to mark annotation classes.

@Target

Indicate where this annotation is used.

public enum ElementType {
    /** class, interface (including annotation type) or enumeration declaration */
    TYPE,
    /** Field declarations (including enumeration constants) */
    FIELD,
    /** method declaration */
    METHOD,
    /** Parameter declaration */
    PARAMETER,
    /** constructor declaration */
    CONSTRUCTOR,
    /** Local variable declaration */
    LOCAL_VARIABLE,
    /** Annotation type declaration */
    ANNOTATION_TYPE,
    /** package declaration */
    PACKAGE,
    /** * Type parameter declaration **@since1.8 * /
    TYPE_PARAMETER,
    /**
     * Use of a type
     *
     * @since1.8 * /
    TYPE_USE
}
Copy the code

@Retention

How long markup annotations can be retained.

public enum RetentionPolicy {
    /** * comments are discarded by the compiler */
    SOURCE,

    /** * The compiler records comments in the class file, but they do not need to be retained by the virtual machine at runtime. * /
    CLASS,

    /** * is reserved at runtime and can be read by reflection */
    RUNTIME
}
Copy the code

@Documented

This annotation is only used to indicate whether javadoc generation will be recorded.

@Inherited

Annotations decorated by it are inheritable (if a class uses an annotation decorated by @Inherited, its subclasses automatically have the annotation).

2 Custom annotations

With a look at some common annotations, let’s look at how to customize annotations and get the annotation content to use. ===> 1. Define a MyAnnotation annotation that takes a parameter [value]

@Target(ElementType.TYPE) / / class notes
@Retention(RetentionPolicy.RUNTIME) // Valid at runtime
public @interface MyAnnotation {

    String value(a);

}
Copy the code

2. Define a Flower class and use this annotation

@myannotation (" Rose ")
public class Flower {}Copy the code

===> 3. Test to obtain the content of the annotation

public class AnnotationTest {
    public static void main(String[] args) {
        try {
            // Get the Class object of Flower
            Class clazz = Flower.class;
            // Determine if there is a MyAnnotation
            if (clazz.isAnnotationPresent(MyAnnotation.class)) {
                // Get the annotation
                MyAnnotation myAnnotation = (MyAnnotation) clazz.getAnnotation(MyAnnotation.class);
                System.out.println(myAnnotation.value());
            } else {
                System.out.println("MyAnnotation is not configured!"); }}catch(Exception e) { e.printStackTrace(); }}}Copy the code

===> The result is as follows