Java Learning Note 11--annotation

Source: Internet
Author: User
Tags deprecated ranges

Java Learning Note 11--annotation

Annotation: A new feature added after JDK1.5, known as the metadata feature, is called a comment after JDK1.5, that is, the use of annotations to add information about some programs.

The Java.lang.annotation annotation interface is an interface that all annotation must implement.

System built-in annotation

After JDK1.5, the system has established the following three built-in annotation types, users can directly use.

@Override: Overwrite the annotation

@Deprecated: annotation used in disapproval

@SuppressWarnings: annotation to suppress safety warnings

Custom annotation

Annotation Definition Format:

"Public" @interface annotation name {

Data type variable name ();

}

[Java]View Plaincopy
    1. Public @interface meaning {
    2. String value ();
    3. }


After that, you can use the @meaning format directly in your program.

[Java]View Plaincopy
    1. @Meaning (value="Itmyhome")
    2. Class demo{
    3. }


You can set a parameter in annotation to receive the contents of the variable, such as value above, and when using annotation, you must assign a value to the parameter such as: Value= "Itmyhome"

Since you can set a parameter, you can also set multiple parameters.

[Java]View Plaincopy
    1. Public @interface meaning {
    2. String key ();
    3. String value ();
    4. }


This annotation need to set two parameters when using, a key one value

[Java]View Plaincopy
    1. @Meaning (key="Hi", value="Itmyhome")
    2. Class demo{
    3. }


You can also set an array in

[Java]View Plaincopy
    1. Public @interface meaning {
    2. String[] Value ();
    3. }


The received content to pass the array

[Java]View Plaincopy
    1. @Meaning (value={"Hello","World"})
    2. Class demo{
    3. }

There is a feature in all the annotation defined above, all the parameter contents need to be set up when using the annotation, then you can set the default content for a parameter, use default when declaring.

[Java]View Plaincopy
    1. Public @interface meaning {
    2. String value () default ""; //default is empty
    3. }

You can not set a value when you use it

[Java]View Plaincopy
    1. @Meaning
    2. Class demo{
    3. }


In the operation, for a annotation, sometimes the fixed period of the value range, only a fixed number of values, this time actually need to rely on enumeration.

[Java]View Plaincopy
    1. Public enum Formitemtype { //define enum type
    2. Hidden,text,select,date
    3. }


define Annotation

[Java]View Plaincopy
    1. Public @interface meaning {
    2. Formitemtype value (); //Set to enum type
    3. }


The value of the annotation can only be a value in the enumeration type

[Java]View Plaincopy
    1. @Meaning (Value=formitemtype.date)
    2. Class demo{
    3. }


Retention and Retentionpolicy

In annotation, you can use retention to define the save scope of a annotation, which is defined as follows:

[Java]View Plaincopy
    1. @Retention (Retentionpolicy.runtime)
    2. @Target (Elementtype.field)
    3. Public @interface meaning {
    4. Formitemtype value (); //Set to enum type
    5. }


In the retetion definition above, there is a retentionpolicy variable, which is used to specify annotation's save range, Retentionpolicy contains three ranges

Of the three ranges, the most important one is the runtime range, because it works at the time of execution.

Built-in annotation retentionpolicy

Three built-in definitions of annotation:

The override definition uses @retention (retentionpolicy.source) to appear only in the source file

The deprecated definition uses @retention (retentionpolicy.runtime), which can occur at execution time.

The suppresswarnings definition is @retention (retentionpolicy.source) and can only appear in the source file

A annotation if you want to make it meaningful, you must combine the reflection mechanism to get all the content set in the annotaion.

There are several methods in class that are related to annotation operations

[Java]View Plaincopy
  1. Package com.itmyhome;
  2. Import java.lang.annotation.Annotation;
  3. Import Java.lang.reflect.Method;
  4. Class demo{
  5. @SuppressWarnings ("unchecked")
  6. @Deprecated
  7. @Override
  8. Public String toString () {
  9. return "Hello";
  10. }
  11. }
  12. Public class T {
  13. public static void Main (string[] args) throws exception{
  14. class<?> C = class.forname ("Com.itmyhome.Demo");
  15. Method MT = C.getmethod ("toString"); //Find the ToString method
  16. Annotation an[] = mt.getannotations (); //Get all the annotation
  17. For (Annotation a:an) {
  18. System.out.println (a);
  19. }
  20. }
  21. }


At this point a annota has been made. The above operations are actually done through the built-in annotation of three systems, or you can customize a annotation

[Java]View Plaincopy
    1. @Retention (Retentionpolicy.runtime)
    2. @Target (Elementtype.method)
    3. Public @interface meaning {
    4. Formitemtype value (); //Set to enum type
    5. }

[Java]View Plaincopy
  1. Package com.itmyhome;
  2. Import Java.lang.reflect.Method;
  3. Class demo{
  4. @Meaning (Value=formitemtype.select) //Custom annotation
  5. @SuppressWarnings ("unchecked")
  6. @Deprecated
  7. @Override
  8. Public String toString () {
  9. return "Hello";
  10. }
  11. }
  12. Public class T {
  13. public static void Main (string[] args) throws exception{
  14. class<?> C = class.forname ("Com.itmyhome.Demo");
  15. Method MT = C.getmethod ("toString"); //Find the ToString method
  16. //The specified comment is present on this element
  17. if (mt.isannotationpresent (meaning. Class)) {
  18. meaning M = mt.getannotation (meaning.   Class); //Get the specified annotation
  19. System.out.println (M.value ()); //Get the value of annotation
  20. }
  21. }
  22. }


@Target

Indicates the kind of program element that the annotation type applies to. If a Target meta comment does not exist in the annotation type declaration, the declared type can be used on either program element. If such meta-annotations exist, the compiler enforces the specified usage restrictions, such as: @Target (Elementtype.annotation_type)

ElementType Range of storage

Java Learning Note 11--annotation

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.