java Beginners 10 --- Notes

From the JVM's point of view, annotation does not have any impact on the code logic, how to use annotations entirely determined by the tool.

Java annotations can be divided into three categories:

The first class is used by the compiler notes, for example:

  • @Override: Let the compiler checks whether the method is implemented correctly overwritten;
  • @SuppressWarnings: Tells the compiler to ignore the warning code is generated here.

Such notes will not be compiled into the .classfile, they are thrown away in the compiler to compile after.

The second type is a tool for dealing .classannotation files used, for example, some tools when loading the class, the class made for dynamic changes to achieve some special features. Such notes are compiled into the .classfile, but after the end of the load does not exist in memory. Such notes are only used some of the underlying libraries, in general we do not have to deal with their own.

The third category is the comments in the program run can read, they have been present in the JVM after loading, which is the most commonly used comment. For example, one configuration of the @PostConstructmethod is automatically invoked after calling the constructor (which is a function of the code reading annotation Java implementation, the JVM does not recognize the annotation).

java language used to define @interface notes, our custom annotations, be sure to remember to add @Target, according to the situation inside the parameters added, ElementType.METHOD, ElementType.FIELD ...

@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE)
public @interface Target {
    /**
     * Returns an array of the kinds of elements an annotation type
     * can be applied to.
     * @return an array of the kinds of elements an annotation type
     * can be applied to
     */
    ElementType[] value();
}
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE)
public @interface Retention {
/**
* Returns the retention policy.
* @return the retention policy
*/
RetentionPolicy value();
}
 

@Retention wish this annotation can be defined annotations life cycle, only the class file for the default

  • Only RetentionPolicy.SOURCEcompile: ;
  • Only class RetentionPolicy.CLASSfile: ;
  • Runtime:RetentionPolicy.RUNTIME .

Let's look at how to use custom annotations, this can be used when we do unit testing

Import java.lang.annotation.ElementType;
 Import java.lang.annotation.Retention;
 Import java.lang.annotation.RetentionPolicy;
 Import java.lang.annotation.Target;
 Import java.lang.reflect.Field; 

// put the most commonly used the parameter is defined as the value (), recommended that all parameters are set default values as much as possible. 
@Target (ElementType.FIELD {}) 
@Retention (RetentionPolicy.RUNTIME) 
@ interface MyAnno {
     int min () default 0 ; 

    int max () default 20 is ; 
} 

class the Person { 
    @MyAnno (min = 10, max = 80 )
     public int age;
}

public class HelloWorld {
    public static void main(String[] args) throws Exception {
        Person p = new Person();
        Person p1 = new Person();
        p.age = 100;
        p1.age = 15;
        Field f = Person.class.getField("age");
        int s = (Integer) f.get(p);
        int s1 = (Integer) f.get(p1);
        MyAnno anno = f.getAnnotation(MyAnno.class);
        System.out.println(s < anno.max() && s > anno.min());  // false
        System.out.println(s1 < anno.max() && s1 > anno.min());  // true
    }
}

Guess you like

Origin www.cnblogs.com/yangshixiong/p/12168933.html