Java Comment @interface the use of "go"

Source: Internet
Author: User

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.

The

Annotation @retention can be used to modify annotations, which are annotations, called meta-annotations. The
retention annotation has a property of value, is of type Retentionpolicy, enum Retentionpolicy is an enumeration type, and
This enumeration determines how retention annotations should be persisted, It can also be understood as rentention with Rententionpolicy. Retentionpolicy has 3 values: class  runtime   SOURCE
with @retention (retentionpolicy.class ) modified annotations, indicating 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;
Annotations decorated with @retention (retentionpolicy.source). The information that represents the annotation is discarded by the compiler and is not left in the class file, and the annotated information is only left in the source file;
@retention (retentionpolicy.runtime)-modified annotations that indicate that the information of the annotations is retained in the class file ( bytecode files), when the program is compiled, is kept by the virtual machine at run time,
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 parsing the program.

Package Com.self;import Java.lang.annotation.retention;import Java.lang.annotation.RetentionPolicy; @Retention ( retentionpolicy.runtime) public @interface mytarget{} defines an annotated @mytarget, which is decorated with retentionpolicy.runtime; 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", n  ULL); if (Method.isannotationpresent (Mytarget.class))//If annotation @mytarget exists on the DoSomething method, true {System.out.println (  Method.getannotation (Mytarget.class)); }}} above program print: @com. Self.mytarget (), if the Retentionpolicy value is not runtime, it is not printed. @Retention (retentionpolicy.source) public @interface override@retention (retentionpolicy.source) public @interface Suppresswarnings@retention (retentionpolicy.runtime) public @interface deprecated from the above can be seen, Only annotations @deprecated can be read into annotations at run time by the JVM to define attributes, see example: @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, The default value is Gege property, the world type is string, there is no default Value property for array type, the default value is 2,4,5,6 property, the lamp type is an enumeration, there is no default Value property lannotation type is annotation, The default value is @testannotation, and the attribute in the annotation is the annotation property, the style type is class, and the default is a class type of type string, see the following example: Defines a mytest class, with annotations @myannotation adornments, Annotation @myannotation defined properties are assigned a value @myannotation (hello = "Beijing", world= "Shanghai", array={},lamp=trafficlamp.red,style= Int.class) public class mytest{@MyAnnotation ([email protected] (value= "Baby"), world = "Shanghai", array={1,2,3}, Lamp=trafficlamp.yellow) @Deprecated @SuppressWarnings ("") public void output () {System.out.println ("output something !"); }} The message 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 (MyTest.class.isAnnotationPresent (Myannotation.class)) {MyTest If there is an annotation @myannotation adornment on the class name) {  System.out.println ("have annotation"); } if (Method.isannotationpresent (Myannotation.class)) {Method.invoke (myTest, NULL);//Call output method//Get annotations on method @myanno    tation information Myannotation myannotation = method.getannotation (Myannotation.class);   String Hello = Myannotation.hello ();   String world = Myannotation.world (); System.out.println (Hello + "," + world);//Print Properties Hello and World Value System.out.println (Myannotation.array (). length);// Prints the property of the array length 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, Retentionpolicy.runtime modified annotation[] annotations = method.getannotations (); for (Annotation annotation:annotations) {System.out.println (Annotation.annotatiOntype (). GetName ()); }}} above program print: Have Annotationoutput something!gege, Shanghai3babyclass java.lang.Stringcom.heima.annotation.MyAnnotationjava.lang.Deprecated If there is an attribute name called value in the annotation, you can omit the property name when you apply it. 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 ();} It can be used this way: @MyTarget ("AAA") public void DoSomething () {System.out.println ("Hello World");} annotation @target is also a meta-annotation used to modify annotations, which has a property El Ementtype is also an enumeration type with a value of: Annotation_type CONSTRUCTOR FIELD local_variable METHOD package PARAMETER type such as @target ( Elementtype.method) A modified annotation 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 in the class, then the program error, such as: @MyTargetpublic class mytargettest annotations are mostly used in the development framework, OK, the relevant annotations on learning so much, thank you.

  

Java Comment @interface the use of "go"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.