Java annotation retention, documented, inherited introduction _java

Source: Internet
Author: User
Tags reflection

retention annotation

Retention (retention) Notes indicate that this type of annotation will be retained at that stage. has three values:
1.retentionpolicy.source--This type of annotations is reserved at the source level only, and is ignored at compile time
2.retentionpolicy.class--This type of annotations is kept at compile time and exists in the CLASS file, but the JVM will ignore
3.retentionpolicy.runtime--This type of annotations will be retained by the JVM, so they can be read and used at runtime by the JVM or other code that uses the reflection mechanism.
Example 5 demonstrates the Retentionpolicy.runtime declaration:

Example of Java Annotations 1:

Copy Code code as follows:

@Retention (Retentionpolicy.runtime)
Public @interface Test_retention {
String dotestretention ();
}

In this example, the @Retention (retentionpolicy.runtime) annotation indicates that the test_retention annotation will be retained by the virtual machine so that it can be read at run time by reflection.

documented annotation

Documented annotations indicate that this annotation should be logged by the Javadoc tool. By default, Javadoc does not include annotations. However, if a @Documented is specified when the annotation is declared, it is handled by a tool such as Javadoc, so the annotation type information is also included in the generated document. Example 6 further demonstrates the use of @Documented:

Example of Java Annotations 2:

Copy Code code as follows:

@Documented
Public @interface test_documented {
String dotestdocument ();
}

Next, modify the Testannotations class as follows:

Copy Code code as follows:

public class Testannotations {
public static void Main (String arg[]) {
New Testannotations (). Dosometestretention ();
New Testannotations (). dosometestdocumented ();
}
@Test_Retention (dotestretention= "Retention annotation Information test")
public void Dosometestretention () {
System.out.printf ("Test annotation type ' Retention '");
}
@Test_Documented (dotestdocument= "Hello document")
public void dosometestdocumented () {
System.out.printf ("Test annotation type ' documented '");
}
}

Now, if you use the Javadoc command to generate the testannotations.html file, you will see a result similar to Figure 1.

As you can see from the screenshot, there is no annotation-type information () method for the Dosometestretention () method in the document. However, the documentation for the Dosometestdocumented () method provides descriptive information for the annotation. This is because the @Documented tag is added to the test_documented annotation. The previous annotation test_retention did not specify @Documented tag (tag).

inherited annotation (This paragraph may be problematic ...)

This is a slightly more complex type of annotation. It indicates that the class being annotated is automatically inherited. More specifically, if you define a callout with a @Inherited tag and then use a defined annotation to annotate another parent class and a subclass of the parent class (subclass), all the properties of the parent class will be inherited into its subclasses. In Example 7, you'll see the benefits of using @Inherited tags.

Example of Java Annotations 3

First, define your annotations:

Copy Code code as follows:

@Inherited
Public @interface Myparentobject {
Boolean isinherited () default true;
String dosomething () default "do what";
}

Next, a class is annotated with a note:

Copy Code code as follows:

@MyParentObject
Public Class Mychildobject {
}

As you can see, you do not need to define an interface method in the implementation class. These are automatically inherited because @Inherited tags are used. What would it look like if you used an old way to define an implementation class? Look at this old way of achieving this:

Copy Code code as follows:

public class Mychildobject implements Myparentobject {
public Boolean isinherited () {
return false;
}
Public String dosomething () {
Return "";
}
public boolean equals (Object obj) {
return false;
}
public int hashcode () {
return 0;
}
Public String toString () {
Return "";
}
Public Class Annotationtype () {
return null;
}
}

See the difference? As you can see, you have to implement all the methods of the parent interface. In addition to isinherited () and the Myparentobject dosomething () method, you also need to implement Java.lang.Object equals (), toString (), and Hascode () methods. There is also the Annotationtype () method of the Java.lang.annotation.Annotation class. Whether you want to implement these methods or not, you must include these in the inherited objects.

Conclusion

This article shows you how to make development easier by using the JDK5 annotation feature. Annotations do not directly affect the semantics of the program.  Development and deployment Tools can read these annotations in some way and process them, using programs that include annotations to replace additional Java source files, XML documents, or other old artifacts. Annotations allow you to do the same thing with less code and have better compile-time error detection mechanisms. The purpose of annotations is to devote less time to the business logic rules in those diehard useless details. This article is the first part of the Java Annotation series. In the second section, you will learn how to use annotations to develop a simple Web application. Finally, in the third part, you'll see a complex example that includes multiple database tables.

Related Article

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.