【 Part one :】
  Find out java1.5 From the default three annotation type : 
 @Override:   Can only be used on methods , This method is used to tell others that this method is to rewrite the parent class . 
 @Deprecated:  Advise others not to use old ones API I used it when I was there , When compiling, it will generate a warning message , It can be set on all the elements in the program . 
 @SuppressWarnings: This type can be used to turn off some warning messages temporarily . 
 

【 Part two :】
  Let's talk about it first annotation The concept of , Let's talk about how to design your own annotation.

  First, in the jdk Self contained java.lang.annotation Inside the bag , Open the following source files :

 1, source file Target.java 
  Java code  
  @Documented  
  @Retention(RetentionPolicy.RUNTIME)   
  @Target(ElementType.ANNOTATION_TYPE)   
  public @interface Target {   
     ElementType[] value();   
  } 

     @Documented
     @Retention(RetentionPolicy.RUNTIME)
     @Target(ElementType.ANNOTATION_TYPE)
     public @interface Target {
        ElementType[] value();
     }

  Among them @interface It's a keyword , In design annotations When you have to put a
Types are defined as @interface, It doesn't work class or interface keyword

 2, source file Retention.java 
  Java code  
  @Documented  
  @Retention(RetentionPolicy.RUNTIME)   
  @Target(ElementType.ANNOTATION_TYPE)   
  public @interface Retention {   
     RetentionPolicy value();   
  } 

 

  All the documents above are used RetentionPolicy,ElementType These two fields , You might have guessed that these are two java file . you bet , The source code for these two files is as follows :

 3, source file RetentionPolicy.java 
  Java code  
  public enum RetentionPolicy {   
   SOURCE,   
   CLASS,   
   RUNTIME   
  } 

  This is a enum type , There are three values , namely SOURCE,CLASS and RUNTIME. 

 SOURCE It's for this Annotation Type information is only kept in the source code of the program , If the source code is compiled ,Annotation The data will disappear , It doesn't stay in the compiled .class In the file . 

 ClASS It means this Annotation Type information is kept in the source code , It will also be kept in the compiled .class In the file , At the time of execution , This information is not loaded into the virtual machine (JVM) In the middle . Pay attention , When you don't set one Annotation Type Retention Value , The system default value is CLASS. 
 RUNTIME, In the source code , Compiled .class Keep information in file , Some of the information will be loaded at this time JVM In the middle . 
 
  Take an example , as @Override Inside Retention Set to SOURCE, If the compilation is successful, you don't need to check the information ; contrary ,@Deprecated Inside Retention Set to RUNTIME, It indicates that we will be warned which is used by Deprecated Methods , It is also possible to find out whether the method has been implemented Deprecated.

 4, source file ElementType.java 
  Java code  
  public enum ElementType {   
   TYPE, FIELD, METHOD, PARAMETER, CONSTRUCTOR,   
   LOCAL_VARIABLE, ANNOTATION_TYPE,PACKAGE   
  } 

   
 @Target Inside ElementType Is used to specify Annotation What elements can a type be used on . Explain :TYPE( type ), FIELD( attribute ),
METHOD( method ), PARAMETER( parameter ), CONSTRUCTOR( Constructor ),LOCAL_VARIABLE( local variable ),
ANNOTATION_TYPE,PACKAGE( package ), Among them TYPE( type ) Can be used in Class,Interface,Enum and Annotation Type . 
  in addition , from 1 Can be seen from the source code ,@Target I also use myself to declare myself , Can only be used in ANNOTATION_TYPE above . 
  If one Annotation The type is not specified @Target What elements are used on , Then it can be used on any element , The elements here refer to the eight types above . 
  Take a few correct examples : 
 @Target(ElementType.METHOD) 
 @Target(value=ElementType.METHOD) 
 @Target(ElementType.METHOD,ElementType.CONSTRUCTOR) 
  For specific reference javadoc file


  Up and down 1 and 2 Source file for , They're all used @Documented,@Documented The goal is to make this one Annotation Type information can be displayed in the javaAPI On the instruction document ; If not added , use javadoc generate API The information generated by this type will not be found in the document . 
  Another point , If you need to Annotation The data of is inherited to the subclass , Then it will be used @Inherited This one Annotation type .

  Part three : The following design is the simplest Annotation example , This example shares four files ; 
 1,Description.java 
 Java code  
  package lighter.iteye.com;   
    
  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 Description {   
      String value();   
  } 

 

  explain : be-all Annotation Will automatically inherit java.lang.annotation This interface , So you can't inherit other classes or interfaces . 
  The most important point ,Annotation How to set the parameters in the type : 
  first , It can only be used public Or default (default) These two access rights are decorated . for example ,String value(); Here we set the method as defaul Default type . 

  second , Parameter members can only use basic types byte,short,char,int,long,float,double,boolean Eight basic data types and String,Enum,Class,annotations Etc , And arrays of these types . for example ,String
value(); The parameter member here is String. 
  third , If there is only one parameter member , It is best to set the parameter name to "value", Parenthesis after . example : The above example has only one parameter member .

 2,Name.java 
 Java code  
  package lighter.iteye.com;   
    
  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;   
   
   // Pay attention here @Target And @Description The difference in , Parameter members are also different    
  @Target(ElementType.METHOD)   
  @Retention(RetentionPolicy.RUNTIME)   
  @Documented  
  public @interface Name {   
      String originate();   
      String community();   
  } 

  
 3,hzu_OpenSource.java 
 Java code  
  package lighter.iteye.com;   
    
  @Description("Hzu_OpenSource, Do the best Java Employment training ")   
  public class hzu_OpenSource {   
      @Name(originate=" Sponsor? : Department of computer and information engineering, Heze University ",community="java Employment training ")   
      public String getName()   
      {   
          return null;   
      }   
         
      @Name(originate=" Sponsor? : Beijing Kaiyuan Huizhi ",community="java Employment training ")   
      public String getName2()   
      {   
          return "java, My life !";   
      }   
  } 

}
4, last , Write an extraction that can be run hzu_OpenSource Class of information TestAnnotation 
Java code  
 package lighter.iteye.com;   
  
  import java.lang.reflect.Method;   
  import java.util.HashSet;   
  import java.util.Set;   
  
  public class TestAnnotation {   
    /**  
     * author lighter  
     * explain : Specific guantian Annotation Of API Please refer to javaDoc file   
     */  
       public static void main(String[] args) throws Exception {   
       String  CLASS_NAME = "lighter.iteye.com.hzu_OpenSource";   
       Class  test = Class.forName(CLASS_NAME);   
         
       boolean flag = test.isAnnotationPresent(Description.class);   
        if(flag)   
        {   
            Description des =
(Description)test.getAnnotation(Description.class);   
            System.out.println(" describe :"+des.value());   
            System.out.println("-----------------");   
        }   
           
        // hold JavaEyer This category has been used @Name Save all methods to Set In the middle    
        Method[] method = test.getMethods(); 
        Set<Method> set = new HashSet<Method>();   
        for(int i=0;i<method.length;i++)   
        {   
            boolean otherFlag = method[i].isAnnotationPresent(Name.class);   
            if(otherFlag) set.add(method[i]);   
        }   
        for(Method m: set)   
        {   
            Name name = m.getAnnotation(Name.class);   
            System.out.println(name.originate());   
            System.out.println(" Communities created :"+name.community());   
        }   
     }   


5, Operation results

Technology
©2019-2020 Toolsou All rights reserved,
What are the protection measures for website security ? Talking about uni-app Page value transfer problem Remember once EventBus Project issues caused by memory leaks I've been drinking soft water for three years ? What is the use of soft water and water softener Time conversion front desk will 2020-07-17T03:07:02.000+0000 Into 2020-07-17 11:07:02 Chuan Shen Count the number of letters (java Language implementation ) Time format 2020-01-13T16:00:00.000Z Medium T and Z What do they mean , How to deal with it msf Generate Trojan horse attack android mobile phone The world's first free open source ERP Odoo Ubuntu The best development environment What's the difference in server prices ?