What are annotations

  • Annotation is a new technology introduced starting with JDK5.0.

  • An Annotation does:

    • Not the program itself, but the program itself.(This is no different from comments.)

    • Can be read by other programs (e.g. compilers, etc.).

  • Annotation format:

    • Comments exist in the code as @comment names, and you can also add parameter values. For example: @ SuppressWarnings (value = “unchecked”).

  • Where are annotations used?

    • Can be attached in the package, class, method, field, such as equivalent to add additional auxiliary information for them.

    • Access to this metadata can be programmed through reflection mechanisms.

Built-in annotations

  • Override: Defined in java.lang.Override, this annotation applies only to modifying methods, indicating that a method declaration intends to Override another method declaration in the parent (superclass).

  • Deprecated: Defined in java.lang.Deprecated, this annotation can be used for rhetoric, attributes, or classes to indicate that programmers are discouraged from using such an element, often because it is dangerous or because a better alternative exists.

  • @ SuppressWarnings: defined in Java. Lang. In SuppressWarnings, used to suppress the warning at compile time.

  • [] ### unlike the first two comments, you need to add a parameter to use them correctly. These parameters are already defined, so we can use them selectively.

    • @SuppressWarnings(“all”)

    • @SuppressWarnings(“unchecked”)

    • @SuppressWarnings(value={“unchecked”,”deprecation”})

    • And so on…

    package com.chao.annotation;
    import java.util.ArrayList;
    import java.util.List;
    // What are annotations
    public class Test01 extends Object{
        // @override Override annotation
        @Override
        public String toString(a) {
            return super.toString();
        }
        Deprecated is not recommended for programmers, but it can be Deprecated, or there is a better way
        @Deprecated
        public static void test(a){
            System.out.println("Deprecated");
        }
        @SuppressWarnings("all")
        public void test02(a){
            List list = new ArrayList();
        }
        public static void main(String[] args) { test(); }}Copy the code

Yuan notes

  • Meta-annotations are responsible for annotating other annotations. Java defines four standard meta-annotation types that provide annotations for other annotation types.

  • These types and the classes they support are found in the Java.lang. Annotation package (@target,@Retention,@Documented,@Inherited).

    • @target: Used to describe the scope of the annotation (i.e. where the described annotation can be used).

    • @Retention: Indicates the level at which annotation information is required to be stored and is used to describe the annotation’s life cycle.

      • (RUNTIME > CLASS > SOURCE)
    • Documented: This annotation will be included in javadoc.

    • @Inherited: Indicates that a child class can inherit the annotation in its parent class.

    package com.chao.annotation;
    import java.lang.annotation.*;
    // Test meta annotations
    @MyAnnotation
    public class Test02 {
        public void test(a){}}// Define an annotation
    //Target indicates where our annotations can be used.
    @Target(value = {ElementType.METHOD,ElementType.TYPE})
    //Retention indicates where our annotations are still valid.
    //runtime>class>sources
    @Retention(value = RetentionPolicy.RUNTIME)
    //Documented indicates whether our annotations are generated in Javadoc
    @Documented
    //Inherited Subclasses can inherit annotations from their parent
    @Inherited
    @interface MyAnnotation{
    }
    Copy the code

Custom annotations

  • When using the @ interface custom annotations, automatic inherited Java. Lang. The annotation. The annotation interface.

  • Analysis:

    • @interface is used to declare an annotation in the format: public @interface Annotation name (define content).

    • Each of these methods actually declares a configuration parameter.

    • The name of the method is the name of the parameter.

    • The return value type is the type of the parameter (the return value can only be the basic type,Class,String,enum).

    • You can declare default values for parameters with default.

    • If there is only one parameter member, the general parameter name is value.

    • An annotation element must have a value, and when we define an annotation element, we often use an empty string, 0, as the default.

package com.chao.annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
// Custom annotations
public class Test03 {
    // Annotations can display assignments. If there is no default value, we must assign the annotation
    @Myannotation2 (Age = 18,name = "Wang Chao ")
    public void test(a){}
    @ MyAnnotation3 (" wang ")
    public void test2(a){}}@Target({ElementType.TYPE,ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@interface MyAnnotation2{
    // Annotated arguments: parameter type + parameter name ();
    String name(a) default "";
    int age(a);
    int id(a) default- 1;// If the default value is -1, it does not exist
    String[] schools() default {"Northwest University, West China University of Technology, Xi 'an Polytechnic University"};
}
@Target({ElementType.TYPE,ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@interface MyAnnotation3{
    String value(a);
}
Copy the code