1. Overview
Annotations can be defined on a method, on a class, a callout is equivalent to a class, which equals an instance of an object, plus annotations, which is equivalent to adding a flag.
Common notes:
@Override: A method that represents a new parent class.
This can also determine whether to overwrite the parent class method, in front of the method plus this statement, if prompted errors, then you are not covered by the parent class method, if the hint of no error, then the overridden parent class method.
@SuppressWarnings ("deprecation"): Suppresses compiler warnings (for example, the method you use is obsolete)
@Deprecated: This statement at the top of the method also indicates that this method is obsolete, or is used on top of the class
Copy Code code as follows:
Import java.util.ArrayList;
Import java.util.List;
public class Annotationdemo {
/*
* For collections, if no type of storage is specified, a security warning is available.
* Add @suppresswarnings (Parameters) to the class or method if you do not want to be prompted for a security warning
*/
@SuppressWarnings ("Unchecked")
public static void Main (string[] args) {
List list=new ArrayList ();
}
}
2. Custom annotations
1. Format
Permission @interface Note name {}
Steps:
Define the annotation class---> Define classes that apply the annotation class---> classes that reflect classes that apply the annotation class (this class can be defined differently, or it can be tested in the application annotation Class).
Copy Code code as follows:
Import java.lang.annotation.Retention;
Importjava.lang.annotation.RetentionPolicy;
Define this annotation to remain in the byte code
@Retention (Retentionpolicy.runtime)
Public @interface Myannotation {
}
@MyAnnotation
Apply a defined annotation class
public class Applymyannotation {
public static void Main (string[] args) {
if (ApplyMyAnnotation.class.isAnnotationPresent (Myannotation.class)) {//Determine if the specified annotation class exists on this class
Myannotation annotation= (myannotation) Applymyannotation.class
. Getannotation (Myannotation.class);
System.out.println (annotation);
}
}
}
2. Declaration Cycle
Format: For example: @Retention (Retentionpolicy.class)
Defines a cycle on a custom annotation class, @Retention (parameter type) parameter type is Retentionpolicy
Retentionpolicy.class: On class file, runtime virtual machine does not preserve annotations
Retentionpolicy.runtime: On class file, runtime virtual preserves annotations
Retentionpolicy.source: On source files, discarding annotations
Suppresswarnings and override are Retentionpolicy.source,
Deprecated is in retentionpolicy.runtime, as defined by Run-time invocation, then it must be retentionpolicy.runtime,
The default is Retentionpolicy.class:
3. Designation of targets
Format: For example: Method @target (Elementtype.method)
A defined annotation can annotate what member. If you do not declare this annotation, you can put it on the elements of any program.
Can be package, interface, parameter, method, local variable, field ... Wait
Copy Code code as follows:
Define this annotation to remain in the byte code
@Retention (Retentionpolicy.runtime)
@Target ({Elementtype.method,elementtype.type})//Can be defined on the method and on the class interface, representing the type
Public @interface Myannotation {
}
@MyAnnotation
Apply a defined annotation class
public class Applymyannotation {
@MyAnnotation//Definition in method
public static void Main (string[] args) {
if (ApplyMyAnnotation.class.isAnnotationPresent (Myannotation.class)) {//Determine if the specified annotation class exists on this class
myannotation annotation = (myannotation) applymyannotation.class
. Getannotation (Myannotation.class);
System.out.println (annotation);
}
}
}
3. Add attributes for annotations
1. Type
The attributes of the annotation can be: 8 basic data types, String, enumeration, annotation, Class, array type,
2. Note the point
When there is only one attribute in the annotation or only one attribute needs to be assigned, it can be written directly at the time of the call, without the need to specify a property name.
You can omit {} When the annotation property is an array type and only assigns a value when the value is assigned.
3. Example
3.1. Property type (is String)
Copy Code code as follows:
Import Java.lang.annotation.ElementType;
Import java.lang.annotation.Retention;
Import Java.lang.annotation.RetentionPolicy;
Import java.lang.annotation.*;
Define this annotation to remain in the byte code
@Retention (Retentionpolicy.runtime)
Public @interface Myannotation {
String value ();
String Color () default "red";//setting defaults to "Red"
}
@MyAnnotation ("Java")
public class Applymyannotation {
public static void Main (string[] args) {
/**
* This is to get annotations on the class, or to get annotations on the method, as an example of getting annotations on a class
*/
if (ApplyMyAnnotation.class.isAnnotationPresent (Myannotation.class)) {//Determine if the specified annotation class exists on this class
myannotation annotation = (myannotation) applymyannotation.class
. Getannotation (Myannotation.class);
System.out.println ("value=" +annotation.value ());
System.out.println ("color=" +annotation). Color ());
}
}
}
Results:
Value=java
Color=red
From the calling program, you can also see that the property name can be omitted if only one property can be assigned a value. Otherwise @ Annotation Class (property name = value)
3.2. Integrated type
Copy Code code as follows:
/* Enum class * *
public enum week{
Sun,mon;
}
/**
* Annotation Class
*/
Public @interface Annotationtext {
String value ();
}
Import Java.lang.annotation.ElementType;
Import java.lang.annotation.Retention;
Import Java.lang.annotation.RetentionPolicy;
Import java.lang.annotation.*;
Define this annotation to remain in the byte code
@Retention (Retentionpolicy.runtime)
Public @interface Myannotation {
String value ();
String Color () default "red";//setting defaults to "Red"
Week Week () default week.mon;//enum type
int [] Array () default {1,2,3};//array type
Annotationtext annotation () Default @annotationText ("my");//annotation type
Class Classdemo () default Integer.class;//class type
}
@MyAnnotation (value= "Java", color= "green", week=week.sun,array=5,annotation= @annotationText ("You"), classdemo= String.class)//Array array={4,5,6}
public class Applymyannotation {
public static void Main (string[] args) {
/**
* This is to get annotations on the class, or to get annotations on the method, as an example of getting annotations on a class
*/
if (ApplyMyAnnotation.class.isAnnotationPresent (Myannotation.class)) {//Determine if the specified annotation class exists on this class
Myannotation annotation= (myannotation) Applymyannotation.class
. Getannotation (Myannotation.class);
System.out.println ("value=" +annotation.value ());
System.out.println ("color=" +annotation). Color ());
System.out.println ("week=" +annotation.week ());
SYSTEM.OUT.PRINTLN ("Array length =" +annotation.array (). length);
System.out.println ("annotation Type value =" +annotation.annotation (). Value ());
System.out.println ("class Type value =" +annotation.classdemo ());
}
}
}
Results:
Copy Code code as follows:
Value=java
Color=green
Week=sun
Array length =1
Annotation Type value =you
Class Type value =classjava.lang.string
Annotations on the 4.Method
Copy Code code as follows:
Importjava.lang.annotation.Retention;
Importjava.lang.annotation.RetentionPolicy;
/**
* Annotation Class
*/
@Retention (Retentionpolicy.runtime)
Public @interface annotationtext{
StringValue ();
}
publicclassapplymyannotation{
Publicstaticvoidmain (String[]args) throwsexception{
Methodmethodshow=applymyannotation.class.getmethod ("show");
Annotationtextanno=methodshow.getannotation (Annotationtext.class);
System.out.println (Anno.value ());
}
@annotationText ("Java")
Publicvoidshow () {
System.out.println ("Hello");
}
}
Results: Java