First recognized custom Annotation (annotation), first recognized Annotation

Source: Internet
Author: User

First recognized custom Annotation (annotation), first recognized Annotation

When I was learning Java, I had been familiar with the concept of annotation for a long time. When the Child class describes the parent class method, I should add a line "@ Override" before the method. However, this annotation only serves as a prompt, logically, it does not have any practical effect.

In some third-party frameworks, we often see some custom annotations that make configuration operations very simple.

For example, the EventBus framework recently introduced requires some configuration (such as the thread where the specified event is located) when implementing the event subscription method ). At this time, you do not need to call methods such as setXXX (). You just need to add a line "@ Subscribe (treadMode = xxxx)" before defining this method to complete the configuration, very concise.

The following describes how to use custom annotations.

Use @ iterface to define an annotation, which is similar to defining a class/interface:

public @interface MyAnnotation{    String s() ;    int i() default 0;}

Then I added two member variables s and I in this custom annotation. The form of defining member variables is the same as that of defining methods. The parameter of the method is null, the return value type is the type of the member variable. The default value is followed by the default value of the member variable.

Note that there is no Member method in the annotation.

In addition, you can use four "meta annotations" to set and constrain your custom annotations (which can be understood as annotations ):

@ Target: Specifies the Target of the annotation. There is only one member variable, which is the value defined in ElementType and can be set:
CONSTRUCTOR This annotation is only applied to the constructor field. This annotation is only applied to the FIELD LOCAL_VARIABLE. This annotation is only applied to the local variable METHOD. only applied to the method pacakge. only applied to the package PARAMETER. only applied to the type parameter.

Example:

@Target(ElementType.METHOD)public @interface MyAnnotation{}

This shows that the MyAnnotation annotation is only applied to methods. If @ Target is not set, the annotation can be applied anywhere by default.

Other meta annotations (@ Repeatable, @ Inherited, @ Document) will not be described one by one here.

Now we have created a custom annotation MyAnnotation, and we can apply it to the places we previously specified through @ Target.

For example, add a line of annotation before a method:

@MyAnnotation(s = "XXX",i = 1)public void test(){    //...}

Here, the s and I attributes of the annotation are assigned values in the form of name = value. If the value is not assigned, the value is specified as the default value.

As mentioned at the beginning, custom annotations can simplify configuration operations. Now we have set two configuration parameters for the test method through the MyAnnotion annotation. How can we get the values of these two parameters?

This requires the reflection mechanism of java.

Method method = Name of the class where the Method is located. class. getMethod ("test", null); // obtain the test method Instance if (method. isAnnotationPresent (MyAnnotation. class) {// determines whether to set the annotation MyAnnotation myAnno = method. getAnnotation (MyAnnotation. class); System. out. println (myAnno. s () + myAnno. I ());}

Run this code and you can see that "XXX" and "1" set in the annotation are output ".

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.