Java Annotations Introduction and custom annotation instances

Source: Internet
Author: User

JDK1.5 introduced annotations. By source Category:
Self-Annotated
Third party annotations
Custom annotations

Self-Annotated: for example, Override,deprecated,suppvisewarnnings.
Override is to identify the rewrite, if there is no rewrite, it will be an error.
Deprecated is used to identify obsolete methods, if used, there will be risk hints, and underline, use suppvisewarnnings to confirm the use, eliminate the risk hint.

Third-party annotations: for example, the insertprovider,updateprovider,options among autowired,service,resource,repository.mybatis in spring and so on.

Custom annotations: Public @interface [annotation name]. By operating mechanism classification:
SOURCE annotation
Compile-time annotations
Run-time annotations

SOURCE Note: In the source code when the existence, compiled into a class file is gone
Compile-time annotation: source code exists, compiled into class file also exists
Run-time annotations: To work with programs, such as autowired annotations, you can assemble instances automatically. Custom Annotation instance:

/**
@author: micro_hz
August 21, 2015
 /
//target goal, define the scope of the annotation, here defines the method and the action on the class
@Target ({ Elementtype.method,elementtype.type})
//annotations can inherit
the operating mechanism of @Inherited//annotation annotations
@Retention ( Retentionpolicy.runtime)
//Note When generating documents @Documented public
@interface Description {
    //if only one annotation , it must be value, this time using the annotation without writing the parameter value name and "=", write the content directly description ("")
    String value ();
}
We use this custom annotation in the normal class
Package annotation.test;
/**
@author: micro_hz
August 21, 2015
 ///
Use our custom annotation @Description on the class
("This is a class Annotation") Public
class Annotationtest {
    //using our custom annotation @Description on the method
    ("This is a methods Annotation")
    @ deprecated//uses this annotation to indicate that the method has become obsolete public
    void Deprecatedmethod ()
    {
        System.out.println () ("I am a deprecated Annotation method ");
    }
    public void P ()
    {
        System.out.println (' This is a annotationtest class ');
    }

Import java.lang.annotation.Annotation;

Import Java.lang.reflect.Method; /** @author: Micro_hz August 21, 2015/public class Test {@SuppressWarnings (' deprecation ') public static void Main (S) Tring args[]) {//If using deprecated annotation method, will warn, add suppresswarnnings can confirm use, eliminate risk annotationtest
        = new Annotationtest ();

        Annotationtest.deprecatedmethod (); try {//Get the annotation content on the class//use class to load get this class class<?> C = Class.forName ("Annotation.test.Annot
            Ationtest "); Determine if this class exists Description this note if (C.isannotationpresent (Description.class)) {//if present, print
                Out of the value annotation values of this class Description d = (Description) c.getannotation (Description.class);
            System.out.println (D.value ()); //Get the annotation content on the method/* Method 1///Get all the methods method[] m
            Ethods = C.getmethods ();
           Iterate through all the methods For (methods M:methods) {//Get all annotations for each method annotation[] as = M.getannotations (); Iterate through each note for (Annotation a:as) {//If annotation is description class Type, print out the value if (a instanceof Description) {System.out.print
                    ln (((Description) a). Value ());
            }}/* Method 2 */For (Methods M:methods)
                {//Whether the method has the annotation, and the value if (M.isannotationpresent (description.class) of the annotation is printed)
                {System.out.println ((Description) m.getannotation (Description.class)). Value ());
            catch (ClassNotFoundException e) {//TODO auto-generated catch block
        E.printstacktrace ();
 }
    }
}

Run Result:

I am a deprecated Annotation method-A class Annotation this are a method Annotation this is a
notation
Engineering Catalogue

MU-Course Network Annotation Tutorial
Meta-annotation Scope:

Inheritance is an annotation on an inherited class and does not inherit annotations on the method.

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.