Java uses @interface annotation{} to define an annotation @Annotation, and one annotation is a class.
@Override, @Deprecated, @SuppressWarnings 3 common annotations.
Annotations are equivalent to a mark, and adding annotations to a program is tantamount to adding some kind of markup to the program,
Javac compilers, development tools and other programs can use reflection to understand your class and the various elements have no markup, see what you have the mark, go to do the corresponding thing.
Note @override is used on methods, when we want to rewrite a method, add @override to the method, when we approach
When the name is wrong, the compiler will error
Annotation @deprecated, which is used to indicate that a class's properties or methods are obsolete and do not want others to use in properties and methods
Modified with @deprecated,
Note @suppresswarnings is used to suppress warnings from the program, such as when no generics are used or the method is obsolete.
Annotation @retention can be used to modify annotations, which are annotations, called meta-annotations.
The retention annotation has an attribute value, which is of type Retentionpolicy, and enum Retentionpolicy is an enumeration type,
This enumeration determines how retention annotations should be maintained, and can also be understood as rentention with Rententionpolicy. Retentionpolicy has 3 values:CLASS RUNTIME SOURCE
Annotated with @retention (Retentionpolicy.class), which indicates that the information of the annotations is kept in the CLASS file (bytecode file) when the program is compiled, but is not read by the virtual machine at run time;
@retention (Retentionpolicy.source) modified annotations, indicating that the information of the annotations will be discarded by the compiler, will not be left in the class file, the information will only remain in the source file;
Annotated with @retention (Retentionpolicy.runtime), indicating that the information of the annotations is retained in the class file (bytecode file) when the program compiles, the virtual machine is kept at runtime,
So they can be read in a reflective way. Retentionpolicy.runtime allows you to read the annotation annotation information from the JVM so that it can be used when analyzing the program.
- Package com.self;
- Import java.lang.annotation.Retention;
- Import Java.lang.annotation.RetentionPolicy;
- @Retention (Retentionpolicy.runtime)
- Public @interface Mytarget
- { }
- Define an annotated @MyTarget and modify it with Retentionpolicy.runtime;
- Package com.self;
- Import Java.lang.reflect.Method;
- Public class Mytargettest
- {
- @MyTarget
- public void DoSomething ()
- {
- System.out.println ("Hello World");
- }
- public static void Main (string[] args) throws Exception
- {
- method = Mytargettest. Class.getmethod ("dosomething",null);
- if (method.isannotationpresent (mytarget. Class)//True if annotation @mytarget is present on the DoSomething method
- {
- System.out.println (Method.getannotation (mytarget. Class));
- }
- }
- }
- The above program prints:@com. Self.mytarget () and does not print if the Retentionpolicy value is not runtime.
- @Retention (Retentionpolicy.source)
- Public @interface Override
- @Retention (Retentionpolicy.source)
- Public @interface Suppresswarnings
- @Retention (Retentionpolicy.runtime)
- Public @interface Deprecated
- As can be seen, only annotations @Deprecated can be read by the JVM at run time
- You can define attributes in the annotations to see examples:
- @Retention (Retentionpolicy.runtime)
- Public @interface Myannotation
- {
- String Hello () default "Gege";
- String World ();
- int[] Array () default { 2, 4, 5, 6};
- Enumtest.trafficlamp lamp ();
- Testannotation lannotation () default @TestAnnotation (value = "ddd");
- Class style () default String. Class
- }
- In the above program, define an annotation @MyAnnotation, define 6 attributes, their name is:
- Hello,world,array,lamp,lannotation,style.
- Property Hello type is string, default value is Gege
- Property world Type is string, no default value
- Property array type is an array with default values of 2,4,5,6
- Attribute lamp Type is an enumeration with no default value
- The attribute lannotation type is annotated, the default value is @TestAnnotation, and the attribute in the annotation is the annotation
- Property style type is class, default value is String type class type
- Take a look at the following example: Defines a mytest class, with annotations @MyAnnotation adornments, annotations @MyAnnotation defined properties are assigned values
- @MyAnnotation (hello = "Beijing", world="Shanghai", array={},lamp=trafficlamp.red,style=Int.Class)
- Public class MyTest
- {
- @MyAnnotation (lannotation=@TestAnnotation (value="Baby"), world = "Shanghai", array={1, 2,3},lamp=trafficlamp.yellow)
- @Deprecated
- @SuppressWarnings ("")
- public void output ()
- {
- System.out.println ("Output something!");
- }
- }
- The information of the annotations is then read by reflection:
- Public class Myreflection
- {
- public static void Main (string[] args) throws Exception
- {
- MyTest MyTest = new MyTest ();
- class<mytest> C = MyTest. class;
- method = C.getmethod ("Output", new class[] {});
- true if the MyTest class name has annotations @myannotation adornments
- if (MyTest. Class.isannotationpresent (myannotation. Class))
- {
- System.out.println ("have annotation");
- }
- if (method.isannotationpresent (myannotation. Class))
- {
- Method.invoke (myTest, null); //Call the output method
- //Get information on method annotations @myannotation
- Myannotation myannotation = method.getannotation (myannotation. Class);
- String Hello = Myannotation.hello ();
- String world = Myannotation.world ();
- System.out.println (hello + "," + World); Print properties Hello and world values
- System.out.println (Myannotation.array (). length); //Print Properties The length of array arrays
- System.out.println (Myannotation.lannotation (). value ()); //Print the value of the attribute Lannotation
- System.out.println (Myannotation.style ());
- }
- //Get all annotations on the output method, of course, are retentionpolicy.runtime modified
- annotation[] annotations = method.getannotations ();
- For (Annotation annotation:annotations)
- {
- System.out.println (Annotation.annotationtype (). GetName ());
- }
- }
- }
- The above program prints:
- have annotation
- Output something!
- Gege, Shanghai
- 3
- Baby
- Class Java.lang.String
- Com.heima.annotation.MyAnnotation
- java.lang.Deprecated
- If there is an attribute name called value in the annotation, you can omit the property name from being applied.
- Visible,in the @Retention (retentionpolicy.runtime) annotation, retentionpolicy.runtime is the annotation attribute value, the property name is value,
- The return type of the property is Retentionpolicy, as follows:
- Public @interface Mytarget
- {
- String value ();
- }
- This can be used:
- @MyTarget ("AAA")
- public void DoSomething ()
- {
- System.out.println ("Hello World");
- }
- Annotation @Target is also a meta-annotation used to modify annotations, which has an attribute ElementType is also an enumeration type.
- Value: Annotation_type CONSTRUCTOR FIELD local_variable METHOD Package PARAMETER TYPE
- An annotated note such as @Target (Elementtype.method) indicates that the annotation can only be used to decorate the method.
- @Target (Elementtype.method)
- @Retention (Retentionpolicy.runtime)
- Public @interface Mytarget
- {
- String value () default "hahaha";
- }
- If the @MyTarget modified on the class, then the program error, such as:
- @MyTarget
- Public class Mytargettest
Usage of Java annotation @interface