@interface < note name >{< annotation attribute type > < annotation property name >[default< default;]}
<span style= "FONT-FAMILY:SIMSUN;FONT-SIZE:18PX;" > @interface mytest{//itself defines the annotation attribute string msg ();} </span>
2. Determine the use target for annotations
For different purposes, annotations can have different use targets.
For example, the method annotation, the constructor annotation, the Variable field annotation, the class or the interface annotation and so on
@Target (elementtype.< use target point >)
<span style= "FONT-FAMILY:SIMSUN;FONT-SIZE:18PX;" >//declares that this annotation can only annotate a class or interface @target (elementtype.type) @interface mytest{//define its own annotation property, String msg (); </span>
3. Determining the use of annotations
Annotations can be used in different ways depending on the purpose of use.
Class: Annotations exist in the Thunder file. But the virtual machine is not able to get annotations confidence when executing
Source: Annotations exist only in the source code. be removed at compile time
Runtime: Annotations exist in the class file, and the virtual machine can obtain annotation information when executing
@Retention (retentionpolicy.< aging value >)
<span style= "FONT-FAMILY:SIMSUN;FONT-SIZE:18PX;" >//declares that this annotation can only annotate a class or interface @target (Elementtype.type)/declares that this annotation can be obtained @retention (retentionpolicy.runtime) at execution time @interface mytest{//itself defines the annotation attribute string msg ();} </span>
4. Get annotations Using Reflection
Some of the previous blogs have talked about reflections, which can also be used to get the annotated information through reflection, similar to the above.
<span style= "FONT-FAMILY:SIMSUN;FONT-SIZE:18PX;" >package com. Annotation;import Java.lang.annotation.*;import java.lang.reflect.*;//declares the use of annotations with the target type @retention ( Retentionpolicy.runtime) @Target (elementtype.type) @interface Myannotationforclass {java.lang.String msg ();} Declares an annotated @retention (retentionpolicy.runtime) @Target (Elementtype.method) using the target METHOD @interface Myannotationformethod {java.lang.String msgPart1 (); java.lang.String msgPart2 ();} The class @myannotationforclass (msg = "This is an annotation to a class") that declares the use of annotations is MyClass {@MyAnnotationForMethod (MsgPart1 = "The first part of the method annotation", MsgPart2 = "Second part of Method annotation") public void SayHello () {System.out.println ("Congratulations on your successful call to the SayHello Method!!
! ");}} Main class public class Sample34_10 {public static void main (string[] args) throws Nosuchmethodexception {//Get class using annotations MyClass corresponding Class object class AC = myclass.class;//Gets the annotations of the MyClass class Myannotationforclass MAFC = (myannotationforclass) ac.getannotation ( Myannotationforclass.class)///Print the annotation information for the class SYSTEM.OUT.PRINTLN (the MyClass class's annotation information is: "" + mafc.msg () + "".
");//Gets the corresponding method object for the specified methods = Ac.getmethod (" SayHello ", new Class[0]);//Gets the corresponding annotation myannotationformethod MAFM = ( Myannotationformethod) method.getannotation (myannotationformethod.class);//printing method The corresponding annotation information System.out.println (" The first part of the SayHello method annotation information is: "" + mafm.msgpart1 () + "".
"); System.out.println (the second part of the SayHello method, the annotation information is: "" + mafm.msgpart2 () + "". ");}} </span>
Two types of annotation information are defined in the above examples. An application to a class or interface. An application to a method, followed by a reflection mechanism to obtain the corresponding annotation class, and then be able to manipulate the annotation information