On the development of java custom annotation

A, jdk built-in notes

  @Override labeled must override the parent method of the parent class or interface, or the compiler does not pass   

  Labeled outdated method for @Deprecated

  @SuppressWarings for suppressing compiler warnings, marked the place will not compile-time warning

  @SafeVarargs suppression heap pollution warning, can suppress warnings when using generic

Second, meta-annotation

  Meta-annotation is described annotation notes.

  The annotation @Target scope defined annotation, the following scopes

                     

  @Retention the annotation defines the scope annotation survival, the survival of a total of three scopes, Source, Class, Runtime. Runtime which survived the longest time, we custom annotation survival scope must be Runtime

  @Documented are marked marked the notes when making reservations javadoc will go

  @Inherited annotated by the annotation tag having inherited, if the class has a subclass tag may be acquired from the same subclass annotation by reflection

Third, how to develop custom annotations

  1, write your own self-righteousness of class notes

  2, writing notes parsing class

  3, writing notes handled by category

  Unit testing to simulate annotation, for example, the following code

package com.wzw.activemqstudy.annotation;

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)
public @interface MyTest {
}
Package com.wzw.activemqstudy.annotation; 

Import java.lang.reflect.InvocationTargetException;
 Import the java.lang.reflect.Method;
 / ** 
 * custom annotation parsing class 
 * / 
public  class MyTestParse {
     public  static  void main (String [] args) throws IllegalAccessException, an InstantiationException is, a InvocationTargetException {
         // get the test class bytecode object 
        class CLZ = TestDemo. class ;
         // get all objects by the reflection method 
        method, [] = methods clz.getMethods ();
        IF ( null ! =Methods) {
            for (Method, Method: Methods) {
                // if there is a determination method @MyTest comment tag 
               IF (method.isAnnotationPresent (the MyTest. class )) { 
                    Method.invoke (clz.newInstance (), null ); 
               } 
           } 
       } 

    } 
}
Package com.wzw.activemqstudy.annotation; 

/ ** 
 * class custom annotations to process 
 * / 
public  class TestDemo { 
    @MyTest 
    public  void test1 () { 
        System.out.println ( "Run test1 ....... ...... " ); 
    } 
    @MyTest 
    public  void test2 () { 
        System.out.println ( " run ........... test2 " ); 
    } 
    @MyTest 
    public  void Test3 () { 
        System.out.println ( "run ........... Test3" ); 
    } 
}

 

 

  

Guess you like

Origin www.cnblogs.com/wzw2java/p/12445631.html