We used the JDK to provide us with the @Override @Deprecated @SuppressWarning annotations that the JDK provided to us, we are just using someone else to write the good things, then we can write their own annotations? Of course it's possible.
The notes we wrote included three links
1, the statement of the annotations, that is, the definition of the annotation class as follows @interface to make annotations declaration
Package me.test;
import java.lang.annotation.*;//importing all annotations in the note package
@Retention (Retentionpolicy.runtime)//explained as follows
@Target (Elementtype.type)//explained as follows
Public @interface Myannotation
{
}
@Retention (Retentionpolicy.runtime)
The lifetime of annotations that are reserved for annotations is specified with a three-retentionpolicy enumeration of three values representing three declaration periods default is class
Enumeration Constants Summary
CLASS
The compiler will record annotations in the class file, but the VM does not need to retain annotations at run time.
RUNTIME
The compiler will record annotations in the class file, and the VM will retain comments at run time, so it can be read in a reflective manner.
SOURCE
The comment that the compiler will discard.
@Target (Elementtype.type)
This identifier annotation should be marked there. Several enumerated values of ElementType represent where the annotations should be written.
CONSTRUCTOR
Construction method declaration
FIELD
Field declarations (including enumeration constants)
Local_variable
Local variable declaration
METHOD
Method declaration
Package
Package Declaration
PARAMETER
Parameter declaration
TYPE
classes, interfaces (including annotation types) or enumeration declarations
2. Class or method using annotations
@MyAnnotation
Class A
{
}
3, using reflection to operate the annotations in detail see code
The class class has a method
<a extends Annotation>
A getannotation(class<a> annotationclass)
This method takes an annotated bytecode parameter, and then returns the annotated object identified by the class, because we identify a note that is equivalent to producing a callout object.
Booleanisannotationpresent(class<? extends annotation> Annotationclass)
This method determines whether a class is identified by annotations
Here is the code example
In Myannotationtest.java
Package me.test;
Import java.lang.annotation.Annotation;
@MyAnnotation
public class Myannotationtest
{
public static void Main (String []args)
{
if (MyAnnotationTest.class.isAnnotationPresent (Myannotation.class))
{
Myannotation an= (myannotation) MyAnnotationTest.class.getAnnotation (Myannotation.class);
System.out.println (An.tostring ());
}
}
}
In Myannotation.java
Package me.test;
Import Java.lang.annotation.ElementType;
Import java.lang.annotation.Retention;
Import Java.lang.annotation.RetentionPolicy;
Import Java.lang.annotation.Target;
@Retention (Retentionpolicy.runtime)
@Target (Elementtype.type)
Public @interface Myannotation
{
}
@ In-depth annotations, designing and using your own annotations in Java