Introduction to JAVA annotation

  • 2020-04-01 02:11:47
  • OfStack

One, the most common annotation
The & # 8226; Override: overrides a method to tell someone that the method overrides the superclass
The & # 8226; Deprecated: used to advise people not to use the old API, it is used to generate warnings when compiling, can be set on all the elements in the program.
The & # 8226; SuppressWarnings: temporarily close some warning messages
The & # 8226; @entity: indicates that the class is persistable

Two, design your own Annotation
Look at the code before you speak
1. Annotation implementation with only one parameter


package chb.test.annotation;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface MyAnnotation1 {
        String value();
}

2. Annotation implementation with two parameters

package chb.test.annotation;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface MyAnnotation2 {
        String description();
        boolean isAnnotation();
}

3. Annotation experimental class

package chb.test.annotation;
@MyAnnotation1("this is annotation1")
public class AnnotationDemo {
        @MyAnnotation2(description="this is annotation2",isAnnotation=true)
        public void sayHello(){
                System.out.println("hello world!");
        }
}

4.Annotation test specification class

package chb.test.annotation;
import java.lang.reflect.Method;
import org.junit.Test;
public class TestAnnotation {
        @Test
        public void test() throws ClassNotFoundException, SecurityException, NoSuchMethodException{
                Class<?> cls = Class.forName("chb.test.annotation.AnnotationDemo");
                boolean flag = cls.isAnnotationPresent(MyAnnotation1.class);
                if(flag){
                        System.out.println(" Judge class is annotation");
                        MyAnnotation1 annotation1 = cls.getAnnotation(MyAnnotation1.class);
                        System.out.println(annotation1.value());
                }

                Method method = cls.getMethod("sayHello");
                flag = method.isAnnotationPresent(MyAnnotation2.class) ;
                if(flag){
                        System.out.println(" The judgment method is also annotation");
                        MyAnnotation2 annotation2 = method.getAnnotation(MyAnnotation2.class);
                        System.out.println(annotation2.description()+"/t"+annotation2.isAnnotation());
                }
        }

}

As a result of the experiment, the console printed the following information:

The judgment class is the annotation
This is annotation1
The judgment method is also an annotation
This is annotation2         True,

Iii. Introduction and instructions
1. Myanannotation 1 @target (elementtype.type)
The ElementType in @target is used to specify which elements the Annotation type can be used on. For example:
TYPE, FIELD, METHOD, PARAMETER, CONSTRUCTOR, LOCAL_VARIABLE, and PACKAGE, where TYPE is used on Class,Interface,Enum, and Annotation types.

2. @retention in myanannotation 1 (retentionpolicy.runtime)

RetentionPolicy has three policies, which are:
The & # 8226; SOURCE: this Annotation type of information is only retained in the SOURCE code of the program. If the SOURCE code has been compiled, the Annotation data will disappear and will not be retained in the compiled. Class file

The & # 8226; CLASS: this Annotation type of information is kept in the source code of the program, as well as in the compiled. CLASS file, and is not loaded into the JVM at execution time. Note: the default policy is CLASS type

The & # 8226; RUNTIME: represents the retention of information in the source, compiled. Class file, which is loaded into the JVM during execution

3. @myanannotation 1 in myanannotation 1
The purpose is to display the information for this Annotation on the JAVA API document, and if you don't add @documented, the JAVA API document won't display the relevant Annotation information

4. @interface in myanannotation 1
Keyword that indicates that the class is defined as an Annotation

5. String value() in myanannotation 1;
Indicates that there is a member parameter with the name of value and the access right as the default modifier. Note the following two points:
The & # 8226; Access can only be decorated with public and default
The & # 8226; Parameter members can only use basic types of byte, short, char, int, long, float, double, Boolean eight basic data types, and String, Enum, Class, annotations and other data types, and that some types of arrays

6. @myanannotation 1 in the AnnotationDemo ("this is annotation1")
Because myanannotation 1 has only one parameter, you can directly write the value in parentheses. Note: if the Annotation has only one parameter, it is recommended that the parameter name be defined as value

7. The CLS TestAnnotation. IsAnnotationPresent (MyAnnotation1. Class)
Determines whether the class USES the annotation of myanannotation 1

8. Myanannotation 1 annotation1 in TestAnnotation = cls.getAnnotation(myanannotation 1. Class)
Returns the annotation for myanannotation 1 of this class

9. The method of TestAnnotation. IsAnnotationPresent (MyAnnotation2. Class)
Determine whether the method USES the annotation of myanannotation 2


Related articles: